Class: TextMetrics::Processors::Base
- Inherits:
-
Object
- Object
- TextMetrics::Processors::Base
- Defined in:
- lib/text_metrics/processors/base.rb
Direct Known Subclasses
Constant Summary collapse
- GEM_PATH =
File.dirname(__FILE__, 2).freeze
- METRICS =
Single source of truth: #to_h and reader methods are both derived from this list.
%i[ words_count characters_count sentences_count syllables_count punctuation_count polysyllabic_words_count long_words_count syllables_per_word_average letters_per_word_average words_per_sentence_average characters_per_sentence_average words_per_punctuation_average punctuation_per_sentence_average type_token_ratio flesch_reading_ease flesch_kincaid_grade lix smog_index gunning_fog_index coleman_liau_index automated_readability_index reading_time ].freeze
Instance Attribute Summary collapse
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
- #automated_readability_index ⇒ Object
- #characters_count(ignore_spaces: true) ⇒ Object
- #characters_per_sentence_average ⇒ Object
- #coleman_liau_index ⇒ Object
- #flesch_kincaid_grade ⇒ Object
- #flesch_reading_ease ⇒ Object
- #gunning_fog_index ⇒ Object
-
#initialize(text, language: nil) ⇒ Base
constructor
A new instance of Base.
- #letters_per_word_average ⇒ Object
- #lix ⇒ Object
- #long_words_count ⇒ Object
- #polysyllabic_words_count ⇒ Object
- #punctuation_count ⇒ Object
- #punctuation_per_sentence_average ⇒ Object
- #reading_time(wpm: nil) ⇒ Object
- #sentences_count ⇒ Object
- #smog_index ⇒ Object
- #syllables_count ⇒ Object
- #syllables_per_word_average ⇒ Object
- #to_h ⇒ Object
- #type_token_ratio ⇒ Object
-
#words_count ⇒ Object
counts.
- #words_per_punctuation_average ⇒ Object
- #words_per_sentence_average ⇒ Object
Constructor Details
#initialize(text, language: nil) ⇒ Base
Returns a new instance of Base.
38 39 40 41 |
# File 'lib/text_metrics/processors/base.rb', line 38 def initialize(text, language: nil) @text = (text || "").squeeze(" ") @language = language end |
Instance Attribute Details
#language ⇒ Object (readonly)
Returns the value of attribute language.
36 37 38 |
# File 'lib/text_metrics/processors/base.rb', line 36 def language @language end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
36 37 38 |
# File 'lib/text_metrics/processors/base.rb', line 36 def text @text end |
Instance Method Details
#automated_readability_index ⇒ Object
153 154 155 156 157 |
# File 'lib/text_metrics/processors/base.rb', line 153 def automated_readability_index return 0.0 if words_count.zero? (4.71 * characters_count / words_count.to_f + 0.5 * average_words_per_sentence - 21.43).round(1) end |
#characters_count(ignore_spaces: true) ⇒ Object
52 53 54 |
# File 'lib/text_metrics/processors/base.rb', line 52 def characters_count(ignore_spaces: true) ignore_spaces ? text.delete(" ").length : text.length end |
#characters_per_sentence_average ⇒ Object
90 91 92 93 94 |
# File 'lib/text_metrics/processors/base.rb', line 90 def characters_per_sentence_average return 0.0 if sentences_count.zero? (characters_count.to_f / sentences_count).round(2) end |
#coleman_liau_index ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/text_metrics/processors/base.rb', line 138 def coleman_liau_index return 0.0 if words_count.zero? letters_per_100_words = average_letters_per_word * 100 sentences_per_100_words = sentences_count.to_f / words_count * 100 (0.0588 * letters_per_100_words - 0.296 * sentences_per_100_words - 15.8).round(2) end |
#flesch_kincaid_grade ⇒ Object
118 119 120 121 122 |
# File 'lib/text_metrics/processors/base.rb', line 118 def flesch_kincaid_grade return 0.0 if words_count.zero? (0.39 * average_words_per_sentence + 11.8 * average_syllables_per_word - 15.59).round(1) end |
#flesch_reading_ease ⇒ Object
114 115 116 |
# File 'lib/text_metrics/processors/base.rb', line 114 def flesch_reading_ease raise NotImplementedError end |
#gunning_fog_index ⇒ Object
132 133 134 135 136 |
# File 'lib/text_metrics/processors/base.rb', line 132 def gunning_fog_index return 0.0 if words_count.zero? (0.4 * (average_words_per_sentence + 100.0 * polysyllabic_words_count / words_count)).round(1) end |
#letters_per_word_average ⇒ Object
82 83 84 |
# File 'lib/text_metrics/processors/base.rb', line 82 def letters_per_word_average average_letters_per_word.round(2) end |
#lix ⇒ Object
147 148 149 150 151 |
# File 'lib/text_metrics/processors/base.rb', line 147 def lix return 0.0 if words_count.zero? (average_words_per_sentence + 100.0 * long_words_count / words_count).round(2) end |
#long_words_count ⇒ Object
74 75 76 |
# File 'lib/text_metrics/processors/base.rb', line 74 def long_words_count words.count { |word| word.length > 6 } end |
#polysyllabic_words_count ⇒ Object
70 71 72 |
# File 'lib/text_metrics/processors/base.rb', line 70 def polysyllabic_words_count words.count { |word| count_syllables_in_word(word) >= 3 } end |
#punctuation_count ⇒ Object
66 67 68 |
# File 'lib/text_metrics/processors/base.rb', line 66 def punctuation_count punctuation_marks.size end |
#punctuation_per_sentence_average ⇒ Object
102 103 104 105 106 |
# File 'lib/text_metrics/processors/base.rb', line 102 def punctuation_per_sentence_average return 0.0 if punctuation_count.zero? || sentences_count.zero? (punctuation_count.to_f / sentences_count).round(2) end |
#reading_time(wpm: nil) ⇒ Object
159 160 161 162 163 164 |
# File 'lib/text_metrics/processors/base.rb', line 159 def reading_time(wpm: nil) wpm ||= TextMetrics.configuration.wpm return 0.0 if words_count.zero? || wpm.zero? (words_count / wpm.to_f).round(2) end |
#sentences_count ⇒ Object
56 57 58 59 60 |
# File 'lib/text_metrics/processors/base.rb', line 56 def sentences_count return 0 if words_count.zero? [1, sentences.size].max end |
#smog_index ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/text_metrics/processors/base.rb', line 124 def smog_index return 0.0 if sentences_count < 3 (1.043 * Math.sqrt(30.0 * polysyllabic_words_count / sentences_count) + 3.1291).round(1) rescue ZeroDivisionError 0.0 end |
#syllables_count ⇒ Object
62 63 64 |
# File 'lib/text_metrics/processors/base.rb', line 62 def syllables_count words.sum { |word| count_syllables_in_word(word) } end |
#syllables_per_word_average ⇒ Object
78 79 80 |
# File 'lib/text_metrics/processors/base.rb', line 78 def syllables_per_word_average average_syllables_per_word.round(1) end |
#to_h ⇒ Object
43 44 45 |
# File 'lib/text_metrics/processors/base.rb', line 43 def to_h @to_h ||= METRICS.to_h { |metric| [metric, public_send(metric)] } end |
#type_token_ratio ⇒ Object
108 109 110 111 112 |
# File 'lib/text_metrics/processors/base.rb', line 108 def type_token_ratio return 0.0 if words_count.zero? (words.uniq.size.to_f / words_count).round(2) end |
#words_count ⇒ Object
counts
48 49 50 |
# File 'lib/text_metrics/processors/base.rb', line 48 def words_count words.size end |
#words_per_punctuation_average ⇒ Object
96 97 98 99 100 |
# File 'lib/text_metrics/processors/base.rb', line 96 def words_per_punctuation_average return 0.0 if words_count.zero? || punctuation_count.zero? (words_count.to_f / punctuation_count).round(2) end |
#words_per_sentence_average ⇒ Object
86 87 88 |
# File 'lib/text_metrics/processors/base.rb', line 86 def words_per_sentence_average average_words_per_sentence.round(2) end |