>>> import textstat
>>> test_data = (
"Playing games has always been thought to be important to "
"the development of well-balanced and creative children; "
"however, what part, if any, they should play in the lives "
"of adults has never been researched that deeply. I believe "
"that playing games is every bit as important for adults "
"as for children. Not only is taking time out to play games "
"with our children and other adults valuable to building "
"interpersonal relationships but is also a wonderful way "
"to release built up tension."
)
>>> textstat.flesch_reading_ease(test_data)
>>> textstat.flesch_kincaid_grade(test_data)
>>> textstat.smog_index(test_data)
>>> textstat.coleman_liau_index(test_data)
>>> textstat.automated_readability_index(test_data)
>>> textstat.dale_chall_readability_score(test_data)
>>> textstat.difficult_words(test_data)
>>> textstat.linsear_write_formula(test_data)
>>> textstat.gunning_fog(test_data)
>>> textstat.text_standard(test_data)
>>> textstat.fernandez_huerta(test_data)
>>> textstat.szigriszt_pazos(test_data)
>>> textstat.gutierrez_polini(test_data)
>>> textstat.crawford(test_data)
>>> textstat.gulpease_index(test_data)
>>> textstat.osman(test_data)
The argument (text) for all the defined functions remains the same -
i.e the text for which statistics need to be calculated.
Install
You can install textstat either via the Python Package Index (PyPI) or from source.
Install using pip
pip install textstat
Install using easy_install
easy_install textstat
Install latest version from GitHub
git clone https://github.com/textstat/textstat.git
cd textstat
pip install .
tar xfz textstat-*.tar.gz
cd textstat-*/
python setup.py build
python setup.py install # as root
Language support
By default functions implement algorithms for english language.
To change language, use:
textstat.set_lang(lang)
The language will be used for syllable calculation and to choose
variant of the formula.
Language variants
All functions implement en_US language. Some of them has also variants
for other languages listed below.
Function
en
de
es
fr
it
nl
pl
ru
flesch_reading_ease
✔
✔
✔
✔
✔
✔
✔
gunning_fog
✔
✔
Spanish-specific tests
The following functions are specifically designed for spanish language.
They can be used on non-spanish texts, even though that use case is not recommended.
Returns the Flesch-Kincaid Grade of the given text. This is a grade
formula in that a score of 9.3 means that a ninth grader would be able to
read the document.
Returns the SMOG index of the given text. This is a grade formula in that
a score of 9.3 means that a ninth grader would be able to read the document.
Texts of fewer than 30 sentences are statistically invalid, because
the SMOG formula was normed on 30-sentence samples. textstat requires at
least 3 sentences for a result.
Returns the grade level of the text using the Coleman-Liau Formula. This is
a grade formula in that a score of 9.3 means that a ninth grader would be
able to read the document.
Returns the grade level using the Linsear Write Formula. This is
a grade formula in that a score of 9.3 means that a ninth grader would be
able to read the document.
Different from other tests, since it uses a lookup table
of the most commonly used 3000 English words. Thus it returns
the grade level using the New Dale-Chall Formula.
Returns the number of syllables present in the given text.
Uses the Python module Pyphen
for syllable calculation in most languages, but defaults to
nltk.corpus.cmudict for
en_US.
Lexicon Count
textstat.lexicon_count(text, removepunct=True)
Calculates the number of words present in the text.
Optional removepunct specifies whether we need to take
punctuation symbols into account while counting lexicons.
Default value is True, which removes the punctuation
before counting lexicon items.
Sentence Count
textstat.sentence_count(text)
Returns the number of sentences present in the given text.
Character Count
textstat.char_count(text, ignore_spaces=True)
Returns the number of characters present in the given text.
Letter Count
textstat.letter_count(text, ignore_spaces=True)
Returns the number of characters present in the given text without punctuation.
Polysyllable Count
textstat.polysyllabcount(text)
Returns the number of words with a syllable count greater than or equal to 3.
Monosyllable Count
textstat.monosyllabcount(text)
Returns the number of words with a syllable count equal to one.
Contributing
If you find any problems, you should open an
issue.
If you can fix an issue you've found, or another issue, you should open
a pull request.
Fork this repository on GitHub to start making your changes to the master
branch (or branch off of it).
Write a test which shows that the bug was fixed or that the feature works as expected.
Send a pull request!
Development setup
It is recommended you use a virtual environment, or Pipenv to keep your development work isolated from your
systems Python installation.
$ git clone https://github.com/<yourname>/textstat.git # Clone the repo from your fork
$ cd textstat
$ pip install -r requirements.txt # Install all dependencies
$ # Make changes
$ python -m pytest test.py # Run tests