Class: Birdwatcher::WordList

Inherits:
Object
  • Object
show all
Defined in:
lib/birdwatcher/word_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  @options   = options
  @corpus    = []
  @word_list = {}
end

Instance Attribute Details

#corpusObject (readonly)

Returns the value of attribute corpus.



3
4
5
# File 'lib/birdwatcher/word_list.rb', line 3

def corpus
  @corpus
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/birdwatcher/word_list.rb', line 3

def options
  @options
end

#word_listObject (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

#processObject



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 options[:min_word_count]
    words.delete_if { |word, count| count < options[:min_word_count].to_i }
  end
  sorted_words = words.sort_by { |word, count| count }.reverse
  if options[:word_cap]
    sorted_words = sorted_words.take(options[:word_cap].to_i)
  end
  @word_list = sorted_words
end