Data Augmentation
Synthetic training sentences for the supervised methods, written by a large language
model or with a Wikipedia lexicon, as in Banerjee (2025, ch. 5). The LLM
methods need a client such as OpenAIClient from the LLM clients.
augmentation
Synthetic training data for supervised term extraction, as in Banerjee (2025, ch. 5): a large language model or a lexicon from Wikipedia writes new sentences from a sample of the gold sentences, and the terms of the new sentences are their annotations.
METHODS = ('term', 'context', 'combined', 'wiki')
module-attribute
The augmentation methods: LLM-TermDA, LLM-ContextDA, LLM-CombinedDA and Wiki-TermDA.
SAMPLINGS = ('diversity', 'random')
module-attribute
The ways of choosing the gold sentences to augment.
PROMPTS = {'combined': "Given these sentences where concepts related to {domain} are tagged by <tag>, generate {k} sentences while minimizing data distribution shift. Make sure that the sentences are diverse and different from those provided below. Please make sure that you don't use any terms from the provided sentences. Annotate the concepts belonging to the domain of {domain} with <tag> in the generated sentences:\n{sentences}", 'term': 'Given below are sentences in which concepts related to {domain} are tagged by <tag>, replace the annotated with different concepts that are related to {domain}. While replacing the concepts be careful to not disturb the syntactic and semantic coherence of the input. Please generate {k} diverse sentences and make sure all generated sentences are in lower case:\n{sentences}', 'context': 'Given a list of terms that occur in the sentences below:\n{terms}\nSentences are:\n{sentences}\nGiven these terms and the sentences, generate {k} sentences that use these terms. The terms are tagged with <tag>. In the generated sentences tagging is not required. All sentences in lower case, make sure that each sentence on a new line and the list is not numbered.'}
module-attribute
The prompts of the LLM methods (Banerjee, 2025, Table 5.1). {domain} is the
domain, {k} the number of sentences to write, {sentences} the sampled gold
sentences, one per line with their terms in <tag> tags, and {terms} their terms.
TAG = re.compile('<tag>(.*?)</tag>', re.IGNORECASE | re.DOTALL)
module-attribute
A term tagged by the model.
QUOTES = '"\'`“”‘’'
module-attribute
Quotes stripped from both ends of a sentence written by the model.
Example = tuple[str, list[tuple[int, int]]]
module-attribute
A sentence and the offsets of its terms within it.
augment(corpus, labels=None, method='term', n=50, client=None, domain=None, lexicon=None, per_prompt=10, sampling='diversity', seeds=40, random_state=None, max_words=10, words_layer=None, terms_layer=GOLD_LAYER, sentences_layer=None)
Add synthetic sentences to a training corpus, as in Banerjee (2025, ch. 5).
A sample of the gold sentences, the seeds, is chosen, and new sentences are written from them by one of four methods:
- "term" (LLM-TermDA): the model replaces the terms of the seeds with other terms of the domain.
- "context" (LLM-ContextDA): the model writes new sentences that use the terms of the seeds.
- "combined" (LLM-CombinedDA): the model writes new sentences with new terms.
- "wiki" (Wiki-TermDA): each term of a seed is replaced by a similar term from a
lexicon, such as one from
termxtract.wikipedia.lexicon.
The model marks the terms of the sentences it writes with <tag> tags, except for
"context", whose sentences are annotated with the terms of the seeds they contain.
Sentences without terms, with unbalanced tags or that repeat a sentence are dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corpus
|
Union[Corpus, List[str]]
|
The training corpus. |
required |
labels
|
Optional[Dict[str, int]]
|
Dictionary with terms as keys and binary
labels {0, 1}: needed for a list of strings, whose sentences are annotated
with the terms labelled 1 they contain. None for a Teanga corpus takes the
gold terms of |
None
|
method
|
str
|
"term", "context", "combined" or "wiki". |
'term'
|
n
|
int
|
The number of synthetic sentences to write. Fewer are written if the model or the lexicon runs out of new ones. |
50
|
client
|
Optional[LLMClient]
|
The model of the LLM methods. It must be able to
answer at length: |
None
|
domain
|
Optional[str]
|
The domain, as written in the prompts of the LLM methods, such as "heart failure". |
None
|
lexicon
|
Optional[Dict[str, List[str]]]
|
For "wiki", the replacements of each term, in lower case. |
None
|
per_prompt
|
int
|
The number of sentences each prompt asks for. The seeds are shuffled for each prompt, so that the prompts differ. |
10
|
sampling
|
str
|
"diversity" for the seeds that together have the most distinct terms, as in Banerjee (2025), or "random". |
'diversity'
|
seeds
|
int
|
The number of seeds; all the gold sentences with terms if there are fewer. |
40
|
random_state
|
Optional[int]
|
Seed of the sampling, the order of the seeds in each prompt and the replacements of "wiki". |
None
|
max_words
|
int
|
For a list of strings, the maximum number of words of a labelled term. |
10
|
words_layer
|
Optional[str]
|
For Teanga input, the span layer holding the words,
or None for |
None
|
terms_layer
|
str
|
For Teanga input without labels, the span layer of the gold terms. |
GOLD_LAYER
|
sentences_layer
|
Optional[str]
|
For Teanga input, the span layer of the
sentences, or None for |
None
|
Returns:
| Type | Description |
|---|---|
Corpus | tuple[list[str], dict[str, int]]
|
Union[Corpus, Tuple[List[str], Dict[str, int]]]: For a Teanga corpus, a new |
Corpus | tuple[list[str], dict[str, int]]
|
corpus with a document for each gold sentence and each synthetic sentence, with |
Corpus | tuple[list[str], dict[str, int]]
|
the words layer, a |
Corpus | tuple[list[str], dict[str, int]]
|
the strings followed by the synthetic sentences, and the labels with each term |
Corpus | tuple[list[str], dict[str, int]]
|
of the synthetic sentences labelled 1. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the method or the sampling is unknown, a list of strings has no labels, or the method lacks its client, domain or lexicon. |
outermost(spans)
Drop the spans that overlap a longer one, or an earlier one as long, so that the terms can be tagged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spans
|
Iterable[Tuple[int, int]]
|
The spans. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[int, int]]
|
List[Tuple[int, int]]: The spans kept, in order. |
sample(gold, size, sampling, rng)
Choose the gold sentences to augment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gold
|
List[Example]
|
The gold sentences with terms. |
required |
size
|
int
|
The number to choose. |
required |
sampling
|
str
|
"diversity" to choose, one at a time, the sentence with the most terms not yet chosen and, of those, the most terms, as in Banerjee (2025, §5.1.1); or "random". |
required |
rng
|
Generator
|
The random number generator of "random". |
required |
Returns:
| Type | Description |
|---|---|
list[Example]
|
List[Example]: The chosen sentences, in the order of |
tag(text, spans)
Mark the terms of a sentence with <tag> tags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The sentence. |
required |
spans
|
List[Tuple[int, int]]
|
The offsets of its terms, which do not overlap. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The sentence with each term in tags. |
untag(line)
Read a sentence written by the model with its terms in <tag> tags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
The sentence. |
required |
Returns:
| Type | Description |
|---|---|
Example | None
|
Optional[Example]: The sentence without the tags and the offsets of its terms, |
Example | None
|
or None if the tags are unbalanced, a term is empty or a term does not start and |
Example | None
|
end at the edges of words. |
make_prompt(method, seeds, domain, k)
Write a prompt of an LLM method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
"term", "context" or "combined". |
required |
seeds
|
List[Example]
|
The gold sentences, in the order to show them. |
required |
domain
|
str
|
The domain. |
required |
k
|
int
|
The number of sentences to ask for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The prompt. |
parse_sentences(answer, method, terms=None)
Read the sentences of a model's answer, one per line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
answer
|
str
|
The answer. |
required |
method
|
str
|
"term", "context" or "combined". |
required |
terms
|
Optional[List[str]]
|
For "context", the terms of the seeds, which are looked for in each sentence. |
None
|
Returns:
| Type | Description |
|---|---|
list[Example]
|
List[Example]: The sentences with terms, each with the offsets of its terms. |
generate(client, method, seeds, domain, n, per_prompt, rng, seen)
Ask the model for new sentences until there are n, or until it has been asked
three times as often as n needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
LLMClient
|
The model. |
required |
method
|
str
|
"term", "context" or "combined". |
required |
seeds
|
List[Example]
|
The gold sentences to show. |
required |
domain
|
str
|
The domain. |
required |
n
|
int
|
The number of sentences wanted. |
required |
per_prompt
|
int
|
The number of sentences each prompt asks for. |
required |
rng
|
Generator
|
The random number generator that shuffles the seeds. |
required |
seen
|
Set[str]
|
The sentences, in lower case, that are not new; updated. |
required |
Returns:
| Type | Description |
|---|---|
list[Example]
|
List[Example]: The new sentences. |
replace_terms(seeds, lexicon, n, rng, seen)
Write new sentences by replacing each term of the seeds that is in the lexicon with one of its replacements, as in Wiki-TermDA (Banerjee, 2025, §5.1.2).
The seeds are taken in turn, in a random order, until there are n sentences or a
round of them adds none. The replacements of a term are drawn at random without
replacement, and drawn again once all have been used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seeds
|
List[Example]
|
The gold sentences. |
required |
lexicon
|
Dict[str, List[str]]
|
The replacements of each term, in lower case. |
required |
n
|
int
|
The number of sentences wanted. |
required |
rng
|
Generator
|
The random number generator. |
required |
seen
|
Set[str]
|
The sentences, in lower case, that are not new; updated. |
required |
Returns:
| Type | Description |
|---|---|
list[Example]
|
List[Example]: The new sentences. |