Class: TextStat::Main

Inherits:
Object
  • Object
show all
Includes:
BasicStats, DictionaryManager, ReadabilityFormulas
Defined in:
lib/textstat/main.rb

Overview

Main class providing text readability analysis

This class combines all TextStat modules to provide a unified interface for text analysis. It includes basic statistics, dictionary management, and readability formulas in a single class.

The class maintains backward compatibility through method delegation, ensuring that existing code continues to work seamlessly.

Examples:

Creating an instance

analyzer = TextStat::Main.new
analyzer.flesch_reading_ease(\"Sample text\")  # => 83.32

Using class methods (backward compatibility)

TextStat::Main.flesch_reading_ease(\"Sample text\")  # => 83.32
TextStat.flesch_reading_ease(\"Sample text\")        # => 83.32

Author:

  • Jakub Polak

Since:

  • 1.0.0

Class Method Summary collapse

Methods included from ReadabilityFormulas

#automated_readability_index, #coleman_liau_index, #dale_chall_readability_score, #flesch_kincaid_grade, #flesch_reading_ease, #forcast, #gunning_fog, #linsear_write_formula, #lix, #powers_sumner_kearl, #smog_index, #spache, #text_standard

Methods included from DictionaryManager

cache_size, cached_languages, clear_cache, #difficult_words

Methods included from BasicStats

#avg_letter_per_word, #avg_sentence_length, #avg_sentence_per_word, #avg_syllables_per_word, #char_count, #lexicon_count, #polysyllab_count, #sentence_count, #syllable_count

Class Method Details

.clear_dictionary_cacheHash

Clear all cached dictionaries

Examples:

TextStat::Main.clear_dictionary_cache

Returns:

  • (Hash)

    empty cache

Since:

  • 1.0.0



96
97
98
# File 'lib/textstat/main.rb', line 96

def clear_dictionary_cache
  DictionaryManager.clear_cache
end

.dictionary_pathString

Get current dictionary path

Examples:

TextStat::Main.dictionary_path  # => \"/path/to/dictionaries\"

Returns:

  • (String)

    current dictionary path

Since:

  • 1.0.0



87
88
89
# File 'lib/textstat/main.rb', line 87

def dictionary_path
  DictionaryManager.dictionary_path
end

.dictionary_path=(path) ⇒ String

Set dictionary path for all instances

Examples:

TextStat::Main.dictionary_path = \"/custom/dictionaries\"

Parameters:

  • path (String)

    path to dictionary directory

Returns:

  • (String)

    the set path

Since:

  • 1.0.0



78
79
80
# File 'lib/textstat/main.rb', line 78

def dictionary_path=(path)
  DictionaryManager.dictionary_path = path
end

.load_dictionary(language) ⇒ Set

Load dictionary for specified language

Examples:

TextStat::Main.load_dictionary('en_us')

Parameters:

  • language (String)

    language code

Returns:

  • (Set)

    set of easy words for the language

Since:

  • 1.0.0



106
107
108
# File 'lib/textstat/main.rb', line 106

def load_dictionary(language)
  DictionaryManager.load_dictionary(language)
end

.method_missing(method_name, *args, **kwargs, &block) ⇒ Object

Handle method delegation for backward compatibility

This method ensures that all instance methods can be called as class methods, maintaining compatibility with the pre-1.0 API.

Parameters:

  • method_name (Symbol)

    the method name being called

  • args (Array)

    method arguments

  • kwargs (Hash)

    keyword arguments

  • block (Proc)

    block if provided

Returns:

  • (Object)

    result of the method call

Since:

  • 1.0.0



53
54
55
56
57
58
59
60
# File 'lib/textstat/main.rb', line 53

def method_missing(method_name, *args, **kwargs, &block)
  instance = new
  if instance.respond_to?(method_name)
    instance.send(method_name, *args, **kwargs, &block)
  else
    super
  end
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Check if method exists for delegation

Parameters:

  • method_name (Symbol)

    the method name to check

  • include_private (Boolean) (defaults to: false)

    whether to include private methods

Returns:

  • (Boolean)

    true if method exists

Since:

  • 1.0.0



68
69
70
# File 'lib/textstat/main.rb', line 68

def respond_to_missing?(method_name, include_private = false)
  new.respond_to?(method_name, include_private) || super
end