Class: Birdwatcher::WordList
- Inherits:
-
Object
- Object
- Birdwatcher::WordList
- Defined in:
- lib/birdwatcher/word_list.rb
Instance Attribute Summary collapse
-
#corpus ⇒ Object
readonly
Returns the value of attribute corpus.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#word_list ⇒ Object
readonly
Returns the value of attribute word_list.
Instance Method Summary collapse
- #add_to_corpus(text) ⇒ Object
-
#initialize(options) ⇒ WordList
constructor
A new instance of WordList.
- #process ⇒ Object
Constructor Details
#initialize(options) ⇒ WordList
Returns a new instance of WordList.
5 6 7 8 9 |
# File 'lib/birdwatcher/word_list.rb', line 5 def initialize() @options = @corpus = [] @word_list = {} end |
Instance Attribute Details
#corpus ⇒ Object (readonly)
Returns the value of attribute corpus.
3 4 5 |
# File 'lib/birdwatcher/word_list.rb', line 3 def corpus @corpus end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/birdwatcher/word_list.rb', line 3 def @options end |
#word_list ⇒ Object (readonly)
Returns the value of attribute word_list.
3 4 5 |
# File 'lib/birdwatcher/word_list.rb', line 3 def word_list @word_list end |
Instance Method Details
#add_to_corpus(text) ⇒ Object
11 12 13 |
# File 'lib/birdwatcher/word_list.rb', line 11 def add_to_corpus(text) @corpus << text.to_s end |
#process ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/birdwatcher/word_list.rb', line 15 def process words = {} corpus.each do |text| normalize_and_split(text).each do |word| next if exclude_word?(word) words.key?(word) ? words[word] += 1 : words[word] = 1 end end if [:min_word_count] words.delete_if { |word, count| count < [:min_word_count].to_i } end sorted_words = words.sort_by { |word, count| count }.reverse if [:word_cap] sorted_words = sorted_words.take([:word_cap].to_i) end @word_list = sorted_words end |