Skip to content

Commit d32ff8d

Browse files
committed
update Crystal example
1 parent eb6ed31 commit d32ff8d

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

example-crystal/src/example-crystal.cr

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
require "json"
22
require "yaml"
33

4-
# TODO: Write documentation for `FastWordsCr`
5-
64
module Example::Crystal
7-
VERSION = "0.2.0"
5+
VERSION = "0.3.0"
86
CHARSET = "aąbcćdeęfghijklłmnńoópqrsśtuvwxyzźż"
97

10-
def self.main(outpath = "words")
8+
def self.main(outdir = "words")
119
with_sorting = false
1210
concurrent = true
1311

14-
prepare_folder(outpath, "*.txt")
12+
prepare_folder(outdir, "*.txt")
1513

1614
file_count = 0
1715
total_size = 0
1816

1917
channel = Channel(Tuple(String, Int64)).new
2018
srcPath = "../data/??/**/*.yml"
21-
# srcPath = "./bibles/??/**/*.yml"
22-
Dir.glob(srcPath, follow_symlinks: true).each do |path|
19+
paths = Dir.glob(srcPath, follow_symlinks: true)
20+
count = paths.size
21+
paths.each do |path|
2322
if concurrent
2423
spawn do
25-
channel.send worker(path, outpath, with_sorting)
24+
channel.send worker(path, outdir, with_sorting)
2625
end
2726
file_count += 1
2827
else
29-
worker(path, outpath, with_sorting)
28+
worker(path, outdir, with_sorting)
3029
end
3130
end
3231
if concurrent
3332
file_count.times do |i|
3433
path, size = channel.receive
3534
total_size += size
36-
# puts("[#{i + 1}/#{file_count}] #{path}")
35+
puts(::sprintf("[%3d/%d] %s", i + 1, file_count, path))
3736
end
3837
end
3938
total_size = total_size / 1024 / 1024
4039
puts("Total size: #{total_size} MB")
4140
end
4241

43-
def self.worker(path, outpath, with_sorting)
42+
def self.worker(path, outdir, with_sorting)
4443
filepath = path.gsub(".yml", ".txt")
4544
text = File.read(filepath).gsub("\n", " ").downcase
4645

@@ -51,10 +50,9 @@ module Example::Crystal
5150
end
5251

5352
meta = File.open(path) { |file| YAML.parse(file) }
54-
filepath = %Q(#{outpath}/#{meta["lang"]}-#{meta["code"]}.txt)
53+
filepath = %Q(#{outdir}/#{meta["lang"]}-#{meta["code"]}.txt)
5554
File.write(filepath, words.join("\n"))
5655
filesize = File.size(filepath)
57-
puts([filepath, filesize])
5856
{filepath, filesize}
5957
end
6058

0 commit comments

Comments
 (0)