Class: Training

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

Instance Method Summary collapse

Constructor Details

#initialize(val_i) ⇒ Training

Returns a new instance of Training.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/shunkuntype/training.rb', line 4

def initialize(val_i)
  val_i ||= 47
  data_dir=File.expand_path('../../../lib/data', __FILE__)
  p base_name="STEP-#{val_i}.txt"
  file_name=File.join(data_dir,base_name)

  @period = 60
  @counter = 0

  @time_flag=true
  print_keyboard
  start_time,data = init_proc(file_name)
  return if start_time.nil?
  time_training(base_name,data,start_time)
end

Instance Method Details

#counter(word1, word2) ⇒ Object

count correct spelling



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shunkuntype/training.rb', line 25

def counter(word1,word2)
  ws1=word1.split(/\s+/)
  ws2=word2.split(/\s+/)
  i,j=0,0
  while (i < ws1.length) && (j < ws2.length) do
    if ws1[i] == ws2[j] then
      @counter+=1
    end
    i+=1
    j+=1
  end
  puts @counter
end

#init_proc(file_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shunkuntype/training.rb', line 48

def init_proc(file_name)
  data=File.readlines(file_name)
  data.each{|word| print word }
  print "\nRepeat above sentences. Type return-key to start."
  line=STDIN.gets
  if line.nil?
    # 入力がなければnilを返して呼び出し元で処理
    return nil, data
  end
  line = line.chomp
  start_time = Time.now
  return start_time, data
end

#keep_record(start_time, file_name, period) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/shunkuntype/training.rb', line 62

def keep_record(start_time, file_name, period)
  # テスト環境なら記録しない
  return if ENV['TEST'] == 'true'

  data_file = open(Shunkuntype::TRAINING_FILE, "a+")
  data_file << "#{start_time},#{file_name},#{@counter},#{period}\n"
  exit
end

#loop_thread(sec) ⇒ Object

time loop



40
41
42
43
44
45
46
# File 'lib/shunkuntype/training.rb', line 40

def loop_thread(sec)
  Thread.start(sec) do |wait_time|
    sleep wait_time
    puts "\a Time up. Type return-key to finish.."
    @time_flag=false
  end
end


20
21
22
# File 'lib/shunkuntype/training.rb', line 20

def print_keyboard
  Shunkuntype.print_keyboard
end

#time_training(base_name, data, start_time) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/shunkuntype/training.rb', line 82

def time_training(base_name,data,start_time)
  loop_thread(@period)
  data2=[]
  20.times{ data2 << data }
  type_loop(data2.flatten)
  keep_record(start_time,base_name,@period)
end

#type_loop(data) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/shunkuntype/training.rb', line 71

def type_loop(data)
  data.each do |sentence|
    break if @time_flag == false
    puts sentence
    line = STDIN.gets
    break if line.nil?
    line = line.chomp
    counter(sentence, line)
  end
end