Entry
labl.data.base_entry.BaseLabeledEntry
¶
Bases: LabeledInterface, ABC
Base class for all data entries. This class handles the creation of public getters, disallowing setting and providing a private constructor key to prevent direct instantiation.
relabel
¶
relabel(
relabel_fn: Callable[[LabelType], LabelType]
| None = None,
relabel_map: dict[str | int, LabelType] | None = None,
) -> None
Relabels the entry in-place using a custom relabeling function or a mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Callable[[str | int | float | None], str | int | float | None]
|
A function that will be applied to each label in the entry. The function should take a single argument (the label) and return the new label. The function should return the label without any processing if the label should be preserved. |
None
|
|
dict[str | int, str | int | float | None]
|
A dictionary that maps old labels to new labels. The keys are the old labels and the values are the new labels. This can be used instead of the relabel_fn to relabel the entry if labels are discrete. |
None
|
Source code in labl/data/base_entry.py
get_agreement
¶
get_agreement(
other: EntryType,
level_of_measurement: LevelOfMeasurement | None = None,
) -> AgreementOutput
Compute the inter-annotator agreement for the token labels of two entries using Krippendorff's alpha.
Source code in labl/data/base_entry.py
labl.data.labeled_entry.LabeledEntry
¶
LabeledEntry(
text: str,
spans: SpanList,
tagged: str,
tokens: list[str],
tokens_labels: Sequence[LabelType],
tokens_offsets: list[OffsetType],
info: InfoDictType = {},
constructor_key: object | None = None,
)
Bases: BaseLabeledEntry
Class for a text entry with a set of granular annotations over some of its parts.
The class provides a centralized object to easily switch between different annotation formats:
- The original text without labels.
- A tagged version with
... labels. - A list of spans corresponding to tagged substrings, with start/end indices.
- A tokenized version of the text with labels associated to each token.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
The original text. |
spans |
list[Span]
|
A list of lists of |
tagged |
str
|
A tagged versions of |
tokens |
list[str]
|
A list of strings representing the tokenized version of |
tokens_labels |
list[str | int | float | None]
|
A list of the same length as |
labeled_tokens |
list[list[LabeledToken]]
|
A list of |
tokens_offsets |
list[tuple[int, int] | None]
|
Offsets for each token in |
label_types |
list[type]
|
A list of the types of labels for the entry. |
info |
dict[str, str | int | float | bool]
|
A dictionary containing additional information about the entry. |
A LabeledEntry can be initialized from:
-
A
taggedtext, e.g.Hello <error>world</error>!, usingLabeledEntry.from_tagged(tagged=...). -
A
textand a list of labeledspans, e.g.Hello world!and[{'start': 0, 'end': 5, 'label': 'error'}], usingLabeledEntry.from_spans(text=..., spans=...). -
A list of
labeled_tokenswith string/numeric labels, e.g.[('Hel', 0.5), ('lo', 0.7), ('world', 1), ('!', 0)], or two separate lists oftokensandlabelsusingLabeledEntry.from_tokens(labeled_tokens=...)orLabeledEntry.from_tokens(tokens=..., labels=).
Source code in labl/data/labeled_entry.py
tagged
property
writable
¶
The tagged version of the text. This is a read-only property.
tokens
property
writable
¶
The tokenized version of the text. This is a read-only property.
tokens_labels
property
writable
¶
The labels associated with the tokens. This is a read-only property.
tokens_offsets
property
writable
¶
The offsets for each token in the text. This is a read-only property.
labeled_tokens
property
writable
¶
labeled_tokens: LabeledTokenList
Returns a list of LabeledToken objects joining tokens and tokens_labels with custom visualization.
from_spans
classmethod
¶
from_spans(
text: str,
spans: list[Span] | list[SpanType],
tokenizer: str
| Tokenizer
| PreTrainedTokenizer
| PreTrainedTokenizerFast
| None = None,
tokenizer_kwargs: dict = {},
info: InfoDictType = {},
) -> LabeledEntry
Create a LabeledEntry from a text and a list of spans.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The original text. |
required |
|
list[Span] | list[dict[str, str | int | float | None]]
|
A list or a list of lists of |
required |
|
str | Tokenizer | PreTrainedTokenizer | PreTrainedTokenizerFast | None
|
A |
None
|
|
dict
|
Additional arguments for the tokenizer. |
{}
|
|
dict[str, str | int | float | bool]
|
A dictionary containing additional information about the entry. |
{}
|
Source code in labl/data/labeled_entry.py
from_tagged
classmethod
¶
from_tagged(
tagged: str,
tokenizer: str
| Tokenizer
| PreTrainedTokenizer
| PreTrainedTokenizerFast
| None = None,
keep_tags: list[str] = [],
ignore_tags: list[str] = [],
tokenizer_kwargs: dict = {},
info: InfoDictType = {},
) -> LabeledEntry
Create a LabeledEntry from a tagged text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Tagged version of |
required |
|
str | Tokenizer | PreTrainedTokenizer | PreTrainedTokenizerFast | None
|
A |
None
|
|
list[str]
|
Tag(s) used to mark selected spans, e.g. |
[]
|
|
list[str]
|
Tag(s) that are present in the text but should be ignored while parsing. If not provided, all tags are kept (Default: []). |
[]
|
|
dict
|
Additional arguments for the tokenizer. |
{}
|
|
dict[str, str | int | float | bool]
|
A dictionary containing additional information about the entry. |
{}
|
Source code in labl/data/labeled_entry.py
from_tokens
classmethod
¶
from_tokens(
tokens: list[str],
labels: Sequence[LabelType],
text: str | None = None,
offsets: list[OffsetType] | None = None,
keep_labels: list[str] = [],
ignore_labels: list[str] = [],
tokenizer: str
| Tokenizer
| PreTrainedTokenizer
| PreTrainedTokenizerFast
| None = None,
tokenizer_kwargs: dict = {},
info: InfoDictType = {},
) -> LabeledEntry
Create a LabeledEntry from a list of tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[str] | None
|
A list of tokens. Can be provided together with |
required |
|
list[str | int | float | None] | None
|
A list of labels for the tokens. Can be provided together with |
required |
|
str | None
|
The original text. If not provided, it is detokenized from |
None
|
|
list[tuple[int, int] | None] | None
|
The offsets for each token in |
None
|
|
str | Tokenizer | PreTrainedTokenizer | PreTrainedTokenizerFast | None
|
A |
None
|
|
list[str]
|
Label(s) used to mark selected tokens. If not provided, all labels are kept (Default: []). |
[]
|
|
list[str]
|
Label(s) that are present on tokens but should be ignored while parsing. If not provided, all labels are kept (Default: []). |
[]
|
|
dict
|
Additional arguments for the tokenizer. |
{}
|
|
dict[str, str | int | float | bool]
|
A dictionary containing additional information about the entry. |
{}
|
Example
from labl.data.labeled_entry import LabeledEntry
entry = LabeledEntry.from_tokens(
labeled_tokens=[
("Apple", "ORG"), ("Inc.", "ORG"), ("is", "O"), ("looking", "O"),
("at", "O"), ("buying", "O"), ("U.K.", "LOC"), ("startup", "O"),
("for", "O"), ("$1", "MONEY"), ("billion", "MONEY")
],
ignore_labels=["O"],
)
print(entry.tokens)
>>> Apple Inc. is looking at buying U.K. startup for $1 billion
ORG ORG LOC MONEY MONEY
Source code in labl/data/labeled_entry.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
get_tagged_from_spans
staticmethod
¶
Tags one or more texts using lists of spans.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The text to which tags should be added. |
required |
|
list[Span]
|
The spans to convert to tags. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The tagged texts. |
Source code in labl/data/labeled_entry.py
get_tokens_from_spans
staticmethod
¶
get_tokens_from_spans(
text: str,
spans: list[Span],
tokenizer: Tokenizer | None = None,
) -> tuple[
list[str], Sequence[LabelType], list[OffsetType]
]
Extracts tokens, labels and offsets from a text and a set of labeled spans.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The text to which tags should be added. |
required |
|
list[Span]
|
The spans to convert to tokens. |
required |
|
Tokenizer | None
|
A |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[str], Sequence[LabelType], list[OffsetType]]
|
A tuple |
Source code in labl/data/labeled_entry.py
get_text_and_spans_from_tagged
staticmethod
¶
get_text_and_spans_from_tagged(
tagged: str,
keep_tags: list[str] = [],
ignore_tags: list[str] = [],
) -> tuple[str, SpanList]
Extract spans and clean text from a tagged string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The tagged string to extract spans from. |
required |
|
list[str]
|
Tag(s) used to mark selected spans, e.g. |
[]
|
|
list[str]
|
Tag(s) that are present in the text but should be ignored while parsing. If not provided, all tags are kept (Default: []). |
[]
|
Returns:
| Type | Description |
|---|---|
tuple[str, SpanList]
|
Tuple containing the cleaned text and a list of |
Source code in labl/data/labeled_entry.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
get_spans_from_tokens
staticmethod
¶
get_spans_from_tokens(
text: str,
labels: Sequence[LabelType],
offsets: list[OffsetType] | None = None,
tokenizer: Tokenizer | None = None,
keep_labels: list[str] = [],
ignore_labels: list[str] = [],
) -> SpanList
Extract spans and clean text from a list of labeled tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The original text. |
required |
|
list[str | int | float | None]
|
The labels associated with the tokens. |
required |
|
list[tuple[int, int] | None] | None
|
The offsets for each token in |
None
|
|
Tokenizer | None
|
The tokenizer to use for tokenization. If not provided, whitespace tokenization is used. |
None
|
|
list[str]
|
Token labels that should be ported over to spans. If not provided, all tags are kept (Default: []). |
[]
|
|
list[str]
|
Token labels that should be ignored while parsing. If not provided, all tags are kept (Default: []). |
[]
|
Returns:
| Type | Description |
|---|---|
SpanList
|
A list of |
Source code in labl/data/labeled_entry.py
get_tokens
¶
get_labels
¶
to_dict
¶
Convert the LabeledEntry to a dictionary representation.
Returns:
| Type | Description |
|---|---|
LabeledEntryDictType
|
A dictionary representation of the |
Source code in labl/data/labeled_entry.py
from_dict
classmethod
¶
from_dict(data: LabeledEntryDictType) -> LabeledEntry
Create a LabeledEntry from a dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
A dictionary representation of the |
required |
Returns:
| Type | Description |
|---|---|
LabeledEntry
|
A |
Source code in labl/data/labeled_entry.py
labl.data.edited_entry.EditedEntry
¶
EditedEntry(
orig: LabeledEntry,
edit: LabeledEntry,
has_gaps: bool,
has_bos_token: bool,
has_eos_token: bool,
aligned: WordOutput | None = None,
info: InfoDictType = {},
constructor_key: object | None = None,
)
Bases: BaseLabeledEntry
Class for a pair of text entries (orig and edit) where word-level annotations are obtained from the aligned
tokens of the two entries.
Attributes:
| Name | Type | Description |
|---|---|---|
orig |
LabeledEntry
|
The original entry. |
edit |
LabeledEntry
|
The edited entry. |
has_gaps |
bool
|
Whether the token sequence has gaps. Gaps are used for text/edit pairs to mark the
positions of insertions and deletions in the original/edited texts, respectively. If |
has_bos_token |
bool
|
Whether the tokenizer has a beginning-of-sequence token. |
has_eos_token |
bool
|
Whether the tokenizer has an end-of-sequence token. |
aligned |
WordOutput | None
|
A |
info |
dict[str, str | int | float | bool]
|
A dictionary containing additional information about the entry. |
One or more EditedEntry can be initialized from a text and one or more edits, e.g. Hello world! and
["Goodbye world!", "Hello planet!"], using EditedEntry.from_edits(text=..., edits=...).
Source code in labl/data/edited_entry.py
aligned
property
writable
¶
Aligned output using jiwer for orig and edit.
has_gaps
property
writable
¶
Boolean flag marking whether the token sequence has added gaps for insertion/deletion annotations.
has_bos_token
property
writable
¶
Boolean flag marking whether the tokenizer has a beginning-of-sequence token.
has_eos_token
property
writable
¶
Boolean flag marking whether the tokenizer has an end-of-sequence token.
aligned_str
property
writable
¶
Aligned string at the token level with jiwer.visualize_alignment.
from_edits
classmethod
¶
from_edits(
text: str,
edits: str | list[str],
tokenizer: str
| Tokenizer
| PreTrainedTokenizer
| PreTrainedTokenizerFast
| None = None,
tokenizer_kwargs: dict = {},
with_gaps: bool = True,
keep_final_gap: bool = True,
sub_label: str = "S",
ins_label: str = "I",
del_label: str = "D",
gap_token: str = "▁",
info: InfoDictType | list[InfoDictType] = {},
) -> EditedEntry | MultiEditEntry
Create a EditedEntry or an MultiEditEntry from a text and one or more edits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The original text. |
required |
|
str | list[str] | None
|
One or more edited version of the text. |
required |
|
str | Tokenizer | PreTrainedTokenizer | PreTrainedTokenizerFast | None
|
A |
None
|
|
dict
|
Additional arguments for the tokenizer. |
{}
|
|
bool
|
Whether to add gaps to the tokens and offsets. Gaps are used to mark the positions of insertions and deletions in the original/edited texts, respectively. If false, those are merged to the next token to the right. Default: True. |
True
|
|
bool
|
Whether to keep the final gap token. Default: True. |
True
|
|
str
|
The label for substitutions. Default: "S". |
'S'
|
|
str
|
The label for insertions. Default: "I". |
'I'
|
|
str
|
The label for deletions. Default: "D". |
'D'
|
|
str
|
The token to use for gaps. Default: "▁". |
'▁'
|
|
dict[str, str | int | float | bool] | list[dict[str, str | int | float | bool]]
|
A dictionary containing additional information about the entry. |
{}
|
Returns:
| Type | Description |
|---|---|
EditedEntry | MultiEditEntry
|
A single |
Example
from labl.data.edited_entry import EditedEntry
entries = EditedEntry.from_edits(
text="a simple example",
edits=["this is a simple enough test, you know?", "an example"],
tokenizer="facebook/nllb-200-3.3B",
tokenizer_kwargs={
"tgt_lang": "ita_Latn",
"add_special_tokens": True,
},
)
print(entries[0].aligned_str)
>>> ORIG: ita_Latn ***** *** ▁a ▁simple ******* ***** * **** ***** ▁example </s>
EDIT: ita_Latn ▁this ▁is ▁a ▁simple ▁enough ▁test , ▁you ▁know ? </s>
I I I I I I I S
Source code in labl/data/edited_entry.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
get_tokens_labels_from_edit
classmethod
¶
get_tokens_labels_from_edit(
text: str,
edit: str,
tokens: list[str] | None = None,
tokens_offsets: list[OffsetType] | None = None,
edit_tokens: list[str] | None = None,
edit_tokens_offsets: list[OffsetType] | None = None,
aligned: WordOutput | None = None,
tokenizer: Tokenizer | None = None,
sub_label: str = "S",
ins_label: str = "I",
del_label: str = "D",
gap_token: str = "▁",
) -> tuple[Sequence[str | None], Sequence[str | None]]
Convert text edits to token labels marking insertions, deletions and substitutions. The returned labels include gaps before/after each token, which can be merged to the right to match the original token sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The original text. |
required |
|
str
|
The edited text. |
required |
|
list[str] | None
|
The tokenized version of |
None
|
|
list[tuple[int, int] | None]
|
The offsets of |
None
|
|
list[str] | None
|
The tokenized version of |
None
|
|
list[tuple[int, int] | None]
|
The offsets of |
None
|
|
WordOutput | None
|
The aligned |
None
|
|
Tokenizer | None
|
A |
None
|
|
str
|
The label for substitutions. Default: "S". |
'S'
|
|
str
|
The label for insertions. Default: "I". |
'I'
|
|
str
|
The label for deletions. Default: "D". |
'D'
|
|
str
|
The token to use for gaps. Default: "▁". |
'▁'
|
Returns:
| Type | Description |
|---|---|
tuple[Sequence[str | None], Sequence[str | None]]
|
A tuple containing two lists of labels (one for |
Source code in labl/data/edited_entry.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
get_tokens
¶
get_labels
¶
to_dict
¶
Convert the EditedEntry to a dictionary representation.
Returns:
| Type | Description |
|---|---|
EditedEntryDictType
|
A dictionary representation of the |
Source code in labl/data/edited_entry.py
from_dict
classmethod
¶
from_dict(data: EditedEntryDictType) -> EditedEntry
Create a EditedEntry from a dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
A dictionary representation of the |
required |
Returns:
| Type | Description |
|---|---|
EditedEntry
|
A |
Source code in labl/data/edited_entry.py
merge_gap_annotations
¶
merge_gap_annotations(
merge_fn: Callable[[Sequence[LabelType]], LabelType]
| None = None,
keep_final_gap: bool = True,
) -> None
Merge gap annotations in the tokens of orig and edit.
This method is equivalent to calling EditedEntry.from_edits with with_gaps=False. Gap annotations are merged
to the next non-gap token to the right, and the gap label is added to the label of the non-gap token. The last
gap is kept to account for insertions at the end of the text.
E.g. GAP Hello GAP World GAP ! GAP becomes Hello World ! GAP.
I S I I IS I I