Skip to content

Commit f2a42e0

Browse files
committed
update Ruby example
1 parent 797fe6d commit f2a42e0

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

example-ruby/words.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
require 'yaml'
2+
require 'yaml'
23
require 'parallel'
34
require 'etc'
45
require 'fileutils'
56

67
outdir = 'words'
7-
start = Time.now
88

99
FileUtils.rm_rf(outdir)
1010
Dir.mkdir(outdir)
1111

12+
t = Time.now
13+
1214
sorted = false
1315

1416
paths = Dir['../data/??/**/*.yml']
15-
total_size = 0
1617
count = paths.count
1718

18-
Parallel.each_with_index(paths, in_processes: Etc.nprocessors) do |yaml_path, i|
19+
sizes = Parallel.map_with_index(paths, in_processes: Etc.nprocessors) do |yaml_path, i|
1920
meta = YAML.load_file(yaml_path)
20-
words = IO.read(yaml_path.gsub('.yml', '.txt')).downcase.strip.split(/[^\p{word}]+/).uniq
21+
filepath = yaml_path.gsub('.yml', '.txt')
22+
words = IO.read(filepath).downcase.strip.split(/[^\p{word}]+/).uniq
2123
words = words.sort if sorted
22-
total_size += words.count
2324
outpath = "#{outdir}/#{meta['lang']}-#{meta['code']}.txt"
24-
# puts "[#{i}/#{count}] #{yaml_path}/#{outpath}"
2525
puts(format('[%3d/%d] %s/%s', i, count, yaml_path, outpath))
2626
File.write(outpath, words.join("\n"))
27+
File.size(filepath)
2728
end
2829

29-
secs = Time.now - start
30-
puts "Total size: #{total_size}"
31-
puts "Total time: #{secs} s"
30+
puts "Total size: #{(sizes.sum / 1024.0 / 1024).round} MB"
31+
puts "Total time: #{Time.now - t} s"

0 commit comments

Comments
 (0)