translation_cache ¶
Persistent SQLite caching for English translations.
CachedTranslator ¶
Cache translations by full identity, source language, and input text.
Create one instance per worker; instances may share a database file. Use as a context manager to close the cache automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
translator | Translator | Translation backend with a nonempty engine identifier. | required |
path | StrPath | SQLite database path, or | required |
timeout | float | SQLite lock timeout in seconds. | 60 |
Raises:
| Type | Description |
|---|---|
ValueError | If the engine identifier or timeout is invalid. |
Error | If the cache cannot be opened or initialized. |
Source code in src/gdelt_regkg/translation_cache.py
translation_profile property ¶
Full provenance, when exposed by the wrapped backend.
connection property ¶
Return the cache's open SQLite connection.
Raises:
| Type | Description |
|---|---|
RuntimeError | If the cache is closed. |
close ¶
Roll back pending work and close the cache connection.
Repeated calls are safe. The wrapped translator remains available.
Source code in src/gdelt_regkg/translation_cache.py
translate ¶
Translate one body into English, reusing cached results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | Source body to translate. | required |
source_language | str | Supported language code, name, or regional tag. | required |
Returns:
| Type | Description |
|---|---|
str | The English translation. English input is returned unchanged. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If the language is invalid, a non-English body is blank, or the backend returns invalid output. |
RuntimeError | If the cache is closed. |
Error | If reading or saving cached results fails. |
Source code in src/gdelt_regkg/translation_cache.py
translate_many ¶
Translate bodies into English, batching unique cache misses.
Uses the backend's batch method when available. New results are committed after validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts | Iterable[str] | Source bodies to translate. | required |
source_language | str | Supported language code, name, or regional tag. | required |
Returns:
| Type | Description |
|---|---|
list[str] | Translations in input order, preserving duplicates. English input |
list[str] | is returned unchanged. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If the language is invalid, a non-English body is blank, or the backend returns invalid output. |
RuntimeError | If the cache is closed. |
Error | If reading or saving cached results fails. |