translation ¶
English translation with source-language and engine metadata.
TranslationResult dataclass ¶
TranslationResult(original_text: str, text: str, source_language: str, engine: str | None, language_detection: LanguageDetection | None = None)
Prepared English text and its translation provenance.
Attributes:
| Name | Type | Description |
|---|---|---|
original_text | str | Original input body. |
text | str | English body for downstream analysis. |
source_language | str | Normalized source-language code. |
engine | str | None | Translation engine citation, or |
language_detection | LanguageDetection | None | Detection or verification evidence, when collected. |
translated property ¶
Whether the source language required translation into English.
Translator ¶
Bases: Protocol
English translation backend exposing engine and translate(text, source_language).
TranslationUnavailableError ¶
Bases: RuntimeError
Optional dependencies or local model resources are unavailable.
M2M100Translator ¶
M2M100Translator(tokenizer, model, *, engine, chunk_tokens=384, batch_size=4, num_beams=4, max_new_tokens=768, min_new_tokens=1, cache_identity=None, translation_profile=None)
Local M2M100 translation with sentence chunking and batched inference.
Load with from_model and reuse one instance for sequential translation calls.
Attributes:
| Name | Type | Description |
|---|---|---|
engine | Public model citation used in translation metadata. | |
cache_identity | Full inference configuration, or the supplied engine for direct construction without an explicit cache identity. | |
translation_profile | JSON-serializable load-time provenance from the factory, or an optional caller-supplied profile for direct construction. |
Source code in src/gdelt_regkg/fields/translation.py
from_model classmethod ¶
from_model(model='facebook/m2m100_418M', *, device='cpu', allow_download=False, revision=None, batch_size=4, num_beams=4, chunk_tokens=384, max_new_tokens=768, min_new_tokens=1, cache_dir=None, token=None) -> M2M100Translator
Load a reusable M2M100 model and tokenizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | str | Model identifier or local model directory. | 'facebook/m2m100_418M' |
device | str | PyTorch device, such as | 'cpu' |
allow_download | bool | Allow model downloads when local files are missing. | False |
revision | str | None | Optional model revision. | None |
batch_size | int | Positive number of text chunks per inference batch. | 4 |
num_beams | int | Positive beam count for generation. | 4 |
chunk_tokens | int | Source tokens per chunk, between 1 and 512. | 384 |
max_new_tokens | int | Positive output-token limit per chunk. | 768 |
min_new_tokens | int | Minimum output tokens per chunk, between zero and | 1 |
cache_dir | str | None | Optional Hub cache shared by resolution and loading. | None |
token | str | bool | None | Hub authentication, shared by resolution and loading. | None |
Returns:
| Type | Description |
|---|---|
M2M100Translator | A translator with a short model citation and full configuration provenance. |
Raises:
| Type | Description |
|---|---|
OptionalDependencyError | If a required backend dependency is unavailable. |
TranslationUnavailableError | If model resources cannot be loaded. |
ValueError | If chunk, batch, beam, or output-length settings are invalid. |
Source code in src/gdelt_regkg/fields/translation.py
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 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |
translate ¶
Translate one body into English using the batch translation path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | Source body. | required |
source_language | str | Supported language code, name, or regional tag. | required |
Returns:
| Type | Description |
|---|---|
str | The English translation. |
Raises:
| Type | Description |
|---|---|
ValueError | If the language, input, or generated output is unsupported. |
Source code in src/gdelt_regkg/fields/translation.py
translate_many ¶
Translate same-language bodies in bounded batches of text chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts | Iterable[str] | Source bodies sharing one language. | required |
source_language | str | Supported language code, name, or regional tag. | required |
Returns:
| Type | Description |
|---|---|
list[str] | English translations in input order, with translated chunks joined |
list[str] | by newlines. |
Raises:
| Type | Description |
|---|---|
ValueError | If the language is unsupported or a body produces invalid or empty output. |
OptionalDependencyError | If PyTorch is unavailable. |
Source code in src/gdelt_regkg/fields/translation.py
normalize_language ¶
Return ISO 639-2/T for supported codes/names and regional/script tags.
Examples: UR, Urdu, ur_PK, ur-PK, urPK, and urd_Arab all become urd. Unknown or missing labels fail explicitly; no domain-based guessing runs.
Source code in src/gdelt_regkg/fields/translation.py
translate_text ¶
translate_text(text: str, *, language: str | None = None, translator: Translator, detect_missing_language: bool = True, language_detector: LanguageDetector | None = None, language_check: LanguageCheck = 'script') -> TranslationResult
Prepare English text and record its source language and translation engine.
English input passes through unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | Prepared article body. | required |
language | str | None | Supported language code, name, or regional tag. | None |
translator | Translator | Backend used for non-English input. | required |
detect_missing_language | bool | Detect the language when its label is missing. | True |
language_detector | LanguageDetector | None | Custom detection backend. Defaults to local Lingua. | None |
language_check | LanguageCheck |
| 'script' |
Returns:
| Type | Description |
|---|---|
TranslationResult | English text with original text, language, engine, and detection evidence. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If language settings, the engine, or translated text are invalid. |
LanguageDetectionError | If detection fails or a script conflict is unresolved. |
OptionalDependencyError | If a required detection dependency is unavailable. |