Skip to content

TermExtractor

Entry point for the unsupervised methods. Choose a method with method=...; see the unsupervised extractors for what each one does.

TermExtractor(method='tfidf', alpha=0.5, beta=0.5, threshold=None, n=None, num_topics=20, top_words=200, window_size=None, seed_terms=200, context_words=50, top_terms=200, cvalue_weight=0.8, stoplist=None, phrase_delimiters=None, reference_corpus=None, tie_break='ratio', n_topics=10, max_features=None, aggregation='min', dedup_threshold=0.9, corpus_aggregation='max', variant=None, graph='corpus', model_name=DEFAULT_MODEL, encoder=None, diversity=None, stopwords='english', words_layer=None, lemma_layer=None, min_frequency=1, word_pattern='alphanumeric', pos_pattern=None, pos_layer=None, rerank=None)

A wrapper class for selecting the term extraction method.

TermExtractor.METHODS maps each method name to its extractor class and the parameters passed to it, so list(TermExtractor.METHODS) lists the available methods.

Initialize the TermExtractor with the desired method and parameters.

Each method only uses some of the parameters; the others are ignored.

Parameters:

Name Type Description Default
method str

The term extraction method, one of the keys of TermExtractor.METHODS (e.g. 'tfidf', 'cvalue', 'rake', 'weirdness').

'tfidf'
alpha float

Weight for ( e_t ) ('basic' and 'combobasic').

0.5
beta float

Weight for ( e'_t ) ('combobasic').

0.5
threshold Optional[float]

Minimum score threshold for term inclusion.

None
n Optional[int]

Maximum length of n-grams to consider (all methods except 'rake'), or None for the method's default: 2 for the unithood methods ('pmi', 'llr', 'chi2', 'dice' and 'tscore'), which rank only multi-word terms, 3 for 'yake', 'textrank' and 'embedrank', and 1 for the others.

None
num_topics int

Number of LDA topics ('topicmodeling').

20
top_words int

Number of most probable words of each topic that count towards the scores ('topicmodeling').

200
window_size Optional[int]

The number of words on either side of a candidate that count as its context ('domaincoherence', 'ncvalue' and 'yake'), or of a word that it is linked to in the graph ('textrank'), or None for the method's default: 1 for 'yake' and TextRank, 9 for SingleRank and PositionRank, and 5 for the others.

None
seed_terms int

The number of best candidates by Basic used to choose the context words ('domaincoherence').

200
context_words int

The number of context words that candidates are scored against ('domaincoherence').

50
top_terms int

The number of best candidates by C-value whose context words are weighted ('ncvalue').

200
cvalue_weight float

The weight of the C-value against the context factor ('ncvalue'), 0.8 in Frantzi et al. (2000).

0.8
stoplist Optional[List[str]]

List of stop words (required by 'rake').

None
phrase_delimiters Optional[List[str]]

List of phrase delimiters (required by 'rake').

None
reference_corpus Optional[Union[Corpus, List[str], List[Union[Corpus, List[str]]]]]

General-language reference corpus, required by 'domainpertinence', 'weirdness' and 'relevance'. It must have the same type as the corpus passed to extract. A list of reference corpora is compared one by one by 'domainpertinence', and read as one corpus by the others.

None
tie_break Optional[str]

How to order the candidates that tie at 1 ('domainpertinence'): "ratio", "frequency" or None.

'ratio'
n_topics int

Number of NMF topics ('nmf').

10
max_features Optional[int]

Maximum vocabulary size ('nmf').

None
aggregation str

How the unithood methods combine the scores of the splits of a candidate of three or more words into two parts: "min" (the default) for its weakest split, or "mean".

'min'
dedup_threshold Optional[float]

Drop a candidate that is more similar than this to a better one in the same document ('yake'), or None.

0.9
corpus_aggregation Optional[str]

How a term's scores in each document are combined ('yake' and 'embedrank'): "max" (the default) for its best, "mean", "centroid" for its similarity to the mean document embedding ('embedrank' with the "embedrank" variant), or None to keep the score of each document.

'max'
variant Optional[str]

The variant of 'textrank', "plain" (the default) for TextRank, "single" for SingleRank or "position" for PositionRank, or of 'embedrank', "embedrank" (the default) or "mderank"; None for the method's default.

None
graph str

Build one graph over the whole corpus ("corpus", the default) or one for each document ("document") ('textrank').

'corpus'
model_name str

The Hugging Face model of the sentence encoder ('embedrank'), by default all-MiniLM-L6-v2.

DEFAULT_MODEL
encoder Optional[Encoder]

A loaded termxtract.embeddings.Encoder, used instead of model_name ('embedrank').

None
diversity Optional[float]

Re-rank the candidates by Maximal Marginal Relevance, as in EmbedRank++, with this weight from 0 to 1 on their similarity to better candidates ('embedrank' with the "embedrank" variant), or None.

None
stopwords Union[str, Iterable[str], None]

Drop candidates that start or end with a stopword (all methods except 'rake', which uses stoplist): the name of a built-in list in termxtract.stopwords.STOPWORD_LISTS, such as "english" (the default) or an ISO 639-1 code such as "fr", a list of words, or None to keep every candidate. Stopwords inside a multi-word term, as in "degree of freedom", are kept.

'english'
words_layer Optional[str]

For Teanga input, the span layer holding the words: None (the default) for words or, if the corpus has no words layer, tokens.

None
lemma_layer Optional[str]

For Teanga input, a sequence layer, based on the words layer, holding the lemma of each word (all methods except 'rake'). If given, words are read as their lemmas, so inflected forms such as "cell" and "cells" are counted, and returned, as one term. None (the default) reads the words as written.

None
min_frequency int

Drop candidates that occur fewer times than this in the corpus (all methods except 'rake'). ATR4S uses 2. The default of 1 keeps every candidate.

1
word_pattern Optional[str]

Drop candidates with a word that does not fully match this regular expression (all methods except 'rake'), or the name of a pattern in termxtract.candidates.WORD_PATTERNS: "alphanumeric" (the default) drops words without a letter or digit, such as "%", and "atr4s" keeps words of three or more letters, digits and hyphens, as ATR4S's noise filter does. None keeps every word.

'alphanumeric'
pos_pattern Optional[str]

For Teanga input, count only the occurrences of candidates whose part-of-speech tags match this pattern, so a term must match it in at least one occurrence. Tags are written in angle brackets, as in "<ADJ|NOUN>*<NOUN>", or use the name of a pattern in termxtract.pos.POS_PATTERNS, such as "noun_phrase". None (the default) allows every tag sequence. RAKE's terms are filtered after scoring instead, so its scores are unchanged.

None
pos_layer Optional[str]

The part-of-speech layer used by pos_pattern, based on the words layer: None (the default) for upos or, if the corpus has no upos layer, pos.

None
rerank Optional[Reranker]

Revise the scores of the terms by their words' place in a graph of semantically related words (all methods): a termxtract.rerank.AdaText or termxtract.rerank.SemReRank, or None (the default) to keep the method's scores.

None

Raises:

Type Description
ValueError

If the method is unknown, or a parameter it requires is missing.

extract(corpus)

Extract terms from a given corpus.

Parameters:

Name Type Description Default
corpus Union[Corpus, List[str]]

Either a Teanga Corpus object or a list of strings (target corpus).

required

Returns:

Name Type Description
ATEResults ATEResults

An object containing the corpus, terms, and offsets (if applicable).

Raises:

Type Description
ValueError

If the corpus type is not supported or if a required reference corpus is missing.