All pastes #2004689 Raw Edit

Someone

public text v1 · immutable
#2004689 ·published 2010-11-28 17:58 UTC
rendered paste body
require 'pp'class Word  attr_reader :lang  def initialize(str)    @lang = str.strip.split(/\t/)    @tries = 0    @success = 0  end  def score    if @tries == 0      1    else      fails = @tries - @success      # make sure we keep all words in      if fails == 0        fails += 1      end      fails.to_f / @tries    end  end  def record(success)    @tries += 1    @success += 1 if success  end  def to_s    "#{@lang[0]}/#{@lang[1]} #{score} (#@success/#@tries)"  endendclass Trainer  def initialize(file)    @words = []    File.readlines(file).each do |l|      @words << Word.new(l)    end    @maxactive = 10    @choices = 4  end  def ask    # first pick list of eligible words    eligible = []    total = 0    @words.each do |w|      eligible << w      total += w.score      break if total > @maxactive    end    # pick a random word    pickscore = Random.rand(total)    pick = nil    eligible.each do |w|      pickscore -= w.score      if pickscore < 0        pick = w        break      end    end    # pick other choices    choices = [pick]    choices += (eligible - [pick]).shuffle[0, @choices - 1]    # shuffle answers    choices.shuffle!    # display question    ask_lang = 0    choice_lang = 1    puts "What does '#{pick.lang[ask_lang]}' mean?"    choices.each_with_index do |c, i|      puts "  #{i + 1}. #{c.lang[choice_lang]}"    end    ans = nil    begin      puts "Your answer?"      ans = $stdin.readline.strip      if 'quit'.start_with?(ans)        return false      end    end while not (1..@choices).include? ans.to_i    # our array is 0-based    ans = ans.to_i - 1    if choices[ans].lang[choice_lang] == pick.lang[choice_lang]      puts "You are correct!"      pick.record(true)    else      puts "You made a mistake.  #{pick.lang[ask_lang]} means #{pick.lang[choice_lang]}."      pick.record(false)    end    true  end  def train    while ask      # well, ask    end  endendif $0 == __FILE__  t = Trainer.new(ARGV[0])  t.trainend