Sha256: 3139612fd87ac7710020a35bba8a89d805026512f7ec734784446c1ef7f95972

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Raingrams
  module Helpers
    module Probability
      #
      # Returns the probability of the specified _ngram_ occurring within
      # arbitrary text.
      #
      def probability_of_ngram(ngram)
        prefix = ngram.prefix

        if @prefixes.has_key?(prefix)
          return @prefixes[prefix].probability_of(ngram.last)
        else
          return 0.0
        end
      end

      #
      # Returns the probability of the specified _ngrams_ occurring within
      # arbitrary text.
      #
      def probabilities_for(ngrams)
        table = {}

        ngrams.each do |ngram|
          table[ngram] = probability_of_ngram(ngram)
        end

        return table
      end

      #
      # Returns the joint probability of the specified _ngrams_ occurring
      # within arbitrary text.
      #
      def probability_of_ngrams(ngrams)
        probabilities_for(ngrams).values.inject do |joint,prob|
          joint * prob
        end
      end

      #
      # Returns the probability of the specified _fragment_ occuring within
      # arbitrary text.
      #
      def fragment_probability(fragment)
        probability_of_ngrams(ngrams_from_fragment(fragment))
      end

      #
      # Returns the probability of the specified _sentence_ occuring within
      # arbitrary text.
      #
      def sentence_probability(sentence)
        probability_of_ngrams(ngrams_from_sentence(sentence))
      end

      #
      # Returns the probability of the specified _text_ occuring within
      # arbitrary text.
      #
      def text_probability(text)
        probability_of_ngrams(ngrams_from_text(text))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
raingrams-0.1.2 lib/raingrams/helpers/probability.rb