Class: VocabCounter

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

Instance Method Summary collapse

Constructor Details

#initializeVocabCounter

Returns a new instance of VocabCounter.



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

def initialize
  @count = Hash.new(0)
end

Instance Method Details

#count(line) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/vocab_counter.rb', line 7

def count(line)
  line.downcase!
  line.gsub!(/[^a-z0-9']/, ' ')
  line.gsub!(/\s'|'\s/, ' ')
  line.split(/\s+/).each do |term|
    @count[term.to_sym] += 1
  end
end

#reportObject



16
17
18
19
20
21
22
# File 'lib/vocab_counter.rb', line 16

def report
  out = ""
  @count.sort { |a,b| b[1] <=> a[1] }.each do |k,v|
    out << "#{k.to_s},#{v}\n"
  end
  out
end