Class: Sastrawi::Dictionary::ArrayDictionary
- Inherits:
-
Object
- Object
- Sastrawi::Dictionary::ArrayDictionary
- Defined in:
- lib/sastrawi/dictionary/array_dictionary.rb
Instance Attribute Summary collapse
-
#words ⇒ Object
readonly
Returns the value of attribute words.
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.
Constructor Details
#initialize(words = []) ⇒ ArrayDictionary
Returns a new instance of ArrayDictionary.
6 7 8 9 10 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 6 def initialize(words = []) @words = [] add_words(words) end |
Instance Attribute Details
#words ⇒ Object (readonly)
Returns the value of attribute words.
4 5 6 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 4 def words @words end |
Instance Method Details
#add(word) ⇒ Object
Add a word to the dictionary
38 39 40 41 42 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 38 def add(word) return if word.nil? || word.strip == '' @words.push(word) end |
#add_words(new_words) ⇒ Object
Add multiple words to the dictionary
29 30 31 32 33 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 29 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
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 47 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
15 16 17 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 15 def contains?(word) @words.include?(word) end |
#count ⇒ Object
Count how many words in the dictionary
22 23 24 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 22 def count @words.length end |
#remove(word) ⇒ Object
Remove a word from the dictionary
62 63 64 |
# File 'lib/sastrawi/dictionary/array_dictionary.rb', line 62 def remove(word) @words.delete(word) end |