Skip to content

Word tree

wordtree(texts, keyword, max_n=5, max_per_n=8, **kwargs)

Building a word tree of the contexts of a keyword: the N-grams of up to max_n words that start or end with the keyword are counted in every list of words, such as a sentence. Every level keeps the max_per_n most frequent N-grams that continue a kept shorter one, alphabetically when equal; they are joined into two trees, after the keyword and before it, the size of the font by frequency (Wattenberg and Viégas 2008). Rendering needs the executables of Graphviz.

The function is that of the anyTS core.

The tree of the sentences of Marianela by Galdós from the corpus of literature:

from ests import SentsExtractor, WordsExtractor
from ests.datasets import SpanishLiterature
from ests.visualizers import wordtree

sl = SpanishLiterature()
sl.download()
text = next(
    record["text"] for record in sl.get_records(author="galdos") if record["title"] == "Marianela"
)

we = WordsExtractor(lowercase=True)
sentences = [we.extract(sentence) for sentence in SentsExtractor().extract(text)]
graph = wordtree(sentences, "ojos", max_n=4)
graph.render("wordtree", format="png")

ests

Parameters

Parameter Type Default Description
texts list[list[str]] - List of lists of words
keyword str - Keyword whose contexts are shown
max_n int 5 Largest size of the context
max_per_n int 8 Largest number of examples for every size of the context
**kwargs - - Drawing parameters: max_font_size (default 30), min_font_size (12), font_interp - a function of the relative frequency of a node (its frequency over the greatest one, in (0, 1]) giving the share of the range from min_font_size to max_font_size, from 0 to 1; the cube root by default

The function returns a Digraph of graphviz. Texts that are not a list of lists of strings and a keyword that is not a string raise SourceTypeError; no texts, or a keyword with no word next to it in any of them, SourceError; a max_n that is not an integer of at least 2 and a max_per_n that is not an integer of at least one ParameterError.