Class: FleschKincaid::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/flesch_kincaid/test.rb

Overview

Internal: Tests a block of text based on the Flesch-Kincaid readability test and returns a Result.

Examples

FleschKincaid::Test.new("A sentence that is long and hard to read").result

Constant Summary collapse

BASE =
206.835.freeze
SENTENCE_MULTIPLIER =
1.015.freeze
SYLLABLE_MULTIPLIER =
84.6.freeze
WORD_REGEX =
/\w+[^\w]/.freeze
SENTENCE_REGEX =
/[\w\s](\.|\!|\?)+/.freeze
COMPRESS_VOWEL_REGEX =
/(?:[^laeiouy]es|ed|[^laeiouy]e)$/.freeze
Y_REGEX =
/^y/.freeze
VOWEL_REGEX =
/[aeiouy]{1,2}/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Test

Returns a new instance of Test.



33
34
35
# File 'lib/flesch_kincaid/test.rb', line 33

def initialize(text)
  @text = text
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



30
31
32
# File 'lib/flesch_kincaid/test.rb', line 30

def text
  @text
end

Instance Method Details

#resultObject



37
38
39
# File 'lib/flesch_kincaid/test.rb', line 37

def result
  @result ||= Result.new(formula_output)
end

#total_sentencesObject

The total number of sentences in #text

Returns Integer



51
52
53
# File 'lib/flesch_kincaid/test.rb', line 51

def total_sentences
  text.scan(SENTENCE_REGEX).count
end

#total_syllablesObject

The total number of syllables in #text

Returns Integer



58
59
60
61
62
63
64
65
# File 'lib/flesch_kincaid/test.rb', line 58

def total_syllables
  return 1 if text.length <= 3
  text
    .downcase
    .sub(COMPRESS_VOWEL_REGEX, '')
    .sub(Y_REGEX, '')
    .scan(VOWEL_REGEX).size
end

#total_wordsObject

The total number of words in #text

Returns Integer



44
45
46
# File 'lib/flesch_kincaid/test.rb', line 44

def total_words
  text.scan(WORD_REGEX).count
end