Class: Teletype::Stats

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

Overview

Stats keep track of hit/miss rate for each key and suggests a key that needs more practice.

Defined Under Namespace

Classes: Pair

Instance Method Summary collapse

Constructor Details

#initializeStats

Returns a new instance of Stats.



6
7
8
9
10
# File 'lib/teletype/stats.rb', line 6

def initialize
  @previous = nil
  @pairs = {}
  load
end

Instance Method Details

#fileObject



25
26
27
# File 'lib/teletype/stats.rb', line 25

def file
  File.join(Dir.home, '.teletype-stats')
end

#hit!(key) ⇒ Object



29
30
31
# File 'lib/teletype/stats.rb', line 29

def hit!(key)
  lookup(key)&.hit!
end

#loadObject



12
13
14
15
16
17
18
19
# File 'lib/teletype/stats.rb', line 12

def load
  return unless File.exist?(file)

  File.readlines(file).each do |line|
    keys, hit, miss = line.split("\t")
    @pairs[keys] = Pair.new(keys, hit: Integer(hit), miss: Integer(miss))
  end
end

#lookup(key) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/teletype/stats.rb', line 37

def lookup(key)
  current = key.downcase
  keys = "#{@previous}#{current}"
  @previous = current
  return if keys.strip.length < 2

  @pairs[keys] ||= Pair.new(keys)
end

#miss!(key) ⇒ Object



33
34
35
# File 'lib/teletype/stats.rb', line 33

def miss!(key)
  lookup(key)&.miss!
end

#rankingsObject



50
51
52
# File 'lib/teletype/stats.rb', line 50

def rankings
  @pairs.values.sort.first(10).map(&:to_s).join(' ')
end

#saveObject



21
22
23
# File 'lib/teletype/stats.rb', line 21

def save
  File.write(file, @pairs.map {|k, p| [k, p.hit, p.miss].join("\t")}.join("\n"))
end

#suggestionsObject



46
47
48
# File 'lib/teletype/stats.rb', line 46

def suggestions
  @pairs.values.select(&:inefficient?).sort.map(&:keys)
end