Class: Raingrams::ProbabilityTable
- Defined in:
- lib/raingrams/probability_table.rb
Instance Attribute Summary collapse
-
#dirty ⇒ Object
readonly
Indicates wether the table needs to be rebuilt.
-
#frequencies ⇒ Object
readonly
Frequencies of grams.
-
#probabilities ⇒ Object
readonly
Probabilities of grams.
Instance Method Summary collapse
-
#build ⇒ Object
Builds the probability table using the recorded frequencies, if the table is marked as dirty.
-
#clear ⇒ Object
Clears the probability table.
-
#count(gram) ⇒ Object
Increments the frequency of the specified gram and marks the probability table as dirty.
-
#dirty? ⇒ Boolean
Returns
true
if the probability table is dirty and needs to be rebuilt, returnsfalse
otherwise. -
#each_gram(&block) ⇒ Object
Iterates over each gram in the probability table, passing each to the given block.
-
#empty? ⇒ Boolean
Returns
true
if the probability table is empty, returnsfalse
otherwise. -
#frequency_of(gram) ⇒ Object
Returns the frequency of the specified gram.
-
#grams ⇒ Object
Returns the grams within the probability table.
-
#has_gram?(gram) ⇒ Boolean
Returns
true
if the probability table contains the specified gram, returnsfalse
otherwise. -
#initialize ⇒ ProbabilityTable
constructor
Creates a new empty ProbabilityTable object.
- #inspect ⇒ Object
-
#probability_of(gram) ⇒ Object
(also: #[])
Returns the probability of the specified gram occurring.
-
#set_count(gram, value) ⇒ Object
Sets the frequency of the specified gram to the specified value.
-
#to_hash ⇒ Object
Returns a Hash representation of the probability table.
-
#total ⇒ Object
Calculates the total via the summation of the frequencies.
Constructor Details
#initialize ⇒ ProbabilityTable
Creates a new empty ProbabilityTable object.
16 17 18 19 20 21 |
# File 'lib/raingrams/probability_table.rb', line 16 def initialize @dirty = false @total = 0 @frequencies = {} @probabilities = {} end |
Instance Attribute Details
#dirty ⇒ Object (readonly)
Indicates wether the table needs to be rebuilt
5 6 7 |
# File 'lib/raingrams/probability_table.rb', line 5 def dirty @dirty end |
#frequencies ⇒ Object (readonly)
Frequencies of grams
8 9 10 |
# File 'lib/raingrams/probability_table.rb', line 8 def frequencies @frequencies end |
#probabilities ⇒ Object (readonly)
Probabilities of grams
11 12 13 |
# File 'lib/raingrams/probability_table.rb', line 11 def probabilities @probabilities end |
Instance Method Details
#build ⇒ Object
Builds the probability table using the recorded frequencies, if the table is marked as dirty.
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/raingrams/probability_table.rb', line 111 def build if @dirty current_total = total.to_f @frequencies.each do |gram,count| @probabilities[gram] = count.to_f / current_total end @dirty = false end return self end |
#clear ⇒ Object
Clears the probability table.
136 137 138 139 140 141 142 |
# File 'lib/raingrams/probability_table.rb', line 136 def clear @total = 0 @frequencies.clear @probabilities.clear return self end |
#count(gram) ⇒ Object
Increments the frequency of the specified gram and marks the probability table as dirty.
83 84 85 86 87 88 89 90 91 |
# File 'lib/raingrams/probability_table.rb', line 83 def count(gram) @dirty = true unless @frequencies.has_key?(gram) @frequencies[gram] = 0 end return @frequencies[gram] += 1 end |
#dirty? ⇒ Boolean
Returns true
if the probability table is dirty and needs to be rebuilt, returns false
otherwise.
27 28 29 |
# File 'lib/raingrams/probability_table.rb', line 27 def dirty? @dirty == true end |
#each_gram(&block) ⇒ Object
Iterates over each gram in the probability table, passing each to the given block.
50 51 52 |
# File 'lib/raingrams/probability_table.rb', line 50 def each_gram(&block) @frequencies.each_key(&block) end |
#empty? ⇒ Boolean
Returns true
if the probability table is empty, returns false
otherwise.
129 130 131 |
# File 'lib/raingrams/probability_table.rb', line 129 def empty? @total == 0 end |
#frequency_of(gram) ⇒ Object
Returns the frequency of the specified gram. Returns 0
by default.
57 58 59 |
# File 'lib/raingrams/probability_table.rb', line 57 def frequency_of(gram) @frequencies[gram] || 0 end |
#grams ⇒ Object
Returns the grams within the probability table.
42 43 44 |
# File 'lib/raingrams/probability_table.rb', line 42 def grams @frequencies.keys end |
#has_gram?(gram) ⇒ Boolean
Returns true
if the probability table contains the specified gram, returns false
otherwise.
35 36 37 |
# File 'lib/raingrams/probability_table.rb', line 35 def has_gram?(gram) @frequencies.has_key?(gram) end |
#inspect ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/raingrams/probability_table.rb', line 153 def inspect if @dirty "#<ProbabilityTable @total=#{@total} @frequencies=#{@frequencies.inspect}>" else @probabilities.inspect end end |
#probability_of(gram) ⇒ Object Also known as: []
Returns the probability of the specified gram occurring. Returns 0.0
by default.
65 66 67 |
# File 'lib/raingrams/probability_table.rb', line 65 def probability_of(gram) @probabilities[gram] || 0.0 end |
#set_count(gram, value) ⇒ Object
Sets the frequency of the specified gram to the specified value.
74 75 76 77 |
# File 'lib/raingrams/probability_table.rb', line 74 def set_count(gram,value) @dirty = true @frequencies[gram] = value end |
#to_hash ⇒ Object
Returns a Hash representation of the probability table.
147 148 149 150 151 |
# File 'lib/raingrams/probability_table.rb', line 147 def to_hash build return @probabilities end |
#total ⇒ Object
Calculates the total via the summation of the frequencies. Also marks the probability table as dirty.
97 98 99 100 101 102 103 104 105 |
# File 'lib/raingrams/probability_table.rb', line 97 def total if @dirty @total = @frequencies.values.inject do |sum,freq| sum + freq end end return @total end |