Class: Raingrams::ProbabilityTable

Inherits:
Object
  • Object
show all
Defined in:
lib/raingrams/probability_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProbabilityTable

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

#dirtyObject (readonly)

Indicates wether the table needs to be rebuilt



5
6
7
# File 'lib/raingrams/probability_table.rb', line 5

def dirty
  @dirty
end

#frequenciesObject (readonly)

Frequencies of grams



8
9
10
# File 'lib/raingrams/probability_table.rb', line 8

def frequencies
  @frequencies
end

#probabilitiesObject (readonly)

Probabilities of grams



11
12
13
# File 'lib/raingrams/probability_table.rb', line 11

def probabilities
  @probabilities
end

Instance Method Details

#buildObject

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

#clearObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

#gramsObject

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.

Returns:

  • (Boolean)


35
36
37
# File 'lib/raingrams/probability_table.rb', line 35

def has_gram?(gram)
  @frequencies.has_key?(gram)
end

#inspectObject



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_hashObject

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

#totalObject

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