Class: Rundown::Processors::Frequency
- Inherits:
-
Rundown::Processor
- Object
- Rundown::Processor
- Rundown::Processors::Frequency
- Defined in:
- lib/rundown/processors/frequency.rb
Constant Summary
Constants inherited from Rundown::Processor
Rundown::Processor::PUNCTUATION
Instance Attribute Summary collapse
-
#length ⇒ Object
Returns the value of attribute length.
Attributes inherited from Rundown::Processor
Instance Method Summary collapse
- #cleanup_words! ⇒ Object
-
#initialize(words, length = 1) ⇒ Frequency
constructor
A new instance of Frequency.
- #process ⇒ Object
Methods inherited from Rundown::Processor
Constructor Details
#initialize(words, length = 1) ⇒ Frequency
Returns a new instance of Frequency.
6 7 8 9 |
# File 'lib/rundown/processors/frequency.rb', line 6 def initialize(words, length=1) super(words) @length = length end |
Instance Attribute Details
#length ⇒ Object
Returns the value of attribute length.
4 5 6 |
# File 'lib/rundown/processors/frequency.rb', line 4 def length @length end |
Instance Method Details
#cleanup_words! ⇒ Object
11 12 13 |
# File 'lib/rundown/processors/frequency.rb', line 11 def cleanup_words! @words = words.map {|w| w.gsub(/[^\w]/, '').downcase }.reject {|w| w.strip == '' } end |
#process ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rundown/processors/frequency.rb', line 15 def process cleanup_words! frequencies = {} words.length.times do phrase = words[0..length-1] if phrase.join('').length >= 1 frequencies[phrase] ||= 0 frequencies[phrase] += 1 end @words = words.rotate end totals = {} frequencies.each do |phrase, freq| totals[freq] ||= [] totals[freq] << phrase end totals end |