From: Phlip Date: 2002-08-05T11:58:18+09:00 Subject: Regex to count syllables Rubies: Just a tricky one. I need a function that counts syllables in English words with, say, 99.5% accuracy for common words. The deal is a "syllable" seems to be "a group of one to three vowels, always including Y"; the exceptions seen so far are "a trailing 'ing' is always a syllable", and "silent E on the end not preceded by L is not a syllable." We'l think of other rules, but I'm just curious how to fold my brute-force reckoning into a single regex. Right now I use a regex and a couple of 'if' statements. Note the function needn't accurately locate the syllables, it only needs to find them: Tokenizer = /([aeiouy]{1,3})/ def syllableCounter word len = 0 if word[-3..-1] == 'ing' then len += 1 word = word[0...-3] end got = word.scan(Tokenizer) len += got.size() # the next exception to the regex is the # trailing silent 'e' (which used to be a syllable) # and the exception to the exception is 'le'. 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 assert 1 == syllableCounter('a') assert 1 == syllableCounter('aa') assert 1 == syllableCounter('aaa') assert 2 == syllableCounter('aaay') assert 1 == syllableCounter('rat') assert 1 == syllableCounter('elf') assert 2 == syllableCounter('emu') assert 2 == syllableCounter('elven') assert 2 == syllableCounter('queuing') assert 2 == syllableCounter('kozmic') assert 2 == syllableCounter('throughout') assert 2 == syllableCounter('vicious') assert 2 == syllableCounter('aenid') assert 1 == syllableCounter('way') assert 2 == syllableCounter('parking') assert 3 == syllableCounter('sylabus') assert 4 == syllableCounter('invigorate') assert 4 == syllableCounter('kleptomania') assert 3 == syllableCounter('diastrophism') assert 3 == syllableCounter('elegy') assert 4 == syllableCounter('nymphomaniac') assert 5 == syllableCounter('hippopotamus') assert 2 == syllableCounter('filthy') assert 3 == syllableCounter('venison') assert 1 == syllableCounter('frog') assert 1 == syllableCounter('fly') assert 2 == syllableCounter('eagle') assert 4 == syllableCounter('Liberiencis') assert 3 == syllableCounter('behemoth') assert 2 == syllableCounter('river') assert 2 == syllableCounter('zeekoe') assert 2 == syllableCounter('yarrow') assert 1 == syllableCounter('yeah') assert 2 == syllableCounter('being') assert 0 == syllableCounter('') So how do I merge those exceptions into the main regex? -- Phlip http://c2.com/cgi/wiki?DevNull -- It's a small Web, after all... --