Class: Analtex::Dictionary
- Inherits:
-
Object
- Object
- Analtex::Dictionary
- Defined in:
- lib/analtex/dictionary.rb
Instance Method Summary collapse
- #add_word(word) ⇒ Object
- #add_words(words) ⇒ Object
-
#initialize ⇒ Dictionary
constructor
A new instance of Dictionary.
- #prepare(min_occurrences = 1) ⇒ Object
- #word_counts ⇒ Object
- #words ⇒ Object
Constructor Details
#initialize ⇒ Dictionary
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_counts ⇒ Object
22 23 24 |
# File 'lib/analtex/dictionary.rb', line 22 def word_counts @word_counts end |
#words ⇒ Object
18 19 20 |
# File 'lib/analtex/dictionary.rb', line 18 def words @word_indices end |