Class: Sastrawi::Dictionary::ArrayDictionary
- Inherits:
-
Object
- Object
- Sastrawi::Dictionary::ArrayDictionary
- Defined in:
- lib/sastrawi/dictionary/array_dictionary.rb
Instance Method Summary collapse
-
#add(word) ⇒ Object
Add a word to the dictionary.
-
#add_words(new_words) ⇒ Object
Add multiple words to the dictionary.
-
#add_words_from_text_file(file_path) ⇒ Object
Add words from a text file to the dictionary.
-
#contains?(word) ⇒ Boolean
Check whether a word is contained in the dictionary.
-
#count ⇒ Object
Count how many words in the dictionary.
-
#initialize(words = []) ⇒ ArrayDictionary
constructor
A new instance of ArrayDictionary.
-
#remove(word) ⇒ Object
Remove a word from the dictionary.
-
#words ⇒ Object
Return the words as an Array (for backward compatibility).
Constructor Details
#initialize(words = []) ⇒ ArrayDictionary
Returns a new instance of ArrayDictionary.
8 9 10 11 12 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 8 def initialize(words = []) @words = Set.new add_words(words) end |
Instance Method Details
#add(word) ⇒ Object
Add a word to the dictionary
47 48 49 50 51 52 53 54 55 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 47 def add(word) unless word.is_a?(String) raise ArgumentError, "dictionary entries must be strings, got #{word.class}" end return if word.strip == '' @words.add(word) end |
#add_words(new_words) ⇒ Object
Add multiple words to the dictionary
38 39 40 41 42 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 38 def add_words(new_words) new_words.each do |word| add(word) end end |
#add_words_from_text_file(file_path) ⇒ Object
Add words from a text file to the dictionary
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 60 def add_words_from_text_file(file_path) words = [] File.open(file_path, 'r') do |file| file.each do |line| words.push(line.chomp) end end add_words(words) end |
#contains?(word) ⇒ Boolean
Check whether a word is contained in the dictionary
24 25 26 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 24 def contains?(word) @words.include?(word) end |
#count ⇒ Object
Count how many words in the dictionary
31 32 33 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 31 def count @words.size end |
#remove(word) ⇒ Object
Remove a word from the dictionary
75 76 77 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 75 def remove(word) @words.delete(word) end |
#words ⇒ Object
Return the words as an Array (for backward compatibility)
17 18 19 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 17 def words @words.to_a end |