Class: Freql::Counter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens: {}, total: 0) ⇒ Counter

Returns a new instance of Counter.



11
12
13
14
# File 'lib/freql/counter.rb', line 11

def initialize tokens: {}, total: 0
  @tokens = tokens
  @total = total
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



9
10
11
# File 'lib/freql/counter.rb', line 9

def tokens
  @tokens
end

#totalObject (readonly)

Returns the value of attribute total.



8
9
10
# File 'lib/freql/counter.rb', line 8

def total
  @total
end

Instance Method Details

#add_array(source) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/freql/counter.rb', line 16

def add_array source
  @total += source.length
  source.each do |token|
    add_token(token)
  end
  self
end

#add_inflated_pairs(source, size = 2) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/freql/counter.rb', line 37

def add_inflated_pairs source, size = 2
  out = []
  for x in 0..(source.length-size) do
    out << source[x...x+size]
  end
  add_array(out)
end

#add_matches(source, pattern) ⇒ Object



33
34
35
# File 'lib/freql/counter.rb', line 33

def add_matches source, pattern
  add_array(source.scan(pattern))
end

#add_single_token(token) ⇒ Object



28
29
30
31
# File 'lib/freql/counter.rb', line 28

def add_single_token token
  @total += 1
  add_token(token)
end

#add_words(source) ⇒ Object



24
25
26
# File 'lib/freql/counter.rb', line 24

def add_words source
  add_matches(source, /\w+/)
end

#compute_bindataObject



55
56
57
# File 'lib/freql/counter.rb', line 55

def compute_bindata
  BinData.pack(tokens.transform_values {|count| CB.calc_cb(count,@total).abs.round})
end

#compute_cbObject



45
46
47
48
# File 'lib/freql/counter.rb', line 45

def compute_cb
  @total = @total.to_f
  tokens.transform_values {|count| CB.calc_cb(count,@total)}
end

#compute_zipfObject



50
51
52
53
# File 'lib/freql/counter.rb', line 50

def compute_zipf
  @total = @total.to_f
  tokens.transform_values {|count| ZipF.calc_zipf(count,@total)}
end