Module: Raingrams::Helpers::Probability

Included in:
Model
Defined in:
lib/raingrams/helpers/probability.rb

Instance Method Summary collapse

Instance Method Details

#fragment_probability(fragment) ⇒ Object

Returns the probability of the specified fragment occuring within arbitrary text.



46
47
48
# File 'lib/raingrams/helpers/probability.rb', line 46

def fragment_probability(fragment)
  probability_of_ngrams(ngrams_from_fragment(fragment))
end

#probabilities_for(ngrams) ⇒ Object

Returns the probability of the specified ngrams occurring within arbitrary text.



22
23
24
25
26
27
28
29
30
# File 'lib/raingrams/helpers/probability.rb', line 22

def probabilities_for(ngrams)
  table = {}

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

  return table
end

#probability_of_ngram(ngram) ⇒ Object

Returns the probability of the specified ngram occurring within arbitrary text.



8
9
10
11
12
13
14
15
16
# File 'lib/raingrams/helpers/probability.rb', line 8

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

#probability_of_ngrams(ngrams) ⇒ Object

Returns the joint probability of the specified ngrams occurring within arbitrary text.



36
37
38
39
40
# File 'lib/raingrams/helpers/probability.rb', line 36

def probability_of_ngrams(ngrams)
  probabilities_for(ngrams).values.inject do |joint,prob|
    joint * prob
  end
end

#sentence_probability(sentence) ⇒ Object

Returns the probability of the specified sentence occuring within arbitrary text.



54
55
56
# File 'lib/raingrams/helpers/probability.rb', line 54

def sentence_probability(sentence)
  probability_of_ngrams(ngrams_from_sentence(sentence))
end

#text_probability(text) ⇒ Object

Returns the probability of the specified text occuring within arbitrary text.



62
63
64
# File 'lib/raingrams/helpers/probability.rb', line 62

def text_probability(text)
  probability_of_ngrams(ngrams_from_text(text))
end