Class: VocaBuil::BaseCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/voca_buil/multi_check.rb,
lib/voca_buil/_stacks/_stacks_v2_260114/multi_check.rb

Direct Known Subclasses

AbbrevCheck::BaseCheck

Constant Summary collapse

CHECK_LOG_FILE =
'./check_log.yaml'
WORD_LOG_FILE =
'./word_log.yaml'
WORD_LOG_FILE_J2E =

追加

'./word_log_j2e.yaml'

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ BaseCheck

Returns a new instance of BaseCheck.



48
49
50
51
52
53
# File 'lib/voca_buil/multi_check.rb', line 48

def initialize(options)
  @options = options.dup
  @check_log_file = self.class::CHECK_LOG_FILE
  # reverseオプションでファイル名を切り替え
  @word_log_file = options[:reverse] ? self.class::WORD_LOG_FILE_J2E : self.class::WORD_LOG_FILE
end

Instance Method Details

#check_results(words_shuffled, answers_shuffled, user_inputs, w_num) ⇒ Object

w_num個だけ解答をチェックするように修正



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/voca_buil/multi_check.rb', line 98

def check_results(words_shuffled, answers_shuffled, user_inputs, w_num)
  results = {}
  [w_num, words_shuffled.size].min.times do |i|
    ans = words_shuffled[i]
    next if ans.nil?
    user_num = user_inputs[i].to_i
    print "%-2s" % user_num
    if user_num < answers_shuffled.size && 
       ans == words_shuffled[answers_shuffled[user_num][-1]]
      print "true:  ".green
      t_or_f = 't'
    else
      print "false: ".red
      t_or_f = 'f'
    end
    p ans
    results[ans[0]] = t_or_f
  end
  results
end

#get_user_inputs(words_shuffled, w_num) ⇒ Object

w_num個だけ問題を出すように修正



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/voca_buil/multi_check.rb', line 73

def get_user_inputs(words_shuffled, w_num)
  user_inputs = []
  words_shuffled.first(w_num).each_with_index do |ew, idx|
    break if ew.nil?
    question = "Q#{idx+1}: '#{ew[0]}' ? -> "
    if ENV['SIMPLE_CHECK_TEST']
      puts question
      user_inputs << "0"
    else
      # reverseモードかつquestionが標準ターミナル幅(80-10)を超える場合はputsで表示しaskは空文字
      if @options[:reverse] && question.length > 70
        puts question
        input = Thor::Shell::Basic.new.ask ""
      else
        input = Thor::Shell::Basic.new.ask question
      end
      # 入力値を半角に変換
      user_input = input ? input.chomp.unicode_normalize(:nfkc) : ""
      user_inputs << user_input
    end
  end
  user_inputs
end

#init_word_log(words) ⇒ Object



157
158
159
160
161
# File 'lib/voca_buil/multi_check.rb', line 157

def init_word_log(words)
  word_log = {}
  words.each { |word| word_log[word[0]] = nil }
  word_log
end

#make_answers(words_shuffled) ⇒ Object



58
59
60
# File 'lib/voca_buil/multi_check.rb', line 58

def make_answers(words_shuffled)
  words_shuffled.each_with_index.map { |word, idx| [word[-1], idx] }
end


66
67
68
69
70
# File 'lib/voca_buil/multi_check.rb', line 66

def print_answers(answers_shuffled)
  answers_shuffled.each_with_index do |word, idx|
    puts "#{idx}: #{word[0]}" #, #{word[-1]}"
  end
end

#runObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/voca_buil/multi_check.rb', line 163

def run
  selector = WordsSelector.new(@word_log_file, @options)
  words_shuffled = selector.select(@options[:w_num], @options[:a_num])
  answers = make_answers(words_shuffled)
  answers_shuffled = shuffle_answers(answers, @options[:a_num])
  print_answers(answers_shuffled)
  user_inputs = get_user_inputs(words_shuffled, @options[:w_num])
  results = check_results(words_shuffled, answers_shuffled, user_inputs, @options[:w_num])
  update_logs(results)
  results
end

#select_least_correct_words(whole_words) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/voca_buil/multi_check.rb', line 144

def select_least_correct_words(whole_words)
  return unless File.exist?(@word_log_file)
  word_log = YAML.load_file(@word_log_file)
  # 対象words数が100未満ならその数、100以上なら100
  limit = [whole_words.size, 100].min
  least_words = word_log.sort_by { |k, v| v&.count('t') || 0 }.first(limit)
  selected_words = []
  least_words.each do |sel_word, _|
    whole_words.each { |word| selected_words << word if word[0] == sel_word }
  end
  selected_words
end

#shuffle_answers(answers, a_num) ⇒ Object



62
63
64
# File 'lib/voca_buil/multi_check.rb', line 62

def shuffle_answers(answers, a_num)
  answers.shuffle[0..a_num]
end

#shuffle_words(words, w_num) ⇒ Object



54
55
56
# File 'lib/voca_buil/multi_check.rb', line 54

def shuffle_words(words, w_num)
  words.shuffle.first(w_num)
end

#update_logs(results) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/voca_buil/multi_check.rb', line 119

def update_logs(results)
  cmd_opts = @options[:cmd_line] || ""
  correct = results.values.count('t')
  total = results.size
  log_line = [
    Time.now.strftime('%Y-%m-%d %H:%M:%S'),
    cmd_opts,
    "#{correct}/#{total}"
  ]

  # check_log.yamlの更新
  log_file = @check_log_file
  logs = File.exist?(log_file) ? YAML.load_file(log_file) : []
  logs << log_line
  File.write(log_file, YAML.dump(logs))

  # word_log.yamlの更新
  word_log = File.exist?(@word_log_file) ? YAML.load_file(@word_log_file) : {}
  results.each do |word, tf|
    word_log[word] ||= ""
    word_log[word] += tf
  end
  File.write(@word_log_file, YAML.dump(word_log))
end