Skip to main content

esTS

Spanish Texts Statistics - a library for statistics extraction from texts in Spanish

Documentation · PyPI · Español

Version Supported Python versions Build Ruff License


esTS computes for Spanish texts what usually requires assembling several separate tools: basic statistics, readability and lexical diversity - by published formulas with the coefficients and the scales of their authors, deterministically and without a neural network inside.

The library works both with raw strings and with Doc objects of spaCy, and needs no trained model: sentences, words and character N-grams are extracted by rules, syllables and stress follow from the orthography.

  • Object extraction - configurable sentence, word and character N-gram tokenizers that know the inverted marks, the dialogue dash and the abbreviations of Spanish
  • Syllables and stress - rule-based syllabification and the stressed syllable derived from the spelling, with no dictionary
  • Basic statistics - counts of sentences, words, letters, syllables and punctuation marks by type, with distributions and normalized shares
  • Readability metrics - Fernández Huerta, Szigriszt-Pazos with the INFLESZ scale, Gutiérrez de Polini, Crawford, Legibilidad µ, SOL, LIX and RIX, with a consensus grade, the school stages of Spain and reading time
  • Lexical diversity metrics - TTR and its variations, MATTR, MSTTR, MTLD, HD-D, Simpson's and Yule's indices, entropy, Zipf's and Heaps' laws

Morphology, syntax and cohesion on Universal Dependencies come in 0.2, corpus measures and stylometry in 0.3, style, phonostatistics, metre and rhyme in 0.4.

Installation

Requires Python 3.11 or newer.

pip install pyests

Or with uv:

uv add pyests

The distribution on PyPI is pyests, the package it installs is ests. No spaCy model is needed for the statistics of 0.1; one is needed only to parse a text yourself and pass the Doc instead of a string:

python -m spacy download es_core_news_sm

Quick start

>>> from ests import BasicStats, DiversityStats, ReadabilityStats

>>> text = "Hay tres clases de mentiras: mentiras, malditas mentiras y estadísticas"

>>> BasicStats(text).get_stats()
{'c_letters': {1: 1, 2: 1, 3: 1, 4: 1, 6: 1, 8: 4, 12: 1},
 'c_syllables': {1: 4, 2: 1, 3: 4, 5: 1},
 'n_sents': 1,
 'n_words': 10,
 'n_unique_words': 8,
 'n_long_words': 5,
 'n_complex_words': 5,
 'n_simple_words': 5,
 'n_monosyllable_words': 4,
 'n_polysyllable_words': 6,
 'n_chars': 71,
 'n_letters': 60,
 'n_spaces': 9,
 'n_syllables': 23,
 'n_punctuations': 2,
 'c_punctuations': {'comma': 1, 'period': 0, 'question': 0, 'exclamation': 0,
                    'ellipsis': 0, 'colon': 1, 'semicolon': 0, 'dash': 0,
                    'hyphen': 0, 'angle_quotes': 0, 'straight_quotes': 0,
                    'parentheses': 0, 'other': 0}}

>>> ReadabilityStats(text).flesch_reading_easy
53.545000000000016

>>> DiversityStats(text).ttr
0.8

Features

Object extraction

The library allows creating your own tools for sentence, word and character N-gram extraction from a text, which can be further employed for counting statistics. The sentence splitter knows the inverted marks, the dash of a line of dialogue with the remark of the narrator, the abbreviations and the initials of Spanish; the word tokenizer keeps clitics, ordinals and numbers written the Spanish way together, and lemmas come from simplemma, which needs no model.

>>> from ests import CharNgramsExtractor, SentsExtractor, WordsExtractor

>>> SentsExtractor().extract("—¿Vienes? —preguntó María. ¡Claro que sí!")
('—¿Vienes? —preguntó María.', '¡Claro que sí!')

>>> we = WordsExtractor(use_lexemes=True, stopwords=["de", "y"], filter_nums=True, ngram_range=(1, 2))
>>> we.extract("Hay 3 clases de mentiras y estadísticas")
('haber', 'clase', 'mentira', 'estadística', 'haber_clase', 'clase_mentira', 'mentira_estadística')

>>> CharNgramsExtractor(n=3, lowercase=True).extract("estadísticas")[:5]
('est', 'sta', 'tad', 'adí', 'dís')

More in the documentation.

Syllables and stress

Spanish spelling encodes both the syllable boundaries and the stress, so the library needs no dictionary: diphthongs, hiatuses and triphthongs, the silent u of qu and gu, the vocalic y, the h inside a diphthong and the consonant clusters give the syllables; the written accent, or the ending of the word when there is none, gives the stressed syllable. Adverbs in -mente and hyphenated compounds carry two stresses.

>>> from ests.syllables import stress_type, syllabify, word_stress, word_stresses

>>> syllabify("murciélago")
['mur', 'cié', 'la', 'go']

>>> syllabify("averiguáis")
['a', 've', 'ri', 'guáis']

>>> word_stress("construir"), stress_type("construir")
(1, 'aguda')

>>> word_stresses("fácilmente")
[0, 2]

More in the documentation.

Basic statistics

The library allows extracting the following statistics from a text:

  • the number of sentences
  • the number of words
  • the number of unique words
  • the number of long words
  • the number of complex words
  • the number of simple words
  • the number of monosyllable words
  • the number of polysyllable words
  • the number of characters
  • the number of letters
  • the number of spaces
  • the number of syllables
  • the number of punctuation marks and their distribution by type
  • the distribution of words by the number of letters
  • the distribution of words by the number of syllables

A complex word has three or more syllables and a long word seven or more letters, as the Spanish readability formulas count them. Any statistic can be printed in a readable form:

>>> from ests import BasicStats

>>> text = "Hay tres clases de mentiras: mentiras, malditas mentiras y estadísticas"
>>> BasicStats(text).print_stats()
     Statistic      |  Value
------------------------------
Sentences           |    1
Words               |    10
Unique words        |    8
Long words          |    5
Complex words       |    5
Simple words        |    5
Monosyllabic words  |    4
Polysyllabic words  |    6
Characters          |    71
Letters             |    60
Spaces              |    9
Syllables           |    23
Punctuation marks   |    2

More in the documentation.

Readability metrics

The library allows counting the following readability metrics:

  • Flesch reading ease with the coefficients of Szigriszt-Pazos or of Fernández Huerta
  • Gutiérrez de Polini comprehensibility formula
  • Crawford grade
  • Legibilidad µ
  • SOL grade, the SMOG index converted to Spanish
  • LIX readability measure
  • RIX readability measure

An interpretation layer works on top of the formulas: the band of a scale for the reading ease and for Legibilidad µ, a consensus grade as the median of the grade formulas, the school stage and the reader age of Spain, and reading time by the norms of Spanish-speaking readers.

The coefficients of the Flesch reading ease are selected by the preset argument: by default the fórmula de perspicuidad of Szigriszt-Pazos with the INFLESZ scale validated on texts for patients (general); the coefficients of Fernández Huerta with the bands of their author are available as classic.

>>> from pprint import pprint
>>> from ests import ReadabilityStats

>>> text = "Hay tres clases de mentiras: mentiras, malditas mentiras y estadísticas"
>>> rs = ReadabilityStats(text)

>>> pprint(rs.get_stats(), sort_dicts=False)
{'flesch_reading_easy': 53.545000000000016,
 'gutierrez_polini_index': 33.5,
 'crawford_grade': 5.812999999999999,
 'mu_index': 50.943396226415096,
 'sol_grade': 9.258359866374562,
 'lix': 60.0,
 'rix': 5.0,
 'consensus_grade': 9.0,
 'reading_time': 0.03597122302158273}

>>> rs.print_stats()
                   Metric                    |  Value
-------------------------------------------------------
Flesch reading ease (Szigriszt-Pazos)        |  53.55
Gutiérrez de Polini comprehensibility        |  33.50
Crawford grade                               |   5.81
Legibilidad µ                                |  50.94
SOL grade (SMOG for Spanish)                 |   9.26
LIX readability index                        |  60.00
RIX readability index                        |   5.00
Consensus grade                              |   9.00
Reading time (min)                           |   0.04

>>> rs.describe_level()
'algo difícil'

>>> rs.describe_grade()
'ESO (12-16 years)'

More in the documentation.

Lexical diversity metrics

The library allows counting 32 lexical diversity metrics, among them:

  • Type-Token Ratio and its variations: RTTR, CTTR, Herdan, Summer, Maas, Dugast
  • Moving Average Type-Token Ratio and Mean Segmental Type-Token Ratio
  • Measure of Textual Lexical Diversity and its moving-window variants MA-MTLD and MTLD-W
  • Hypergeometric Distribution D
  • Simpson's index, its reciprocal and the Gini-Simpson index
  • the hapax index (Honoré's R), the measures of Yule, Herdan, Sichel, Michéa, Brunet, Dugast and Baayen
  • Shannon entropy, evenness and perplexity
  • the slope of Zipf's law, the Zipf-Mandelbrot fit and the exponent of Heaps' law

Any metric can be computed over windows of equal length, which is the standard way to compare texts of different lengths: the mean over the windows comes with a confidence interval.

>>> from ests import DiversityStats

>>> text = "Hay tres clases de mentiras: mentiras, malditas mentiras y estadísticas"
>>> ds = DiversityStats(text)

>>> ds.ttr, ds.mtld, ds.yule_k
(0.8, 14.000000000000004, 600.0)

>>> ds.frequency_spectrum
{1: 7, 3: 1}

>>> DiversityStats("La legibilidad de un texto depende de la longitud de sus oraciones y de sus "
...                "palabras. Las fórmulas clásicas miden esas dos magnitudes y las combinan en "
...                "un solo número. Ninguna de ellas mide la comprensión: miden la superficie "
...                "del texto.").windowed("ttr", window_len=10)
WindowStats(mean=0.875, std=0.1258305739211792, lower=0.6747754774664074, upper=1.0752245225335926, n_windows=4)

More in the documentation.

Development

The project uses uv for dependency management and ruff for linting and formatting.

git clone https://github.com/SergeyShk/esTS.git
cd esTS

make deps        # create the environment and install dependencies
make test        # run the tests and docstring examples (doctest)
make lint        # ruff + mypy

Run make help for the full list of commands.

The documentation is bilingual: English pages are docs/*.md, Spanish ones are docs/*.es.md next to them (mkdocs-static-i18n); when editing a page, update both versions.

The installed version is ests.__version__. All exceptions inherit ests.EstsError and one of the built-in classes (SourceError and ParameterError - ValueError, SourceTypeError - TypeError, UnknownStatError - KeyError), so except ValueError keeps working. The library prints nothing on its own: its messages go to the ests logger (logging.getLogger("ests")) and are silent by default.

Before submitting changes, install the hooks that run the linters on commit and the tests on push:

uv run pre-commit install

Contributing

Bug reports, ideas and pull requests are welcome - issues are open. The workflow, the checks to run before submitting a pull request and how to shape the changes are described in CONTRIBUTING.md; the rules of conduct are in the code of conduct.

Project structure
  • docs - project documentation
  • ests:
    • basic_stats.py - basic text statistics
    • constants.py - constants of the Spanish language and of the metrics
    • diversity_stats.py - lexical diversity metrics
    • exceptions.py - library exceptions
    • extractors.py - tools for object extraction from a text
    • readability_stats.py - readability metrics
    • syllables.py - syllabification and stress
    • utils.py - helper tools
  • tests - tests mirroring the package structure

Authors

License

MIT

Citation

Please use the following BibTeX entry for citing esTS if you use it in your research or software. Citations are helpful for the continued development and maintenance of this library. The same metadata is in CITATION.cff - GitHub shows it under the "Cite this repository" button.

@software{esTS,
  author = {Sergey Shkarin},
  title = {{esTS, a library for statistics extraction from texts in Spanish}},
  year = 2026,
  url = {https://github.com/SergeyShk/esTS}
}

Release files for pyests 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyests 0.1.0
File Size Uploaded
pyests-0.1.0.tar.gz 52.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyests 0.1.0
File Interpreter ABI Platform
pyests-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 102.9 kB

Release files / pyests-0.1.0.tar.gz

Download URL pyests-0.1.0.tar.gz
Size 52.5 kB
Tags Source
SHA-256 checksum
How to use checksums
39e1940ff935231fc02df54562c192605cc1e766ca9cc4c945d5d2e38e1b0dc1
BLAKE2b-256 checksum
How to use checksums
f0c0c20d04a3d2498fa24c627cc2203f24596d442f6c43edba9df49b3ed3b83f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pyests-0.1.0-py3-none-any.whl

Download URL pyests-0.1.0-py3-none-any.whl
Size 50.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
edc93a02355e460ab64342e26f8a2f4e78ca03525123eab13982723980cd78d0
BLAKE2b-256 checksum
How to use checksums
c7fd04863d348f44c0293281aafdd1bea55cb4e42628962bcae04e8feec6d4b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

0.2.0

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page