Translation and cache¶
Translate between Odia and other languages. English to Odia tries a bundled offline dictionary first and only then calls Google Translate. Results that come from Google are kept in a cache.
Examples that need the network have no output shown and are not run by the test suite, because Google can rate limit or change its answers. Everything else on this page is run by the suite.
Functions¶
| Function | Direction | Default |
|---|---|---|
other_lang_to_odia(text, source_language_code="en") |
any language to Odia | English source |
odia_to_other_lang(text, dest_language_code="en") |
Odia to any language | English target |
universal_translation(text, source_language_code="en", dest_language_code="or") |
any to any | English to Odia |
Language codes are the ones Google Translate uses, for example hi, de, or.
from openodia import other_lang_to_odia, odia_to_other_lang, universal_translation
other_lang_to_odia("शेयर बाज़ार", source_language_code="hi")
odia_to_other_lang("ନମସ୍କାର", dest_language_code="de")
universal_translation("Guten Morgen", source_language_code="de", dest_language_code="en")
The offline dictionary¶
When the source is "en", other_lang_to_odia looks the text up in an English to Odia dictionary that ships with the package. The lookup is case-insensitive and needs no network.
>>> from openodia import other_lang_to_odia
>>> other_lang_to_odia("good morning")
'ଶୁଭ ସକାଳ'
>>> other_lang_to_odia("Good Morning")
'ଶୁଭ ସକାଳ'
>>> from openodia.corpus.dictionary import get_dictionary
>>> dictionary = get_dictionary()
>>> len(dictionary) > 100_000
True
>>> dictionary["a bad apple"]
'ଏକ ଖରାପ ଆପଲ୍'
The dictionary is loaded once per process, so the first call pays for reading the JSON file and later calls reuse it.
A padded phrase needs a release newer than 0.1.14
The lookup lowercases the text. PyPI 0.1.14 stops there, so "good morning " misses the dictionary and the call goes to Google Translate. A newer release also tries the text with surrounding whitespace removed. The two calls below are that newer behavior, and they hit the dictionary.
>>> other_lang_to_odia("good morning ")
'ଶୁଭ ସକାଳ'
>>> other_lang_to_odia(" Good Morning")
'ଶୁଭ ସକାଳ'
Gotchas
- A non-English source never uses the dictionary, even for a word that is in it.
odia_to_other_langanduniversal_translationalways go to Google, never to the dictionary.
The translation cache¶
Every Google answer is stored under the key (text, source_code, destination_code) in an in-memory LRU cache of 10,000 entries. cache.stats() reports hits, misses and sizes, and the same call repeated later is served from memory.
You can seed the cache yourself, which is also how to see a hit without the network:
>>> from openodia import cache, odia_to_other_lang
>>> cache.clear()
>>> cache.get_cache().set(("ନମସ୍କାର", "or", "en"), "Hello")
>>> odia_to_other_lang("ନମସ୍କାର")
'Hello'
>>> cache.stats()
{'hits': 1, 'misses': 0, 'size': 1, 'max_size': 10000, 'disk_size': 0}
cache.get_cache().get(key) returns None on a miss and counts it:
>>> cache.get_cache().get(("ଅଜଣା", "or", "en")) is None
True
>>> cache.stats()["misses"]
1
Size and eviction¶
cache.configure(max_size=n) sets the capacity. The least recently used entry is evicted first, and reading an entry makes it the most recent.
>>> _ = cache.configure(max_size=2)
>>> c = cache.get_cache()
>>> c.set("a", "1"); c.set("b", "2"); c.set("c", "3")
>>> c.get("a") is None, c.get("c")
(True, '3')
>>> cache.stats()["size"]
2
>>> cache.configure(max_size=0)
Traceback (most recent call last):
...
ValueError: max_size must be >= 1, got 0
Gotcha: configure starts a new cache
configure installs a fresh cache. Entries and hit counters from the old one are gone, and disk_path is dropped unless you pass it again.
Persist across runs¶
Install the extra and give configure a directory:
pip install "openodia[cache]"
from openodia import cache
cache.configure(max_size=50_000, disk_path="~/.cache/openodia/translate")
Reads check memory first, then disk. You can also set the defaults before the process starts with the environment variables OPENODIA_CACHE_MAX_SIZE and OPENODIA_CACHE_DISK. An invalid size raises ValueError the first time the cache is used.
Async and batch¶
odia_to_other_lang_async, other_lang_to_odia_async, and universal_translation_async call the same functions on a worker thread and share this cache. translate_batch translates a list of strings and keeps the input order. Repeated strings in one call are translated once.
Async translation needs a release newer than 0.1.14. from openodia import translate_batch fails on PyPI 0.1.14.
>>> import asyncio
>>> from openodia import odia_to_other_lang_async, translate_batch
>>> cache.clear()
>>> cache.get_cache().set(("ନମସ୍କାର", "or", "en"), "Hello")
>>> asyncio.run(odia_to_other_lang_async("ନମସ୍କାର"))
'Hello'
>>> asyncio.run(translate_batch(["ନମସ୍କାର", "ନମସ୍କାର"], to="en"))
['Hello', 'Hello']
>>> asyncio.run(translate_batch("ନମସ୍କାର"))
Traceback (most recent call last):
...
TypeError: texts must be a list of strings, not a single str or bytes; wrap it in a list
concurrency defaults to 8 and must be an integer of at least 1. retries defaults to 0. A retry waits backoff * 2**attempt seconds, and backoff defaults to 0.5. backoff must be a finite number of at least 0. A bad value raises ValueError.
Only an HTTP 429 rate limit, an HTTP 5xx status, and a connection or timeout error are retried. The first other failure is raised, and texts that have not started are skipped. With return_exceptions=True, that failure is an entry in the result list instead.
source defaults to "or" and to defaults to "en". English to Odia still checks the offline dictionary first.
Next¶
- Command-line interface for the
translatecommand. - API reference: translation and cache and offline dictionary.