cds2.text
Lightweight text analysis: TF-IDF, similarity, summaries.
Lightweight text analysis: tokenization, TF-IDF weighting and similarities.
TfidfResult
dataclass
TF-IDF matrix together with the vocabulary indexing its columns.
Source code in src/cds2/text.py
42 43 44 45 46 47 | |
SummaryResult
dataclass
Highest scoring terms according to mean TF-IDF weight.
Source code in src/cds2/text.py
99 100 101 102 103 | |
tokenize(text)
Lowercase alphanumeric word tokens of text (punctuation dropped).
Source code in src/cds2/text.py
29 30 31 | |
term_frequencies(tokens)
Occurrence count of every distinct token.
Source code in src/cds2/text.py
34 35 36 37 38 39 | |
tfidf_matrix(documents)
Smoothed TF-IDF matrix (rows = documents, columns = sorted vocabulary).
Term frequency is count / len(doc_tokens) (zero for token-less
documents); inverse document frequency uses the smoothed form
ln((1 + N) / (1 + df)) + 1.
Source code in src/cds2/text.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
cosine_similarity(a, b)
Cosine of the angle between two vectors; 0.0 when a norm vanishes.
Source code in src/cds2/text.py
76 77 78 79 80 81 82 83 84 85 86 | |
jaccard_similarity(text_a, text_b)
Jaccard similarity of the token sets; empty unions score 0.0.
Source code in src/cds2/text.py
89 90 91 92 93 94 95 96 | |
summarize_terms(documents, top_k=10)
Mean TF-IDF score per term across documents, best top_k first.
Source code in src/cds2/text.py
106 107 108 109 110 111 112 113 114 115 116 117 | |