Class: Entropic::NGramCounter
- Inherits:
-
Object
- Object
- Entropic::NGramCounter
- Defined in:
- lib/entropic.rb
Overview
Public: a counter for ngrams
Instance Attribute Summary collapse
-
#counts ⇒ Object
Returns the value of attribute counts.
-
#size ⇒ Object
Returns the value of attribute size.
-
#total ⇒ Object
Returns the value of attribute total.
Instance Method Summary collapse
-
#count(ngram, if_not_found) ⇒ Object
Public: get count for string, with default.
-
#initialize(size) ⇒ NGramCounter
constructor
A new instance of NGramCounter.
-
#update(string) ⇒ Object
Public: update a counter with a string, with a multiplier of 1.
-
#update_with_multiplier(string, multiplier) ⇒ Object
Public: update a counter with a string, and a multiplier.
Constructor Details
#initialize(size) ⇒ NGramCounter
Returns a new instance of NGramCounter.
29 30 31 32 33 |
# File 'lib/entropic.rb', line 29 def initialize(size) @size = size @counts = Hash.new(0) @total = 0 end |
Instance Attribute Details
#counts ⇒ Object
Returns the value of attribute counts.
28 29 30 |
# File 'lib/entropic.rb', line 28 def counts @counts end |
#size ⇒ Object
Returns the value of attribute size.
28 29 30 |
# File 'lib/entropic.rb', line 28 def size @size end |
#total ⇒ Object
Returns the value of attribute total.
28 29 30 |
# File 'lib/entropic.rb', line 28 def total @total end |
Instance Method Details
#count(ngram, if_not_found) ⇒ Object
Public: get count for string, with default
Examples
counter = NGramCounter.new(2) counter.update(‘01234’) counter.count(‘01’, 0) #=> 1 counter.count(‘bob, 0) #=> 0
ngram: The String to check if_not_found : what to update with
79 80 81 |
# File 'lib/entropic.rb', line 79 def count(ngram, if_not_found) @counts.fetch(ngram, if_not_found) end |
#update(string) ⇒ Object
Public: update a counter with a string, with a multiplier of 1
Examples
counter = NGramCounter.new(2) counter.update(‘01234’)
string: The String to update with
61 62 63 |
# File 'lib/entropic.rb', line 61 def update(string) update_with_multiplier(string, 1) end |
#update_with_multiplier(string, multiplier) ⇒ Object
Public: update a counter with a string, and a multiplier
Examples
counter = NGramCounter.new(2) counter.update_with_multiplier(‘01234’, 1)
string: The String to update with multiplier: The Integer describing how much weight (will often be 1)
45 46 47 48 49 50 |
# File 'lib/entropic.rb', line 45 def update_with_multiplier(string, multiplier) Entropic.sliding(string, @size).each do |ngram| @counts[ngram] += multiplier @total += multiplier end end |