class Sastrawi::Dictionary::ArrayDictionary
Attributes
words[R]
Public Class Methods
new(words = [])
click to toggle source
# File lib/sastrawi/dictionary/array_dictionary.rb, line 6 def initialize(words = []) @words = [] add_words(words) end
Public Instance Methods
add(word)
click to toggle source
Add a word to the dictionary
# 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)
click to toggle source
Add multiple words to the dictionary
# 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)
click to toggle source
Add words from a text file to the dictionary
# 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)
click to toggle source
Check whether a word is contained in the dictionary
# File lib/sastrawi/dictionary/array_dictionary.rb, line 15 def contains?(word) @words.include?(word) end
count()
click to toggle source
Count how many words in the dictionary
# File lib/sastrawi/dictionary/array_dictionary.rb, line 22 def count @words.length end
remove(word)
click to toggle source
Remove a word from the dictionary
# File lib/sastrawi/dictionary/array_dictionary.rb, line 62 def remove(word) @words.delete(word) end