Class: Analtex::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/analtex/dictionary.rb

Instance Method Summary collapse

Constructor Details

#initializeDictionary

Returns a new instance of Dictionary.



3
4
5
6
# File 'lib/analtex/dictionary.rb', line 3

def initialize
  @word_counts = Hash.new { 0 }
  @word_indices = {}
end

Instance Method Details

#add_word(word) ⇒ Object



8
9
10
# File 'lib/analtex/dictionary.rb', line 8

def add_word(word)
  @word_counts[word] += 1
end

#add_words(words) ⇒ Object



12
13
14
15
16
# File 'lib/analtex/dictionary.rb', line 12

def add_words(words)
  words.each do |word|
    add_word(word)
  end
end

#prepare(min_occurrences = 1) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/analtex/dictionary.rb', line 26

def prepare(min_occurrences = 1)
  raise 'Do not run twice!' unless @word_indices.keys.empty?

  @word_counts.each_pair do |word, occurrences|
    if occurrences >= min_occurrences
      new_word_index = @word_indices.length
      @word_indices[word] = new_word_index
    end
  end
end

#word_countsObject



22
23
24
# File 'lib/analtex/dictionary.rb', line 22

def word_counts
  @word_counts
end

#wordsObject



18
19
20
# File 'lib/analtex/dictionary.rb', line 18

def words
  @word_indices
end