Class: Raingrams::UnigramModel

Inherits:
Model show all
Defined in:
lib/raingrams/unigram_model.rb

Direct Known Subclasses

OpenVocabulary::UnigramModel

Instance Attribute Summary

Attributes inherited from Model

#convert_abbrev, #convert_acronyms, #frequency, #ignore_case, #ignore_phone_numbers, #ignore_punc, #ignore_references, #ignore_urls, #ngram_size, #probability

Instance Method Summary collapse

Methods inherited from Model

#clear, #each_ngram, #has_ngram?, #ngrams, #ngrams_ending_with, #ngrams_starting_with, #ngrams_with, #parse_sentence, #parse_text, #probabilities_for, #probability_of_gram, #probability_of_ngram, #probability_of_ngrams, #train_with_ngram, #train_with_ngrams, #vocabulary, #within_vocabulary?

Constructor Details

#initialize(opts = {}, &block) ⇒ UnigramModel

Returns a new instance of UnigramModel.



6
7
8
9
10
# File 'lib/raingrams/unigram_model.rb', line 6

def initialize(opts={},&block)
  opts[:ngram_size] = 1

  super(opts) { |model| model.build(&block) }
end

Instance Method Details

#build(&block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/raingrams/unigram_model.rb', line 44

def build(&block)
  clear_probabilities

  block.call(self) if block

  total_count = gram_count.to_f
  @frequency.each do |ngram,count|
    @probability[ngram] = count.to_f / total_count
  end

  return self
end

#fragment_probability(fragment) ⇒ Object



57
58
59
# File 'lib/raingrams/unigram_model.rb', line 57

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

#gram_countObject



38
39
40
41
42
# File 'lib/raingrams/unigram_model.rb', line 38

def gram_count
  @frequency.values.inject do |sum,count|
    sum + count
  end
end

#ngrams_from_fragment(fragment) ⇒ Object



16
17
18
# File 'lib/raingrams/unigram_model.rb', line 16

def ngrams_from_fragment(fragment)
  ngrams_from_words(parse_sentence(fragment))
end

#ngrams_from_sentence(sentence) ⇒ Object



20
21
22
# File 'lib/raingrams/unigram_model.rb', line 20

def ngrams_from_sentence(sentence)
  ngrams_from_fragment(sentence)
end

#ngrams_from_text(text) ⇒ Object



24
25
26
27
28
# File 'lib/raingrams/unigram_model.rb', line 24

def ngrams_from_text(text)
  parse_text(text).inject([]) do |ngrams,sentence|
    ngrams + ngrams_from_sentence(sentence)
  end
end

#ngrams_from_words(words) ⇒ Object



12
13
14
# File 'lib/raingrams/unigram_model.rb', line 12

def ngrams_from_words(words)
  words.map { |word| Ngram[word] }
end

#sentence_probability(sentence) ⇒ Object



61
62
63
# File 'lib/raingrams/unigram_model.rb', line 61

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

#text_probability(text) ⇒ Object



65
66
67
# File 'lib/raingrams/unigram_model.rb', line 65

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

#train_with_sentence(sentence) ⇒ Object



30
31
32
# File 'lib/raingrams/unigram_model.rb', line 30

def train_with_sentence(sentence)
  train_with_ngrams(ngrams_from_sentence(sentence))
end

#train_with_text(text) ⇒ Object



34
35
36
# File 'lib/raingrams/unigram_model.rb', line 34

def train_with_text(text)
  train_with_ngrams(ngrams_from_text(text))
end