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; stress marks are stripped from the texts and from keyword.

The tree of the sentences of 50 texts of the StalinWorks dataset:

from ruts import SentsExtractor, WordsExtractor
from ruts.datasets import StalinWorks
from ruts.visualizers import wordtree

se = SentsExtractor()
we = WordsExtractor(min_len=3)
texts = StalinWorks().get_texts(limit=50)
words = [we.extract(sent) for text in texts for sent in se.extract(text)]
tree = wordtree(words, "рабочий", max_n=6)
tree.render("wordtree", format="png")

ruts

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.