Skip to content

Sentence lengths

sentence_lengths_plot(source, window=10, inset=True, ax=None, labels=None, sents_extractor=None, words_extractor=None)

The curve of the lengths of the sentences - the rhythm of a text: the length of every sentence in words, in order, the moving average over window sentences and an inset with the histogram of the lengths. Short and long sentences in turn are an editorial sign of a lively text, a flat curve of a monotonous one. The function takes the axes ax and returns Axes.

The functions are those of the anyTS core; for a string esTS passes its Spanish SentsExtractor and WordsExtractor by default, and the sents_extractor and words_extractor parameters replace them. The default labels are the English ones of VISUALIZER_LABELS in anyts.constants; labels replaces any of them.

The curve of the second window of 2000 words of Niebla by Unamuno from the corpus of literature; a window is cut by words, so it opens with the end of a sentence:

from ests.corpus import split_windows
from ests.datasets import SpanishLiterature
from ests.visualizers import sentence_lengths_plot

sl = SpanishLiterature()
sl.download()
text = next(
    record["text"] for record in sl.get_records(author="unamuno") if record["title"] == "Niebla"
)
chapter = split_windows(text, 2000)[1]
sentence_lengths_plot(chapter, window=10)

ests

Parameters

Parameter Type Default Description
source str / Doc / Iterable[int] - Text, Doc object or lengths of the sentences (a list, a numpy array, a Series)
window int 10 Window of the moving average in sentences
inset bool True Show the inset with the histogram
ax Axes None Axes for the plot
labels dict[str, str] None Labels over the defaults: title, xlabel, ylabel, length (the curve), average (a format string with window), distribution (the inset)
sents_extractor SentsExtractor None Extractor of the sentences of a string; the sentence extractor of the library by default
words_extractor WordsExtractor None Extractor of the words of a sentence of a string; the word extractor of the library by default

The source and the extractors raise the errors of sentence_lengths, and no sentences raise SourceError; a window that is not an integer of at least one, labels that merge_labels refuses and an ax that is not a matplotlib Axes raise ParameterError.

Lengths of the sentences

sentence_lengths(source, sents_extractor=None, words_extractor=None)

The lengths of the sentences in words, in order: a string is split into sentences by the sentence extractor and every sentence into words by the word extractor; the sentences of a Doc come from its boundaries and its words from its tokens, punctuation and symbols left out, while a Doc without boundaries or with partial ones (has_sents) is counted as its text, by the extractors. Sentences without words are skipped. Ready lengths (non-negative integers in a sequence or an iterator) are used as they are. The arguments are those of sentence_lengths_plot.

Raises:

  • SourceTypeError - an object that is not a string, a Doc, a sequence or an iterator, a table, a set, a mapping, bytes, a length that is not an integer, an extractor of another type;
  • SourceError - a negative length, a string with a character without a UTF-8 form.
from ests.visualizers import sentence_lengths

sentence_lengths("El gato duerme. ¿Y el perro? —Come —dijo ella.")
sentence_lengths(chapter)[:5]
[3, 3, 3]
[1, 30, 41, 41, 14]