Frequency lists
Examples: Word frequencies
frequency_list
frequency_list(
corpus: T_Frame,
expr: IntoExpr = "token",
basis: float = 1000000,
file_id_column: Optional[str] = DEFAULT_FILE_ID,
) -> T_Frame
Count how often each word occurs in a corpus.
A corpus holds one row per token, an occurrence of a word. A frequency
list holds one row per type, a distinct form, with the number of tokens of
it. Raw counts can only be compared within one corpus, so the count is
also reported as a rate per basis words, and the number of files the
word occurs in says whether the count comes from across the corpus or from
a few texts.
Parameters:
-
corpus(DataFrame | LazyFrame) –Corpus to count.
-
expr(IntoExpr, default:"token") –Column name or expression identifying the word/type to count (e.g. token or lemma).
-
basis(float, default:1_000_000) –Number of words
rateis reported per. Per million is usual for a large corpus; pass 10_000 for a small one. -
file_id_column(str, default:DEFAULT_FILE_ID) –Column holding file ids, counted to give each word its
range. Pass None for norangecolumn. Defaults to "file_id", which is dropped rather than demanded when the corpus has no such column.
Returns:
-
DataFrame | LazyFrame–One row per type, most frequent first, ties broken by
exprascending:- the word, in a column named for whatever
exprproduces freq: number of tokens of itrate:freqas a share of the corpus, timesbasisrange: number of distinct files it occurs in, absent when there is no file id column
Eager if
corpusis a DataFrame, lazy if it is a LazyFrame. - the word, in a column named for whatever
Raises:
-
ValueError–If
corpusis not a Polars DataFrame or LazyFrame, is empty, or is missing a columnfrequency_listneeds; ifexpris not a column name or expression; or ifbasisis not a positive number.
See Also
dispersion : How evenly a word is spread over the files, past counting them. keywords : Which words are more frequent here than in another corpus.
Notes
Rows holding a null in either expr or file_id_column are dropped.
Normalizing is expr's job. Folding case is
pl.col("token").str.to_lowercase(), and a token expr evaluates to null
is dropped, so restricting what counts as a word is a filter on the
corpus or a when/then in expr. The Examples show both.
rate divides by the tokens that were counted, so whatever normalizing
dropped is gone from the total too. Filtering the result afterwards does
not rescale it: dropping the rare words leaves the common ones with the
rates they had in the whole corpus.
Examples:
>>> import polars_corpus as plc
>>> plc.frequency_list(corpus, "token")
>>> # Counting words rather than tokens: letters only, and case folded.
>>> plc.frequency_list(
... corpus.filter(plc.is_letters("token")),
... pl.col("token").str.to_lowercase(),
... )
>>> # Lemmas rather than word forms, per 10,000 words:
>>> plc.frequency_list(corpus, "lemma", basis=10_000)
>>> # Drop the words that are rare, or that come from a handful of texts:
>>> plc.frequency_list(corpus, "token").filter(
... pl.col("freq") >= 10, pl.col("range") >= 5
... )
Source code in python/polars_corpus/frequency.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |