Class: TextMetrics::Processors::AmericanEnglish

Inherits:
Base
  • Object
show all
Defined in:
lib/text_metrics/processors/american_english.rb

Constant Summary collapse

SYLLABLE_DATABASE_PATH =
File.join(GEM_PATH, "dictionaries/english_word_syllable_database.txt").freeze
DATABASE_LOAD_MUTEX =
Mutex.new

Constants inherited from Base

Base::GEM_PATH, Base::METRICS

Instance Attribute Summary

Attributes inherited from Base

#language, #text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#characters_count, #characters_per_sentence_average, #coleman_liau_index, #flesch_kincaid_grade, #gunning_fog_index, #letters_per_word_average, #lix, #punctuation_count, #punctuation_per_sentence_average, #sentences_count, #smog_index, #syllables_count, #syllables_per_word_average, #to_h, #words_count, #words_per_punctuation_average, #words_per_sentence_average

Constructor Details

#initialize(text, language: :en_us) ⇒ AmericanEnglish

Returns a new instance of AmericanEnglish.



37
38
39
# File 'lib/text_metrics/processors/american_english.rb', line 37

def initialize(text, language: :en_us)
  super
end

Class Method Details

.syllable_databaseObject

CMU Pronouncing Dictionary syllable counts, loaded once and shared across all instances and threads. Lazy so requiring the gem (or using only French/Levenshtein) doesn’t pay the load cost; the mutex guarantees the file is parsed exactly once under concurrent first use, and the double check keeps the common path lock-free. The result is frozen, so concurrent reads are safe.



17
18
19
20
21
22
23
# File 'lib/text_metrics/processors/american_english.rb', line 17

def syllable_database
  return @syllable_database if @syllable_database

  DATABASE_LOAD_MUTEX.synchronize do
    @syllable_database ||= load_syllable_database
  end
end

Instance Method Details

#flesch_reading_easeObject



41
42
43
44
45
# File 'lib/text_metrics/processors/american_english.rb', line 41

def flesch_reading_ease
  return 0.0 if words_count.zero?

  (206.835 - 1.015 * average_words_per_sentence - 84.6 * average_syllables_per_word).round(2)
end