Skip to content

Instantly share code, notes, and snippets.

@clarkgrubb
Created August 21, 2009 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkgrubb/172481 to your computer and use it in GitHub Desktop.
Save clarkgrubb/172481 to your computer and use it in GitHub Desktop.
class FogFactor
class << self
def syllable_count(raw_word)
word = raw_word.gsub(/[^a-zA-Z]/,'')
syllables = word.scan(/[^aeiouy]+y|[^aeiouy]*(ai|au|ea|ee|ei|ie|io|oa|oi|oo|ou|[aeiou])|([^aeiouy]+|y)(ai|au|ea|ee|ei|ie|io|oa|oi|oo|ou|[aeiou])/i)
if syllables.size > 1 and word.match(/(e|es)$/)
syllables.size - 1
else
syllables.size
end
end
def fog_factor(s)
words = s.scan(/\S+/)
sentences = s.split(/[.!?]+/)
trisyllables = words.select { |w| syllable_count(w) > 2 }
(words.size.to_f/sentences.size + trisyllables.size.to_f/words.size) * 0.4
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment