class Syllabize::Counter
Constants
- CONSONANTS
- DIPHTHONGS
- LE_VOWEL_SOUND
- RE_VOWEL
- SUFFIXES
- VOWELS
- Y_AS_VOWEL
Attributes
exceptions_file[RW]
str[RW]
Public Class Methods
new(str)
click to toggle source
# File lib/syllabize.rb, line 9 def initialize(str) @str = strip_punctuation(str) handle_non_string_input load_exceptions @syllables = 0 end
Public Instance Methods
count_syllables()
click to toggle source
# File lib/syllabize.rb, line 24 def count_syllables @str = str.to_i.to_words if is_int_in_string_form? return break_into_words if str.split(' ').length > 1 return handle_exceptions if exceptions.keys.include?(str) count_suffixes if str.scan(SUFFIXES).any? @syllables += count_vowels handle_additions handle_subtractions @syllables <= 1 ? 1 : @syllables end
Private Instance Methods
__dir__()
click to toggle source
for Ruby 1.9
# File lib/syllabize.rb, line 149 def __dir__ File.dirname(__FILE__) end
begins_with_re_vowel?()
click to toggle source
# File lib/syllabize.rb, line 112 def begins_with_re_vowel? str.scan(RE_VOWEL).any? end
break_into_words()
click to toggle source
# File lib/syllabize.rb, line 46 def break_into_words str.split(' ').collect(&:count_syllables).reduce(:+) end
contains_diphthongs?()
click to toggle source
# File lib/syllabize.rb, line 116 def contains_diphthongs? str.downcase.scan(DIPHTHONGS).any? end
contains_le_vowel_sound?()
click to toggle source
# File lib/syllabize.rb, line 100 def contains_le_vowel_sound? str.scan(LE_VOWEL_SOUND).any? end
contains_non_initial_y?()
click to toggle source
# File lib/syllabize.rb, line 104 def contains_non_initial_y? count_ys_in_vowel_role > 0 end
count_diphthongs()
click to toggle source
# File lib/syllabize.rb, line 120 def count_diphthongs str.downcase.scan(DIPHTHONGS).count end
count_suffixes()
click to toggle source
# File lib/syllabize.rb, line 37 def count_suffixes @syllables -= 1 if ends_in_non_syllablic_ed? while str.scan(SUFFIXES).any? suffix = str.scan(SUFFIXES).flatten.last str.sub! /(?<=.)#{suffix}/, '' @syllables += 1 end end
count_vowels()
click to toggle source
# File lib/syllabize.rb, line 92 def count_vowels str.scan(VOWELS).count end
count_ys_in_vowel_role()
click to toggle source
# File lib/syllabize.rb, line 108 def count_ys_in_vowel_role str.scan(Y_AS_VOWEL).size end
ending_ed_is_not_syllablic?()
click to toggle source
# File lib/syllabize.rb, line 136 def ending_ed_is_not_syllablic? str.scan(/(t|d|tr|dr|tl|dl)ed\b/).empty? end
ends_in_ed?()
click to toggle source
# File lib/syllabize.rb, line 132 def ends_in_ed? str.end_with?('ed') end
ends_in_non_syllablic_ed?()
click to toggle source
# File lib/syllabize.rb, line 140 def ends_in_non_syllablic_ed? if has_more_than_one_syllable? && ends_in_ed? && ending_ed_is_not_syllablic? true else false end end
ends_in_silent_e?()
click to toggle source
# File lib/syllabize.rb, line 96 def ends_in_silent_e? str.end_with?('e') end
ends_in_sm?()
click to toggle source
# File lib/syllabize.rb, line 124 def ends_in_sm? str.end_with?('sm') end
exceptions()
click to toggle source
# File lib/syllabize.rb, line 72 def exceptions @exceptions_file['exceptions'] end
handle_additions()
click to toggle source
# File lib/syllabize.rb, line 80 def handle_additions @syllables += count_ys_in_vowel_role if contains_non_initial_y? if contains_le_vowel_sound? || begins_with_re_vowel? || ends_in_sm? @syllables += 1 end end
handle_exceptions()
click to toggle source
# File lib/syllabize.rb, line 76 def handle_exceptions exceptions[str] end
handle_non_string_input()
click to toggle source
# File lib/syllabize.rb, line 62 def handle_non_string_input if !(str.is_a?(String)) raise ArgumentError.new "#{str} must be a string" end end
handle_subtractions()
click to toggle source
# File lib/syllabize.rb, line 87 def handle_subtractions @syllables -= 1 if ends_in_silent_e? @syllables -= count_diphthongs if contains_diphthongs? end
has_more_than_one_syllable?()
click to toggle source
# File lib/syllabize.rb, line 128 def has_more_than_one_syllable? (count_vowels - count_diphthongs) > 1 end
is_int_in_string_form?()
click to toggle source
# File lib/syllabize.rb, line 54 def is_int_in_string_form? str_as_int = str.to_i if str_as_int.zero? return str == '0' ? true : false end (str_as_int * str_as_int) > 0 ? true : false end
load_exceptions()
click to toggle source
# File lib/syllabize.rb, line 68 def load_exceptions @exceptions_file = YAML::load_file(File.join(__dir__, 'exceptions.yml')) end
strip_punctuation(string)
click to toggle source
# File lib/syllabize.rb, line 50 def strip_punctuation(string) string.gsub(/[[:punct:]]/,'') end