A powerful Ruby gem for text readability analysis with exceptional performance
Calculate readability statistics, complexity metrics, and grade levels from text using proven formulas. Now with 36x performance improvement and support for 22 languages.
Operation | v0.1.x | v1.0.0 | Improvement |
---|---|---|---|
difficult_words |
~0.0047s | ~0.0013s | 36x faster |
text_standard |
~0.015s | ~0.012s | 20% faster |
Dictionary loading | File I/O every call | Cached in memory | 2x faster |
gem install textstat
Or add to your Gemfile:
gem 'textstat', '~> 1.0'
require 'textstat'
text = "This is a sample text for readability analysis. It contains multiple sentences with varying complexity levels."
# Basic statistics
TextStat.char_count(text) # => 112
TextStat.lexicon_count(text) # => 18
TextStat.syllable_count(text) # => 28
TextStat.sentence_count(text) # => 2
# Readability formulas
TextStat.flesch_reading_ease(text) # => 45.12
TextStat.flesch_kincaid_grade(text) # => 11.2
TextStat.gunning_fog(text) # => 14.5
TextStat.text_standard(text) # => "11th and 12th grade"
# Difficult words (with automatic caching)
TextStat.difficult_words(text) # => 4
TextStat supports 22 languages with optimized dictionary caching:
# English (default)
TextStat.difficult_words("Complex analysis", 'en_us')
# Spanish
TextStat.difficult_words("AnΓ‘lisis complejo", 'es')
# French
TextStat.difficult_words("Analyse complexe", 'fr')
# German
TextStat.difficult_words("Komplexe Analyse", 'de')
# Russian
TextStat.difficult_words("Π‘Π»ΠΎΠΆΠ½ΡΠΉ Π°Π½Π°Π»ΠΈΠ·", 'ru')
# Check cache status
TextStat::DictionaryManager.cache_size # => 5
TextStat::DictionaryManager.cached_languages # => ["en_us", "es", "fr", "de", "ru"]
Code | Language | Status | Code | Language | Status |
---|---|---|---|---|---|
en_us |
English (US) | β | fr |
French | β |
en_uk |
English (UK) | β | es |
Spanish | β |
de |
German | β | it |
Italian | β |
ru |
Russian | β | pt |
Portuguese | β |
pl |
Polish | β | sv |
Swedish | β |
da |
Danish | β | nl |
Dutch | β |
fi |
Finnish | β | ca |
Catalan | β |
cs |
Czech | β | hu |
Hungarian | β |
et |
Estonian | β | id |
Indonesian | β |
is |
Icelandic | β | la |
Latin | β |
hr |
Croatian | β οΈ | no2 |
Norwegian | β οΈ |
Note: Croatian and Norwegian have known issues with the text-hyphen library.
TextStat now caches language dictionaries in memory for massive performance improvements:
# First call loads dictionary from disk
TextStat.difficult_words(text, 'en_us') # ~0.0047s
# Subsequent calls use cached dictionary
TextStat.difficult_words(text, 'en_us') # ~0.0013s (36x faster!)
# Cache management
TextStat::DictionaryManager.cache_size # => 1
TextStat::DictionaryManager.cached_languages # => ["en_us"]
TextStat::DictionaryManager.clear_cache # Clear all cached dictionaries
# Character and word counts
TextStat.char_count(text, ignore_spaces = true)
TextStat.lexicon_count(text, remove_punctuation = true)
TextStat.syllable_count(text, language = 'en_us')
TextStat.sentence_count(text)
# Averages
TextStat.avg_sentence_length(text)
TextStat.avg_syllables_per_word(text, language = 'en_us')
TextStat.avg_letter_per_word(text)
TextStat.avg_sentence_per_word(text)
# Advanced statistics
TextStat.difficult_words(text, language = 'en_us')
TextStat.polysyllab_count(text, language = 'en_us')
# Popular formulas
TextStat.flesch_reading_ease(text, language = 'en_us')
TextStat.flesch_kincaid_grade(text, language = 'en_us')
TextStat.gunning_fog(text, language = 'en_us')
TextStat.smog_index(text, language = 'en_us')
# Academic formulas
TextStat.coleman_liau_index(text)
TextStat.automated_readability_index(text)
TextStat.linsear_write_formula(text, language = 'en_us')
TextStat.dale_chall_readability_score(text, language = 'en_us')
# International formulas
TextStat.lix(text) # Swedish formula
TextStat.forcast(text, language = 'en_us') # Technical texts
TextStat.powers_sumner_kearl(text, language = 'en_us') # Primary grades
TextStat.spache(text, language = 'en_us') # Elementary texts
# Consensus grade level
TextStat.text_standard(text) # => "8th and 9th grade"
TextStat.text_standard(text, true) # => 8.5 (numeric)
TextStat 1.0.0 features a clean modular architecture:
TextStat::BasicStats
- Character, word, syllable, and sentence countingTextStat::DictionaryManager
- Dictionary loading and caching with 36x performance boostTextStat::ReadabilityFormulas
- All readability calculations and text standardsTextStat::Main
- Unified interface combining all modulesAll existing code continues to work unchanged:
# This still works exactly the same
TextStat.flesch_reading_ease(text) # => 45.12
TextStat.difficult_words(text) # => 4 (but now 36x faster!)
TextStat 1.0.0 includes comprehensive testing:
Run tests:
bundle exec rspec
TextStat 1.0.0 is 100% backward compatible:
# Your existing code works unchanged
TextStat.flesch_reading_ease(text) # Same API
TextStat.difficult_words(text) # Same API, 36x faster!
# New cache management (optional)
TextStat::DictionaryManager.cache_size
TextStat::DictionaryManager.cached_languages
TextStat::DictionaryManager.clear_cache
# New modular access (optional)
analyzer = TextStat::Main.new
analyzer.flesch_reading_ease(text)
Compare performance yourself:
require 'textstat'
require 'benchmark'
text = "Your sample text here..." * 100
Benchmark.bm do |x|
x.report("difficult_words (first call)") { TextStat.difficult_words(text) }
x.report("difficult_words (cached)") { TextStat.difficult_words(text) }
x.report("text_standard") { TextStat.text_standard(text) }
end
git clone https://github.com/kupolak/textstat.git
cd textstat
bundle install
# All tests
bundle exec rspec
# Specific test files
bundle exec rspec spec/languages_spec.rb
bundle exec rspec spec/performance_spec.rb
bundle exec yard doc
bundle exec rubocop
We welcome contributions! Please see our Contributing Guide for details.
git checkout -b feature/amazing-feature
)bundle exec rspec
)bundle exec rubocop
)git commit -m 'Add amazing feature'
)git push origin feature/amazing-feature
)This project is licensed under the MIT License - see the LICENSE.txt file for details.