class Syllables

Public Class Methods

new(text) click to toggle source
Calls superclass method
# File lib/syllables.rb, line 10
def initialize(text)
  super()
  @words = text.gsub(/\W+/, ' ').split\
    .inject({}) {|r,word| r.merge({word.to_sym => count_syllables(word)})}
end

Public Instance Methods

to_h() click to toggle source
# File lib/syllables.rb, line 16
def to_h
  @words
end

Private Instance Methods

count_syllables(word) click to toggle source
# File lib/syllables.rb, line 22
def count_syllables(word)

  len = 0

  ending_matched = word[/(ing|io|yer)$/]
  if ending_matched then
    len += 1
    word = word[0..-(ending_matched.length)]
  end

  got = word.scan(Tokenizer)
  len += got.size()

  if got.size() > 1 and got[-1] == ['e'] and
      word[-1].chr() == 'e' and
      word[-2].chr() != 'l' then
    len -= 1
  end

  return len

end