Corpus plots¶
Plots for the corpus measures: lexical dispersion - where in the text a word occurs, a keyness chart from the results of keyness and a collocation network from the results of collocations. The matplotlib functions take axes ax, a new figure without them, and return Axes; the collocation network is a graphviz Graph, as the word tree is a Digraph.
The functions are those of the anyTS core with the Russian labels of the matplotlib plots of ruts.constants.VISUALIZER_LABELS by default; labels puts the given labels over them. dispersion_plot strips stress marks from the words and from targets.
Lexical dispersion¶
dispersion_plot(words, targets, ax=None, labels=None)
A row for every word of targets and a tick at the position of each of its occurrences in the text. Words are compared as they are: case and lemmatization belong to the extraction of the words.
| Parameter | Type | Default | Description |
|---|---|---|---|
words |
list[str] |
- | Words of the text in order |
targets |
list[str] |
- | Words whose occurrences are shown |
ax |
Axes |
None |
Axes for the plot |
labels |
dict[str, str] |
None |
Labels over the defaults: title, xlabel |
Words or targets that are not a list of strings raise SourceTypeError, no words or no targets SourceError; labels that merge_labels refuses and an ax that is not a matplotlib Axes raise ParameterError.
The words are extracted by WordsExtractor: lower case with lowercase=True, lemmas with use_lexemes=True.
Chart of keywords¶
keyness_plot(positive, negative=(), top_n=20, labels=None, field='score', log=False, ax=None)
Diverging horizontal bars: the words of positive to the right, of negative (the result of keyness with positive=False) to the left, top_n on each side. The length of a bar is the absolute value of the field (score, g2, log_ratio), so the side is set by the list, not by the sign of the measure; words with an undefined or infinite value are skipped. For the odds ratio (from 0 to infinity, one - equal odds) log=True plots the absolute \(\log_2\) of the value, symmetric around one.
| Parameter | Type | Default | Description |
|---|---|---|---|
positive |
list[Keyword] |
- | Positive keywords |
negative |
list[Keyword] |
() |
Negative keywords |
top_n |
int |
20 |
Number of words on each side |
labels |
dict[str, str] / tuple[str, str] |
None |
Labels over the defaults: title, xlabel and xlabel_log (format strings with field), target and reference (the legend); a pair of strings sets the legend alone |
field |
str |
'score' |
Field of Keyword whose values are plotted |
log |
bool |
False |
Plot \(\log_2\) of the value - for the odds ratio |
ax |
Axes |
None |
Axes for the plot |
Keywords that are not a list raise SourceTypeError, and no keyword with a finite value SourceError. An unknown field, a top_n that is not an integer of at least one, labels that merge_labels refuses or a legend that is not a pair, and an ax that is not a matplotlib Axes raise ParameterError.
Network of collocations¶
collocation_network(collocations, top_n=None)
An undirected graph in the neato layout: the nodes are the words, the size of the font by frequency, the edges the pairs, the width and the label by the measure. A pair of a word with itself (a word repeated within the window) would be a loop and is left out before top_n. Rendering needs the executables of Graphviz; Jupyter displays the graph, and graph.render("network") saves a png file.
| Parameter | Type | Default | Description |
|---|---|---|---|
collocations |
list[Collocation] |
- | Collocations |
top_n |
int |
None |
Number of pairs from the start of the list; None - all of them |
Collocations that are not a list raise SourceTypeError, no pair of two different words SourceError, and a top_n that is not an integer of at least one ParameterError.
The plots of 12 texts of the StalinWorks dataset:
from ruts import WordsExtractor
from ruts.corpus import collocations, keyness
from ruts.datasets import FreqDict, StalinWorks
from ruts.visualizers import collocation_network, dispersion_plot, keyness_plot
texts = list(StalinWorks().get_texts(limit=12))
we = WordsExtractor(use_lexemes=True, lowercase=True, filter_nums=True)
lemmas = [lemma for text in texts for lemma in we.extract(text)]
words = [word for text in texts for word in WordsExtractor(filter_nums=True).extract(text)]
dispersion_plot(lemmas, ["партия", "рабочий", "революция", "царь"])
freq_dict = FreqDict()
keyness_plot(
keyness(words, freq_dict, min_freq=5, top_n=10),
keyness(words, freq_dict, positive=False, min_freq=5, top_n=10),
labels=("тексты Сталина", "частотный словарь"),
)
graph = collocation_network(collocations(lemmas, window=3, min_freq=5, top_n=25))
graph.render("network", format="png")

