Class Raingrams::ProbabilityTable
In: lib/raingrams/probability_table.rb
Parent: Object
Model TrigramModel BigramModel QuadgramModel PentagramModel HexagramModel Model TrigramModel BigramModel QuadgramModel PentagramModel HexagramModel RuntimeError PrefixFrequencyMissing Set NgramSet Array Ngram Token StopSentence StartSentence Unknown ProbabilityTable Tokens Commonality Random Similarity Frequency Probability Helpers OpenModel OpenVocabulary Raingrams dot/f_1.png

Methods

[]   build   clear   count   dirty?   each_gram   empty?   frequency_of   grams   has_gram?   inspect   new   probability_of   set_count   to_hash   total  

Attributes

dirty  [R]  Indicates wether the table needs to be rebuilt
frequencies  [R]  Frequencies of grams
probabilities  [R]  Probabilities of grams

Public Class methods

Creates a new empty ProbabilityTable object.

[Source]

# File lib/raingrams/probability_table.rb, line 16
    def initialize
      @dirty = false
      @total = 0
      @frequencies = {}
      @probabilities = {}
    end

Public Instance methods

[](gram)

Alias for probability_of

Builds the probability table using the recorded frequencies, if the table is marked as dirty.

[Source]

# 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

Clears the probability table.

[Source]

# File lib/raingrams/probability_table.rb, line 136
    def clear
      @total = 0
      @frequencies.clear
      @probabilities.clear

      return self
    end

Increments the frequency of the specified gram and marks the probability table as dirty.

[Source]

# 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

Returns true if the probability table is dirty and needs to be rebuilt, returns false otherwise.

[Source]

# File lib/raingrams/probability_table.rb, line 27
    def dirty?
      @dirty == true
    end

Iterates over each gram in the probability table, passing each to the given block.

[Source]

# File lib/raingrams/probability_table.rb, line 50
    def each_gram(&block)
      @frequencies.each_key(&block)
    end

Returns true if the probability table is empty, returns false otherwise.

[Source]

# File lib/raingrams/probability_table.rb, line 129
    def empty?
      @total == 0
    end

Returns the frequency of the specified gram. Returns +0+ by default.

[Source]

# File lib/raingrams/probability_table.rb, line 57
    def frequency_of(gram)
      @frequencies[gram] || 0
    end

Returns the grams within the probability table.

[Source]

# File lib/raingrams/probability_table.rb, line 42
    def grams
      @frequencies.keys
    end

Returns true if the probability table contains the specified gram, returns false otherwise.

[Source]

# File lib/raingrams/probability_table.rb, line 35
    def has_gram?(gram)
      @frequencies.has_key?(gram)
    end

[Source]

# File lib/raingrams/probability_table.rb, line 153
    def inspect
      if @dirty
        "#<ProbabilityTable @total=#{@total} @frequencies=#{@frequencies.inspect}>"
      else
        @probabilities.inspect
      end
    end

Returns the probability of the specified gram occurring. Returns 0.0 by default.

[Source]

# File lib/raingrams/probability_table.rb, line 65
    def probability_of(gram)
      @probabilities[gram] || 0.0
    end

Sets the frequency of the specified gram to the specified value.

[Source]

# File lib/raingrams/probability_table.rb, line 74
    def set_count(gram,value)
      @dirty = true
      @frequencies[gram] = value
    end

Returns a Hash representation of the probability table.

[Source]

# File lib/raingrams/probability_table.rb, line 147
    def to_hash
      build

      return @probabilities
    end

Calculates the total via the summation of the frequencies. Also marks the probability table as dirty.

[Source]

# 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

[Validate]