Unithood measures
Used by TermExtractor with method="pmi", "llr", "chi2", "dice" or "tscore". They rank only multi-word terms, so n defaults to 2.
UnithoodTermExtractor(aggregation='min', threshold=None, n=2, stopwords='english', words_layer=None, lemma_layer=None, min_frequency=1, word_pattern='alphanumeric', pos_pattern=None, pos_layer=None)
Bases: BaseTermExtractor
Base class for extractors that rank multi-word candidates by an association (unithood) measure, which scores how strongly their words stick together rather than how specific they are to the domain.
A candidate of k words can be split in k - 1 places into two parts, its first words
and the rest, and the measure of each split is computed from a contingency table of
the k-word sequences of the corpus (see termxtract.association.NgramCounts). The
scores of the splits are combined by aggregation. Single words have no parts to
associate, so they are not ranked. The tables count every sequence of words, not
only the candidates, so the filters of BaseTermExtractor change which candidates
are ranked but not their scores. Scores are computed over the whole corpus, so a
term has the same score in every document it occurs in.
Subclasses set measure to the name of a measure in
termxtract.association.MEASURES.
Initialize the extractor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregation
|
str
|
How to combine the scores of the splits of a candidate of three or more words: "min" (the default) scores it by its weakest split, and "mean" by the mean of its splits. |
'min'
|
threshold
|
Optional[float]
|
Minimum score for term inclusion. |
None
|
n
|
int
|
Maximum n-gram size, at least 2. |
2
|
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. |
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 |
corpus_scores(docs, corpus_ngrams)
Score the multi-word candidates of a corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
List[List[str]]
|
The words of each document. |
required |
corpus_ngrams
|
List[List[str]]
|
The occurrences of candidates in each
document, from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict[str, float]: The score of each candidate of two or more words. |
extract_terms_teanga(corpus)
Extract terms from a Teanga corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
Corpus
|
A Teanga Corpus object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores, and term offsets. |
extract_terms_strings(corpus)
Extract terms from a plain list of strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
List[str]
|
List of documents as strings. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores. |
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. |
PMITermExtractor(aggregation='min', threshold=None, n=2, stopwords='english', words_layer=None, lemma_layer=None, min_frequency=1, word_pattern='alphanumeric', pos_pattern=None, pos_layer=None)
Bases: UnithoodTermExtractor
Pointwise mutual information (Church and Hanks, 1990): ( \log_2 \frac{P(x y)}{P(x) P(y)} ). It favours rare candidates whose words seldom occur apart.
Initialize the extractor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregation
|
str
|
How to combine the scores of the splits of a candidate of three or more words: "min" (the default) scores it by its weakest split, and "mean" by the mean of its splits. |
'min'
|
threshold
|
Optional[float]
|
Minimum score for term inclusion. |
None
|
n
|
int
|
Maximum n-gram size, at least 2. |
2
|
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. |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
Corpus
|
A Teanga Corpus object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores, and term offsets. |
extract_terms_strings(corpus)
Extract terms from a plain list of strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
List[str]
|
List of documents as strings. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores. |
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. |
corpus_scores(docs, corpus_ngrams)
Score the multi-word candidates of a corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
List[List[str]]
|
The words of each document. |
required |
corpus_ngrams
|
List[List[str]]
|
The occurrences of candidates in each
document, from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict[str, float]: The score of each candidate of two or more words. |
LLRTermExtractor(aggregation='min', threshold=None, n=2, stopwords='english', words_layer=None, lemma_layer=None, min_frequency=1, word_pattern='alphanumeric', pos_pattern=None, pos_layer=None)
Bases: UnithoodTermExtractor
Dunning's (1993) log-likelihood ratio of the words occurring together by chance.
Initialize the extractor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregation
|
str
|
How to combine the scores of the splits of a candidate of three or more words: "min" (the default) scores it by its weakest split, and "mean" by the mean of its splits. |
'min'
|
threshold
|
Optional[float]
|
Minimum score for term inclusion. |
None
|
n
|
int
|
Maximum n-gram size, at least 2. |
2
|
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. |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
Corpus
|
A Teanga Corpus object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores, and term offsets. |
extract_terms_strings(corpus)
Extract terms from a plain list of strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
List[str]
|
List of documents as strings. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores. |
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. |
corpus_scores(docs, corpus_ngrams)
Score the multi-word candidates of a corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
List[List[str]]
|
The words of each document. |
required |
corpus_ngrams
|
List[List[str]]
|
The occurrences of candidates in each
document, from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict[str, float]: The score of each candidate of two or more words. |
Chi2TermExtractor(aggregation='min', threshold=None, n=2, stopwords='english', words_layer=None, lemma_layer=None, min_frequency=1, word_pattern='alphanumeric', pos_pattern=None, pos_layer=None)
Bases: UnithoodTermExtractor
Pearson's χ² statistic of the contingency table (Manning and Schütze, 1999).
Initialize the extractor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregation
|
str
|
How to combine the scores of the splits of a candidate of three or more words: "min" (the default) scores it by its weakest split, and "mean" by the mean of its splits. |
'min'
|
threshold
|
Optional[float]
|
Minimum score for term inclusion. |
None
|
n
|
int
|
Maximum n-gram size, at least 2. |
2
|
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. |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
Corpus
|
A Teanga Corpus object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores, and term offsets. |
extract_terms_strings(corpus)
Extract terms from a plain list of strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
List[str]
|
List of documents as strings. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores. |
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. |
corpus_scores(docs, corpus_ngrams)
Score the multi-word candidates of a corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
List[List[str]]
|
The words of each document. |
required |
corpus_ngrams
|
List[List[str]]
|
The occurrences of candidates in each
document, from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict[str, float]: The score of each candidate of two or more words. |
DiceTermExtractor(aggregation='min', threshold=None, n=2, stopwords='english', words_layer=None, lemma_layer=None, min_frequency=1, word_pattern='alphanumeric', pos_pattern=None, pos_layer=None)
Bases: UnithoodTermExtractor
The Dice coefficient (Smadja et al., 1996): twice the frequency of the candidate over the sum of the frequencies of its two parts.
Initialize the extractor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregation
|
str
|
How to combine the scores of the splits of a candidate of three or more words: "min" (the default) scores it by its weakest split, and "mean" by the mean of its splits. |
'min'
|
threshold
|
Optional[float]
|
Minimum score for term inclusion. |
None
|
n
|
int
|
Maximum n-gram size, at least 2. |
2
|
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. |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
Corpus
|
A Teanga Corpus object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores, and term offsets. |
extract_terms_strings(corpus)
Extract terms from a plain list of strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
List[str]
|
List of documents as strings. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores. |
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. |
corpus_scores(docs, corpus_ngrams)
Score the multi-word candidates of a corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
List[List[str]]
|
The words of each document. |
required |
corpus_ngrams
|
List[List[str]]
|
The occurrences of candidates in each
document, from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict[str, float]: The score of each candidate of two or more words. |
TScoreTermExtractor(aggregation='min', threshold=None, n=2, stopwords='english', words_layer=None, lemma_layer=None, min_frequency=1, word_pattern='alphanumeric', pos_pattern=None, pos_layer=None)
Bases: UnithoodTermExtractor
Student's t-score of the candidate's frequency against its frequency if its parts were independent (Manning and Schütze, 1999).
Initialize the extractor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregation
|
str
|
How to combine the scores of the splits of a candidate of three or more words: "min" (the default) scores it by its weakest split, and "mean" by the mean of its splits. |
'min'
|
threshold
|
Optional[float]
|
Minimum score for term inclusion. |
None
|
n
|
int
|
Maximum n-gram size, at least 2. |
2
|
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. |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
Corpus
|
A Teanga Corpus object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores, and term offsets. |
extract_terms_strings(corpus)
Extract terms from a plain list of strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
List[str]
|
List of documents as strings. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ATEResults |
ATEResults
|
Results with ranked terms and scores. |
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. |
corpus_scores(docs, corpus_ngrams)
Score the multi-word candidates of a corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
List[List[str]]
|
The words of each document. |
required |
corpus_ngrams
|
List[List[str]]
|
The occurrences of candidates in each
document, from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict[str, float]: The score of each candidate of two or more words. |
Association measures
The measures are shared with Roger, which uses them as features.
Association (unithood) measures, which score how strongly the words of an n-gram stick together, from the contingency table of a split of the n-gram into two parts.
MEASURES = ('MI', 'MI3', 'Dice', 'L', 'OccL', 'SeSc', 'J', 'Conv', 'LC', 'CM', 'Khi2', 'T test')
module-attribute
Names of the association measures used as features, in order.
NgramCounts(docs, n)
The contingency tables of the binary splits of the n-grams of a corpus.
Every sequence of 2 to n words in the documents is counted. A k-word n-gram can be
split after its i-th word into x, its first i words, and y, the rest. The
table of that split counts the k-word sequences of the corpus: how many there are,
how many are the n-gram, how many start with x and how many end with y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
list[list[str]]
|
The words of each document. |
required |
n
|
int
|
The maximum length of the n-grams. |
required |
ngrams = Counter(tuple(words[i:i + k]) for words in docs for k in range(2, n + 1) for i in range(len(words) - k + 1))
instance-attribute
The number of occurrences of each n-gram, in order of first occurrence.
table(ngram, i)
Get the contingency table of a split of an n-gram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ngram
|
tuple[str, ...]
|
The words of an n-gram of the corpus. |
required |
i
|
int
|
The number of words before the split, from 1 to |
required |
Returns:
| Type | Description |
|---|---|
int
|
tuple[int, int, int, int]: The arguments |
int
|
|
measures(ngram, i)
Compute the association measures of a split of an n-gram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ngram
|
tuple[str, ...]
|
The words of an n-gram of the corpus. |
required |
i
|
int
|
The number of words before the split, from 1 to |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
list[float]: The measures named in |
association_measures(a, n_x, n_y, total)
Compute the association measures of a collocation x y from its contingency table.
The table counts the total positions of collocations of the same length: a of
them are x y, n_x start with x and n_y end with y. The measures are those
of Azé et al. (2005), section 2, without Jacquemin's association measure, whose
definition is not given there. Rule-based measures (SeSc, J, Conv, LC, CM) treat the
collocation as the rule x → y.
The rule-based measures divide by the number of counter-examples b, the positions
starting with x but not ending with y. When x is always followed by y, b
is 0 and these measures are infinite, so b is then taken to be 0.5.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
Occurrences of the collocation |
required |
n_x
|
int
|
Positions starting with |
required |
n_y
|
int
|
Positions ending with |
required |
total
|
int
|
Positions of collocations of this length. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
list[float]: The measures named in |