Class: LearnWords

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

Constant Summary collapse

USAGE =

TODO: use optparser !!! TODO: use ansicolor instead of highline !!!

"Usage: learn_words FILE [--limit N] [--part M/K]\n"

Class Method Summary collapse

Class Method Details

.exit_with_usage(str = nil) ⇒ Object



67
68
69
70
71
# File 'lib/learn_words.rb', line 67

def self.exit_with_usage(str = nil)
  puts USAGE
  puts("\n" + str) unless str.nil?
  exit 1
end

.run(args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/learn_words.rb', line 73

def self.run(args)

  limit = 3
  part = 1
  parts_count = 1
  words_file = nil

  begin
    if (i = args.index '--limit')
      args.delete_at i
      limit = args.delete_at i
      raise unless limit =~ /\d+/
      limit = limit.to_i
    end

    if (i = args.index '--part')
      args.delete_at i
      match = args.delete_at(i).match /(\d+)\/(\d+)/
      part = match[1].to_i
      parts_count = match[2].to_i
      raise if parts_count == 0 or part == 0 or part > parts_count
    end

    raise if args.count != 1
    words_file = args.first
  rescue
    exit_with_usage
  end

  words = []
  begin
    open(words_file) do |file|
      file.lines.each do |line|
        # pass blank lines
        next if line.strip.empty? or line.strip.start_with? '#'

        # split into two parts by first tab or double space
        parts = line.split( /\t| {2,}/, 2 ).map(&:strip)
        next if parts.count < 2

        words << Word.new( parts[1], parts[0], limit )
      end
    end
  rescue Exception => e
    exit_with_usage e.message
  end

  ###############################################################
  #                          PART WORDS
  ###############################################################

  total_words_count = words.count

  # words per part
  wpp = words.count / parts_count
  left = (part-1) * wpp
  right = part * wpp
  if part == parts_count
    # reminder to last part
    right = words.count
  end
  words = words[left...right]

  ###############################################################
  #                        WELCOME MESSAGE
  ###############################################################

  say %{ <%= color('             Learn Words !           ', GREEN+UNDERLINE) %>}
  say %{ <%= color('made by Vladimir Parfinenko aka cypok', GREEN) %>}
  say %{ <%= color(' some fixes by Ivan Novikov aka NIA  ', GREEN) %>}
  say %{ <%= color('             version 0.1.2           ', GREEN) %>}
  STDOUT.write "\n"
  say %{Note: if there are more than one variant,}
  say %{separate them by "/" or enter one by one}
  STDOUT.write "\n"
  say %{Type "?" to get stats, "!" to mark as learnt}
  say %{and "exit" to quit}
  STDOUT.write "\n"
  say %{<%= color('Current limit of learning is set to #{limit}', BLUE) %>}
  say %{<%= color('Total #{total_words_count} words to learn', BLUE) %>}
  if parts_count != 1
    say %{<%= color('Part #{part} of #{parts_count}: #{words.count} words to learn', BLUE) %>}
  end
  STDOUT.write "\n"

  ###############################################################
  #                          MAIN LOOP
  ###############################################################

  answers = []
  word = nil
  while true
    begin
      local_words = words.sort_by { rand }.find_all { |w| not w.learnt? }

      break if local_words.size == 0

      min_favour = local_words.map{ |x| x.favour}.min

      d = 0.25
      word_prev = word
      word = local_words.find { |x| x.favour <= (min_favour + d) }
      while word == word_prev && local_words.count > 1
        local_words = local_words.sort_by { rand }
        d += 0.25
        word = local_words.find { |x| x.favour <= (min_favour + d) }
      end

      variants_msg = word.variants_number == 1 ? "" : " (#{word.variants_number} variants)"
      say %{< <%= color(%q[#{word.orig}], YELLOW) %>#{variants_msg}}

      answers = []
      while answers.length < word.variants_number and not answers.include? "exit"
        STDOUT.write "> "
        answers += STDIN.readline.split( "/" ).map( &:strip )

        break if ['', 'exit', '!', '?'].any? {|s| answers.include? s }
      end
      break if answers.include? "exit"

      if answers.include? "!"
        STDOUT.write "Are you sure you learnt this? (yes/no) > "
        if STDIN.readline.strip == 'yes'
          # mark the word as learnt
          word.learn!
          say %{< #{word.times_answered}/#{word.times_asked}\t<%= color('Learnt!', BLUE) %>}
        else
          say %{< #{word.times_answered}/#{word.times_asked}}
        end
      elsif answers.include? "?"
        total_to_answer  = words.map {|w| w.limit }.reduce(&:+)
        already_answered = words.map {|w| w.times_answered }.reduce(&:+)

        say %{< Already answered #{already_answered} times correctly}
        say %{< There will be #{total_to_answer-already_answered} questions more}
      else
        if word.asked(answers)
          say %{< #{word.times_answered}/#{word.times_asked}\t<%= color('OK!', GREEN) %>}
        else
          verb = ( word.variants_number != 1 ) ? "were" : "was"
          right_strings = word.trans.map{|t| '"'+t+'"' }.join ", "
          say %{< #{word.times_answered}/#{word.times_asked}\t<%= color(%q[WRONG! Right #{verb} #{right_strings}!], BOLD+RED+UNDERLINE) %>}
        end
      end
      STDOUT.write "\n"
    rescue EOFError, Interrupt
      break
    end
  end

  ###############################################################
  #                          RESULTS
  ###############################################################

  STDOUT.write "\n"
  say %{<%= color('Your results:', GREEN) %>}
  words = words.sort do |x, y|
    if x.frequency == y.frequency
      x.trans <=> y.trans
    else
      -( x.frequency <=> y.frequency )
    end
  end
  max_trans_length = words.map {|w| w.trans.join('/').length }.max
  words.each do |word|
    freq = (word.frequency*100).to_i.to_s.rjust(3)
    trans = word.trans.join('/').ljust(max_trans_length, ' ')
    say %{<%= color(%q[#{freq}%  #{word.times_answered}/#{word.times_asked}   #{trans}  #{word.orig}], BLUE) %>}
  end
end