Skip to content

Instantly share code, notes, and snippets.

@henrygarner
Created March 9, 2011 22:48
Show Gist options
  • Save henrygarner/863181 to your computer and use it in GitHub Desktop.
Save henrygarner/863181 to your computer and use it in GitHub Desktop.
Ruby Coleman-Liau string extension
# Code to calculate the Coleman-Liau index, which measures the understandability of a text.
# See http://en.wikipedia.org/wiki/Coleman-Liau_Index for more information
class String
def words
self.split(/[\W]? /)
end
def letters
self.scan(/\w/)
end
def sentences
# Rules: [.!?] end sentences, repetition is ignored
# Next sentence must be followed by a space and a capital letter or number
# Sentence will continue if last word is an acronym such as e.g. or U.S.
# Trailing non-word characters are removed from end of last sentence
self.split(/[\.!?]+\s+(?=[A-Z0-9])/).inject([]) do |sentences, partial|
sentences.last =~ /\.\w/ ? sentences.last << " #{partial.sub(/\W+$/,'')}" : sentences << partial.sub(/\W+$/,'')
sentences
end
end
def coleman_liau_index
(5.88 * letters.size / words.size) - (29.6 * sentences.size / words.size) - 15.8
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment