Created
March 9, 2011 22:48
-
-
Save henrygarner/863181 to your computer and use it in GitHub Desktop.
Ruby Coleman-Liau string extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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