Sha256: 14d1ca9b9fe0cc611885718401a0383a67dc6f961c5db9444565691c2c526b3e

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'syllable_counter/english_exceptions'

module SyllableCounter
  module Rules
    class Rules
      include EnglishExceptions

      def initialize

      end

      def execute(word)
        parsed_word = clean_input(word)
        parsed_word = clean_input(word)
        parsed_word = delete_trailing_e(parsed_word)
        parsed_word = delete_past_ed(parsed_word)
        parsed_word = combine_consecutive_vowels(parsed_word)
        parsed_word = remove_plural_suffix(parsed_word)
        parsed_word
      end

      def is_exception?(word)
        exceptions.keys.include?(word)
      end

      def exceptions
        EnglishExceptions::EXCEPTIONS
      end

      protected
      def delete_trailing_e(word)
        #deletes trailing e if preceded by a vowel+[lr] or [^lr]
        if word =~ /[aeiouy][lr]e$/ || word =~ /[^lr]e$/
          word.sub!(/e$/,"")
        end
        word
      end

      def delete_past_ed(word)
        #removes /ed/ suffix except when preceded by /t/ or /d/

        if word =~ /[^td]ed$/
          word.sub!(/ed$/,"")
        end
        word
      end

      def combine_consecutive_vowels(word)
        #combines consecutive vowels and replpaces with % placeholder

        while word =~ /[aeiouy%][aeiouy%]/
          word.sub!(/[aeiouy%][aeiouy%]/,"%")
        end
        word
      end

      def remove_plural_suffix(word)
        #Final /es/ plural suffix is realized after /s/ /z/ /sh/ /dg/ /ch/ /[aeiou]g/
        if word =~ /[^([z]|[sh]|[dg]|[ch]|[aeiouy][g]|[s])]es$/
          word.sub!(/es$/,"")
        end
        word
      end

      private
      def clean_input(word)
        word.downcase.strip
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
syllable_counter-1.0.0 lib/syllable_counter/rules.rb