Class: Rundown::Processors::ReadingGrade

Inherits:
Rundown::Processor show all
Defined in:
lib/rundown/processors/reading_grade.rb

Constant Summary

Constants inherited from Rundown::Processor

Rundown::Processor::PUNCTUATION

Instance Attribute Summary

Attributes inherited from Rundown::Processor

#text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rundown::Processor

#initialize, #sentences

Constructor Details

This class inherits a constructor from Rundown::Processor

Class Method Details

.known_wordsObject



16
17
18
# File 'lib/rundown/processors/reading_grade.rb', line 16

def self.known_words
  @known_words ||= File.readlines(File.expand_path('../../../../data/known_words.txt', __FILE__)).map(&:chomp)
end

Instance Method Details

#average_sentence_lengthObject



32
33
34
35
# File 'lib/rundown/processors/reading_grade.rb', line 32

def average_sentence_length
  sen = sentences.map {|s| words_for(s).length }
  sen.reduce(:+).to_f / sen.size
end

#difficult_wordsObject



8
9
10
# File 'lib/rundown/processors/reading_grade.rb', line 8

def difficult_words
  @difficult_words ||= (words_without_nouns.uniq.map(&:downcase) - known_words.map(&:downcase)).reject {|w| w.length < 5 }
end

#gradeObject

4.9 and Below Grade 4 and Below 5.0 to 5.9 Grades 5 - 6 6.0 to 6.9 Grades 7 - 8 7.0 to 7.9 Grades 9 - 10 8.0 to 8.9 Grades 11 - 12 9.0 to 9.9 Grades 13 - 15 (College) 10 and Above Grades 16 and Above (College Graduate)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rundown/processors/reading_grade.rb', line 50

def grade
  @grades ||= case score
    when 10.0..100.0
      16
    when 9.0..10.0
      13
    when 8.5..9.0
      12
    when 8.0..8.5
      11
    when 7.5..8.0
      10
    when 7.0..7.5
      9
    when 6.5..7.0
      8
    when 6.0..6.5
      7
    when 5.5..6.0
      6
    when 5.0..5.5
      5
    else
      4
  end
end

#known_wordsObject



12
13
14
# File 'lib/rundown/processors/reading_grade.rb', line 12

def known_words
  self.class.known_words
end

#percent_difficult_wordsObject



4
5
6
# File 'lib/rundown/processors/reading_grade.rb', line 4

def percent_difficult_words
  (difficult_words.length.to_f / words.length.to_f) * 100
end

#processObject



77
78
79
# File 'lib/rundown/processors/reading_grade.rb', line 77

def process
  grade
end

#scoreObject



38
39
40
41
# File 'lib/rundown/processors/reading_grade.rb', line 38

def score
  raw_score = (0.1579 * (percent_difficult_words)) + (0.0496 * average_sentence_length)
  percent_difficult_words > 5.0 ? raw_score + 3.6365 : raw_score
end

#wordsObject



20
21
22
# File 'lib/rundown/processors/reading_grade.rb', line 20

def words
  text.split(/[^a-zA-Z']+/)
end

#words_for(sentence) ⇒ Object



28
29
30
# File 'lib/rundown/processors/reading_grade.rb', line 28

def words_for(sentence)
  sentence.split(/[^a-zA-Z']+/)
end

#words_without_nounsObject



24
25
26
# File 'lib/rundown/processors/reading_grade.rb', line 24

def words_without_nouns
  words.reject { |w| w[0].upcase == w[0] }
end