Command line¶
The command ruts counts the statistics of texts without Python code: it prints them as a table, JSON or CSV, for the whole text or its parts, writes an HTML report and downloads the dictionaries. It is installed with the library; python -m ruts is the same.
ruts download
ruts stats текст.txt
ruts stats -f json глава1.txt глава2.txt
ruts stats -s basic,readability -p sentences -f csv -o предложения.csv текст.txt
cat текст.txt | ruts stats -f json
ruts report текст.txt -t "Мой текст" -o отчет.html
Command¶
Command()
The command line of the statistics of a language library. Its command stats reads texts from files, or from the standard input without them or for -, parses every text once (parse) and prints the statistics of the classes of stats_classes:
| Option | Default | Description |
|---|---|---|
files |
the standard input | Text files |
-s, --stats |
the classes of default_stats |
Classes of statistics, by name and separated by commas; with -p, those of default_stats that count such parts by default |
-p, --parts |
- | sentences, lines or paragraphs: a row per part (by_parts); a class of -s that counts no such parts is an error |
-f, --format |
table |
table - the table of print_stats of every class, or of the parts; json - the flat statistics of every class by text, get_stats(flat=True), or the records of the parts, with nan and infinities as null; csv - a row per text, or per part of a text, with a column <class>.<statistic> for every statistic and 0 for a count of a distribution a text lacks (stats_dataframe) |
-o, --output |
the standard output | File of the output, replaced only by a result; the standard output for - |
-e, --encoding |
utf-8-sig |
Encoding of the files: UTF-8, a byte order mark left out, by default; the output is in UTF-8 |
The standard input is read as a file, its line breaks and its byte order mark alike, and a table is printed as every text is counted.
Its command download, of a library with datasets, downloads the datasets its classes need, those named or every one, again with --force, and names each on the standard output.
Its command report writes the HTML report of a text, from a file or from the standard input, to -o or to the standard output, with the classes of -s and the title of -t, --title; its statistics, those of its table of sentences too, are counted as by stats, and a class that cannot count the text is left out of it.
run(argv=None) runs the command with the arguments, those of the command line by default, and returns the exit code: 0; 1 when a file cannot be read or written, a class cannot count a text or a dataset cannot be downloaded, whose error goes to the standard error while the others are counted, and when the reader of the output stops, as head, without a message; 2 for wrong arguments, an unknown encoding among them, with the help of argparse, and without a command, with the help of the command; 130 when interrupted. -V, --version prints the versions of the library and of the core.
Raises ParameterError when a subclass is defined with texts that check_messages rejects against CLI_TEXTS, or with default_stats that names a class not in stats_classes.
In ruTS anySeg splits the text (get_document), and the classes of -s are:
| Name | Class |
|---|---|
basic |
BasicStats |
readability |
ReadabilityStats |
unadapted_readability |
UnadaptedReadabilityStats |
diversity |
DiversityStats |
morph |
MorphStats |
lexical |
LexicalStats |
style |
StyleStats |
phon |
PhonStats |
cohesion |
CohesionStats |
verse |
VerseStats, only by its name in -s |
The command has no syntactic statistics: SyntaxStats needs a Doc with a dependency parse. ruts download downloads the frequency dictionary freq2011 for lexical and the stress dictionary stress_dict for verse; without its dictionary a class is not counted, and its error names this command.
Report¶
Report(source, title=None, stats_classes=None, *, skip_errors=False)
The HTML report of the statistics of a text. The text is parsed once (parse), every class counts it and its sentences by compute, and its sections follow sections, each a method section_<name> that gives a fragment of HTML:
| Section | Content |
|---|---|
statistics |
A table per class of stats_classes under its title (titles): the description of every statistic of stats_desc, as print_stats, its value - a float with two decimals, a smaller one than 0.1 with two significant digits, a distribution as its pairs from the most frequent, with the values described by value_desc, an undefined value as the text undefined - and its reading (describe); the classes left out by skip_errors are named above |
sentences |
The text of every sentence with words, numbered as on the plot of their lengths, with the statistics of sentence_columns (by_parts); none without them. A text of more than max_sentences such sentences has those with the highest values of the first statistic, in their order |
highlight |
The text highlighted by highlight_class with its default layers, its styles in the head of the page; none without it |
plots |
The plots of plots in SVG with their captions: the lengths of the sentences (sentence_lengths_plot) by default, without a mark on every sentence for a text of more than 300 of them; none for a text without sentences |
The page is the same for the same text, and no text of the source or of the statistics becomes markup.
| Parameter | Type | Default | Description |
|---|---|---|---|
source |
str / Doc / Document |
- | Data source |
title |
str |
None |
Title of the report; the text title by default |
stats_classes |
dict[str, type[Stats]] |
None |
Classes of statistics by name; those of the class by default |
skip_errors |
bool |
False |
Leave out a class that cannot count the source, with its error in errors, instead of raising it |
The object keeps document, title, the statistics of every class by name, stats, and the errors of the classes left out, errors. to_html() returns the page, and save(path) writes it to a file in UTF-8, the encoding the page declares. A source a class cannot count raises the error of the class, unless skip_errors; a skip_errors that is not a bool and stats_classes that are not a dictionary of classes of statistics raise ParameterError.
In ruTS Report is in ruts.report: its classes are STATS_CLASSES of the same module, the classes of the command but verse; the columns of the sentences are the characters, the words, the syllables, the Flesch reading ease and the water; the highlighting is HighlightedText with its default layers.
from ruts.report import Report
report = Report(
"Кот спит на окне. Пёс громко лает во дворе.", title="Два предложения", skip_errors=True
)
report.save("отчет.html")
report.stats["basic"].n_words, len(report.document.sentences)
(9, 2)