Class: CrypticResolver::Resolver::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/cr/counter.rb

Overview


File : counter.rb Authors : Aoran Zeng <[email protected]> Created on : <2022-03-06> Last modified : <2023-05-14>

counter:

Count words in dictionaries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver) ⇒ Counter

Returns a new instance of Counter.



22
23
24
25
26
27
# File 'lib/cr/counter.rb', line 22

def initialize(resolver)
  @word_count_of_two_libs = 0
  @word_count_of_def_lib = 0
  @word_count_of_extra_lib = 0
  @resolver = resolver
end

Instance Attribute Details

#resolverObject

def_lib + extra_lib



16
17
18
# File 'lib/cr/counter.rb', line 16

def resolver
  @resolver
end

#word_count_of_def_libObject

def_lib + extra_lib



16
17
18
# File 'lib/cr/counter.rb', line 16

def word_count_of_def_lib
  @word_count_of_def_lib
end

#word_count_of_extra_libObject

def_lib + extra_lib



16
17
18
# File 'lib/cr/counter.rb', line 16

def word_count_of_extra_lib
  @word_count_of_extra_lib
end

#word_count_of_two_libsObject

def_lib + extra_lib



16
17
18
# File 'lib/cr/counter.rb', line 16

def word_count_of_two_libs
  @word_count_of_two_libs
end

Instance Method Details

#count!(display:) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cr/counter.rb', line 83

def count!(display: )
  count_def_lib(display: display)
  count_extra_lib(display: display)
  @word_count_of_two_libs = @word_count_of_def_lib + @word_count_of_extra_lib

  if display
  puts
  puts "#{@word_count_of_def_lib.to_s.rjust(4)} words in Default library"
  puts "#{@word_count_of_extra_lib.to_s.rjust(4)  } words in  Extra  library"
  puts "#{@word_count_of_two_libs.to_s.rjust(4)    } words altogether"
  end
end

#count_def_lib(display:) ⇒ Object

Count default library



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cr/counter.rb', line 48

def count_def_lib(display: )
  path = CrypticResolver::Resolver::DEFAULT_LIB_PATH
  default_lib = Dir.children path
  unless default_lib.empty?
    puts "Default library: ".bold.green if display
    default_lib.each do |s|
      next if File.file? path + "/#{s}"
      wc = count_dict_words(path,s)
      @word_count_of_def_lib += wc
      # With color, ljust not works, so we disable color
      puts("#{wc.to_s.rjust(5)}  #{s}")   if display
    end
  end
  return @word_count_of_def_lib
end

#count_dict_words(library, dict) ⇒ Object

a.toml b.toml …



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cr/counter.rb', line 32

def count_dict_words(library, dict)
  dict_dir = library + "/#{dict}"
  wc = 0
  Dir.children(dict_dir).each do |entry|
    next unless entry.end_with?('.toml')
    next if File.directory? dict_dir + "/#{entry}"
    sheet_content = @resolver.load_sheet(library, dict, entry.delete_suffix('.toml'))
    count = sheet_content.keys.count

    wc = wc + count
  end
  return wc
end

#count_extra_lib(display:) ⇒ Object

Count extra library



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cr/counter.rb', line 66

def count_extra_lib(display: )
  if path = @resolver.extra_lib_path
    extra_lib = Dir.children path
    unless extra_lib.empty?
      puts "\nExtra library:".bold.green if display
      extra_lib.each do |s|
        next if File.file? path + "/#{s}"
        wc = count_dict_words(path,s)
        @word_count_of_extra_lib += wc
        puts("#{wc.to_s.rjust(5)}  #{s}")  if display
      end
    end
  end
  return @word_count_of_extra_lib
end

#reset!Object



97
98
99
100
101
# File 'lib/cr/counter.rb', line 97

def reset!
  @word_count_of_two_libs = 0
  @word_count_of_def_lib = 0
  @word_count_of_extra_lib = 0
end