Skip to content

Features

Used by SupervisedTermExtractor with method="features".

FeatureTermExtractor(classifier='random_forest', threshold=0.5, reference_corpus=None, n=4, random_state=None, n_jobs=None, stopwords='english', words_layer=None, lemma_layer=None, min_frequency=1, word_pattern='alphanumeric', pos_pattern=None, pos_layer=None, terms_layer=GOLD_LAYER)

Supervised term extraction with a classifier over features of the candidates, in the style of HAMLET (Rigouts Terryn, Hoste & Lefever, 2021).

The candidates are those of the unsupervised methods (see BaseTermExtractor), and each is described by:

  • termhood: its scores by the unsupervised methods in TERMHOOD_METHODS and, given a reference corpus, REFERENCE_METHODS;
  • unithood: the association measures of termxtract.association.MEASURES, each over the weakest split of the candidate into two parts, or 0 for single words;
  • frequency: its number of occurrences and the share of documents it occurs in;
  • shape: its numbers of words, characters, digits, hyphens and other characters that are neither letters, digits nor spaces, and, for Teanga input, the share of its occurrences written in lower case, upper case and title case;
  • part of speech, for Teanga input with a part-of-speech layer: the tag of its first and last word, as one-hot vectors, and the number of its words with each tag, as HAMLET encodes them. A candidate's tags are those of its most frequent tag sequence, and the tags are those seen in training.

The termhood, unithood and frequency features depend on the size of the corpus, so they are converted to their percentile rank among the candidates of the corpus, which lets a model trained on one corpus score the candidates of another.

The classifier is scikit-learn's random forest by default, as in HAMLET, which found it better than a decision tree, logistic regression or a multi-layer perceptron. Terms are a minority of the candidates, so its classes are weighted by their frequency in each tree's sample (class_weight="balanced_subsample"), and each leaf has at least 5 candidates (min_samples_leaf=5). Of the settings tried, these classified best (F1 0.39, against 0.28 for scikit-learn's defaults) when training on two of ACTER's English training domains and testing on the third. Each candidate is scored by the probability the classifier gives it of being a term, over the whole corpus, so a term has the same score in every document it occurs in.

Reference

Rigouts Terryn, A., Hoste, V. and Lefever, E. (2021). HAMLET: Hybrid Adaptable Machine Learning approach to Extract Terminology. Terminology 27(2), 254–293. https://doi.org/10.1075/term.20017.rig

Initialize the extractor.

Parameters:

Name Type Description Default
classifier Union[str, Any]

"random_forest" (the default) for scikit-learn's RandomForestClassifier, or an unfitted scikit-learn classifier with predict_proba, which is cloned before training.

'random_forest'
threshold Optional[float]

Keep the candidates that the classifier gives at least this probability of being a term: 0.5 (the default) keeps those it classifies as terms, and None ranks every candidate.

0.5
reference_corpus Optional[Union[Corpus, List[str], List[...]]]

A general-language reference corpus, of the same type as the corpora passed to fit and predict, to add the scores of the methods of REFERENCE_METHODS as features, or None.

None
n int

Maximum n-gram size of the candidates.

4
random_state Optional[int]

Seed of the random forest, for reproducible scores.

None
n_jobs Optional[int]

Number of trees of the random forest trained in parallel, as in joblib: -1 for one per CPU, or None for one at a time.

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

Drop candidates that start or end with a stopword: a name in STOPWORD_LISTS, a list of words, or None.

'english'
words_layer Optional[str]

For Teanga input, the span layer holding the words, or None for words or, failing that, tokens.

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 candidate, given by its lemmas, or None to use the words as written. Labels and gold terms are matched to the lemmas.

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, as for TermExtractor; "learned" for the tag sequences of the occurrences of the terms in the training corpus, as HAMLET does; or None (the default) to allow every tag sequence.

None
pos_layer Optional[str]

The part-of-speech layer, or None for upos or, failing that, pos.

None
terms_layer str

For Teanga input without labels, the span layer of gold terms to train on.

GOLD_LAYER

extractors = {method: TermExtractor(method=method, n=n, stopwords=stopwords, words_layer=words_layer, lemma_layer=lemma_layer, min_frequency=min_frequency, word_pattern=word_pattern, pos_pattern=None if self.learns_pos_pattern else pos_pattern, pos_layer=pos_layer, reference_corpus=reference_corpus).extractor for method in methods} instance-attribute

The unsupervised extractors whose scores are features, which also find the candidates.

learn_pos_pattern(corpus, terms)

Learn the tag sequences of the terms, and count only the occurrences of candidates with one of them from then on.

Parameters:

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

The training corpus.

required
terms Set[str]

The terms, normalised by normalize_term.

required

Returns:

Type Description
Pattern[str]

re.Pattern: A pattern matching the tag sequences of the occurrences of the

Pattern[str]

terms of up to n words in the corpus.

Raises:

Type Description
ValueError

If the corpus has no part-of-speech layer.

features(corpus, candidates, training)

Compute the features of the candidates of a corpus.

Parameters:

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

The corpus.

required
candidates _Candidates

The occurrences of its candidates, from _candidates.

required
training bool

Whether the corpus is the training corpus, whose tags are the part-of-speech features.

required

Returns:

Type Description
ndarray

The feature matrix, with one row per candidate and one column per feature,

list[str]

the candidates, in order of first occurrence, and the names of the

list[str]

features.

train(corpus, labels)

Train the classifier on the labelled candidates of a corpus.

Labels are matched to the candidates after lower-casing. If none is 0, as with a list of gold terms, every candidate is an example, and those that are not labelled 1 are the non-terms. Otherwise only the labelled candidates are.

Parameters:

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

The training corpus.

required
labels Dict[str, int]

The label, 1 for a term or 0 for a non-term, of candidates.

required

Raises:

Type Description
ValueError

If no candidate is labelled 1, or every one is.

fit_strings(corpus, labels)

Train the classifier on a list of strings.

fit_teanga(corpus, labels=None)

Train the classifier on a Teanga corpus, with the gold terms of terms_layer as the terms if labels is None.

predict_scores(corpus, candidates)

Score the candidates of a corpus by the probability the classifier gives them of being terms.

Raises:

Type Description
ValueError

If the classifier has not been trained, or the corpus does not have the features it was trained on: a Teanga corpus with a part-of-speech layer, if it was trained on one.

predict_strings(corpus)

Score the candidates of a list of strings.

predict_teanga(corpus)

Score the candidates of a Teanga corpus, with their offsets.

feature_importances()

Get the importance of each feature to the trained classifier.

Returns:

Type Description
dict[str, float]

Dict[str, float]: The feature_importances_ of the classifier, by feature

dict[str, float]

name, from the most to the least important.

Raises:

Type Description
ValueError

If the classifier has not been trained, or has no feature_importances_, as with logistic regression.