YAKE!
Used by TermExtractor with method="yake".
YAKETermExtractor(threshold=None, n=3, window_size=1, dedup_threshold=0.9, corpus_aggregation='max', stopwords='english', words_layer=None, lemma_layer=None, min_frequency=1, word_pattern='alphanumeric', pos_pattern=None, pos_layer=None)
Bases: BaseTermExtractor
YAKE! (Campos et al., 2020), which scores the candidates of each document from local features of their words, with no corpus or training data.
The words are counted case-insensitively. Each word ( t ) of a document gets five features:
- casing, ( T_{case} = \max(TF_U(t), TF_A(t)) / (1 + \ln TF(t)) ), where ( TF_A ) counts its occurrences in capitals and ( TF_U ) those whose only capital is the first letter, other than at the start of a sentence;
- position, ( T_{pos} = \ln(\ln(3 + \mathrm{median}(S_t))) ), where ( S_t ) is the set of indices of the sentences it occurs in;
- frequency, ( T_{freq} = TF(t) / (\mu + \sigma) ), where ( \mu ) and ( \sigma ) are the mean and standard deviation of the frequencies of the words that are not stopwords;
- relatedness to its context, ( T_{rel} = 1 + (D_L + D_R) \cdot TF(t) /
\max TF ), where ( \max TF ) is the highest frequency of any word and
( D_L ) is the number of different words within
window_sizewords on its left divided by the number of words there, and ( D_R ) the same on its right. Words that don't matchword_patterncount in no word's context; - the share of the sentences it occurs in, ( T_{sent} ).
Together they score ( S(t) = T_{rel} T_{pos} / (T_{case} + (T_{freq} + T_{sent}) / T_{rel}) ), where lower is better. A candidate ( k ) scores ( S(k) = \prod_{t \in k} S(t) / (TF(k) (1 + \sum_{t \in k} S(t))) ). A stopword inside a candidate, as in "degree of freedom", multiplies the product by ( 2 - P ) and lowers the sum by ( 1 - P ), where ( P ) is the probability of the word before it being followed by it, times the probability of it being followed by the word after it.
Going down a document's ranking, a candidate is dropped if it is more similar than
dedup_threshold to one already kept, by is_near_duplicate. The terms are
reported in lower case, scored ( -S(k) ) so that higher is better.
Sentences end at the punctuation of SENTENCE_END, such as ".", and a candidate or
context window does not cross other punctuation either, whether it is between the
words or a word itself.
Given the same words, stopwords and lemmas, and a word_pattern that rejects the
numbers and the words mixing letters and digits that yake sets aside, the scores
are those of the yake package (version 0.7.3, with dedup_func="levs"), except that lemma_layer also
merges the candidates whose words share lemmas, where yake merges only the words.
yake reads words ending in "s" as plurals, and words of one or two letters as
stopwords; use lemma_layer and stopwords for that here.
Initialize the YAKE! extractor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
Optional[float]
|
Minimum score for term inclusion. |
None
|
n
|
int
|
Maximum n-gram size for candidate terms. |
3
|
window_size
|
int
|
The number of words on either side of a word that count as its context. |
1
|
dedup_threshold
|
Optional[float]
|
Drop a candidate that is more similar than this to a better one in the same document, or None to keep every candidate. |
0.9
|
corpus_aggregation
|
Optional[str]
|
How a term's scores in the documents it occurs in are combined into one score, which it gets in all of them: "max" (the default) for its best score, "mean" for the mean, or None to keep the score of each document. |
'max'
|
stopwords
|
Union[str, Iterable[str], None]
|
Drop candidates that start or end
with a stopword: a name in |
'english'
|
words_layer
|
Optional[str]
|
For Teanga input, the span layer holding the
words, or None for |
None
|
lemma_layer
|
Optional[str]
|
For Teanga input, a sequence layer holding the lemma of each word, to count inflected forms such as "cell" and "cells" as one term, or None to use the words as written. The casing feature still reads the words as written. |
None
|
min_frequency
|
int
|
Drop candidates that occur fewer times in the corpus. |
1
|
word_pattern
|
Optional[str]
|
Drop candidates with a word that does not fully match this regular expression or named pattern ("alphanumeric", "atr4s"), or None to keep every word. |
'alphanumeric'
|
pos_pattern
|
Optional[str]
|
For Teanga input, count only the occurrences whose part-of-speech tags match this pattern, or None. |
None
|
pos_layer
|
Optional[str]
|
The part-of-speech layer for |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
extract_terms_teanga(corpus)
Extract terms from a Teanga corpus using YAKE!.
extract_terms_strings(corpus)
Extract terms from a plain list of strings using YAKE!.
document_scores(doc, occurrences)
Score the candidates of one document by YAKE!, where lower is better.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
SplitDocument
|
The document. |
required |
occurrences
|
List[Tuple[str, int, int]]
|
The occurrences of the candidates in the document, by their start and end (exclusive) word index. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict[str, float]: The YAKE! score of each candidate that is not dropped as |
dict[str, float]
|
a near-duplicate. |
teanga_words(doc)
Get the words of a Teanga document with their offsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Document
|
A Teanga document with a words layer (see |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[int, int, str]]
|
List[Tuple[int, int, str]]: A (start, end, text) tuple for each word, where |
list[tuple[int, int, str]]
|
text is the word's lemma if |
candidate_ngrams_teanga(corpus)
Find the occurrences of the candidates in a Teanga corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
Corpus
|
A Teanga corpus with a words layer (see |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[tuple[str, tuple[int, int]]]]
|
Dict[str, List[Tuple[str, Tuple[int, int]]]]: For each document ID, every |
dict[str, list[tuple[str, tuple[int, int]]]]
|
occurrence of a candidate, with its start and end character offsets. |
candidate_ngrams_strings(docs)
Find the occurrences of the candidates in documents split into words.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
List[List[str]]
|
The words of each document. |
required |
Returns:
| Type | Description |
|---|---|
list[list[str]]
|
List[List[str]]: Every occurrence of a candidate in each document. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
candidate_occurrences(docs, tags=None)
Find the occurrences of the candidates in documents split into words.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
List[List[str]]
|
The words of each document. |
required |
tags
|
Optional[List[List[str]]]
|
The part-of-speech tag of each word of each
document, needed if |
None
|
Returns:
| Type | Description |
|---|---|
list[list[tuple[str, int, int]]]
|
List[List[Tuple[str, int, int]]]: Every occurrence of a candidate in each |
list[list[tuple[str, int, int]]]
|
document, with its start and end (exclusive) word index. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
frequent_candidates(ngrams)
Find the candidates that occur at least min_frequency times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ngrams
|
Iterable[str]
|
Every occurrence of a candidate in the corpus. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Set[str]: The candidates that occur often enough. |
generate_ngrams_teanga(words_with_offsets)
Generate n-grams with offsets for a Teanga corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
words_with_offsets
|
List[Tuple[int, int, str]]
|
List of (start, end, text) tuples for words. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, tuple[int, int]]]
|
List[Tuple[str, Tuple[int, int]]]: List of n-grams with their start and end offsets. |
generate_ngrams_strings(words)
Generate n-grams for a plain list of strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
words
|
List[str]
|
List of words in the document. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List[str]: List of n-grams. |
compute_tf(ngrams, total=None)
Compute the relative frequency of each n-gram in a document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ngrams
|
List[str]
|
The n-grams in the document. |
required |
total
|
Optional[int]
|
The number to divide the counts by, or None for the
number of |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict[str, float]: Count of each n-gram divided by the total number of n-grams. |
ngram_total(num_words)
Count the n-grams of up to n words in a document, candidates or not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_words
|
int
|
The number of words of the document. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of n-grams. |
compute_idf(corpus_ngrams)
Compute the inverse document frequency of each n-gram as log(N / (1 + df)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus_ngrams
|
List[List[str]]
|
The n-grams of each document. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict[str, float]: IDF score for each n-gram, where N is the number of documents |
dict[str, float]
|
and df is the number of documents containing the n-gram. |
doc_terms(doc_id, scores)
Build one document's entry in ATEResults.terms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc_id
|
str
|
The document ID. |
required |
scores
|
Dict[str, float]
|
The score of each candidate term. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict[str, Any]: |
dict[str, Any]
|
terms below the threshold or starting or ending with a stopword removed, and the rest ranked from highest to lowest score. |
doc_offsets(doc_id, ngrams_with_offsets, doc_terms)
staticmethod
Build one document's entry in ATEResults.offsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc_id
|
str
|
The document ID. |
required |
ngrams_with_offsets
|
Iterable[Tuple[str, Tuple[int, int]]]
|
Every n-gram occurrence in the document with its (start, end) character offsets. |
required |
doc_terms
|
Dict[str, Any]
|
The document's entry in |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict[str, Any]: |
dict[str, Any]
|
listing each occurrence of an extracted term in document order. |
is_near_duplicate(a, b, threshold)
Check whether two strings are more similar than a threshold.
The similarity is ( 1 - d / l ), where ( d ) is the Levenshtein distance and ( l ) the length of the longer string. Only distances up to the largest one that passes the threshold are computed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
str
|
A string. |
required |
b
|
str
|
Another string. |
required |
threshold
|
float
|
The similarity to exceed. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Whether the similarity of |