Usage
CLI usage
Translate text
emtrans "Hello world this is Python code"
# Monsu_1 monsu_2 monsu_3 monsu_4 Monsu_8 monsu_9
Case is normalized for lookup, and casing is preserved on output. Punctuation and spacing are kept:
emtrans "Hello, world! This is a test."
# Monsu_1, monsu_2! monsu_3 monsu_4 monsu_5 monsu_6.
Reverse translation
emtrans -r "monsu_1 monsu_2"
# hello world
Files and pipes
echo "good morning" | emtrans
# monsu_13 monsu_14
emtrans translate -i input.txt -o output.txt
JSON output
emtrans --json "hello zebra"
# {
# "original": "hello zebra",
# "translated": "monsu_1 zebra",
# "missing": [
# "zebra"
# ],
# "reverse": false
# }
Manage the dictionary
emtrans add hi monsu_91 # persist to the user dictionary
emtrans add hola monsu_92 -d ./project.toml # or to a project file
emtrans list
emtrans list --json
See the CLI Reference for every command and option.
Library usage
Basic translation
from emtranslator import Translator
t = Translator()
t.translate("Hello world") # 'Monsu_1 monsu_2'
Reverse direction
rev = Translator(reverse=True)
rev.translate("monsu_1") # 'hello'
Custom dictionaries
A custom mapping is merged over the built-in dictionary; custom entries win. It is not persisted.
t = Translator({"hi": "salut", "hello": "bonjour"})
t.translate("hi there") # 'salut there'
To load a custom dictionary from a TOML file, pass it to the CLI
(emtrans -d file.toml) or read it yourself:
from emtranslator import Translator
from emtranslator.dictionary import load_dictionary
t = Translator(dictionary=load_dictionary("project.toml"))
Adding and persisting words
t = Translator()
t.add_word("hola", "monsu_91") # saved to the user dictionary on disk
t.translate("hola") # 'monsu_91'
Use persist=False to keep the word only for the current instance:
t.add_word("hola", "monsu_91", persist=False)
Reporting missing words
t = Translator()
t.missing_words("hello antelope") # ['antelope']
API reference
| Member | Description |
|---|---|
Translator(dictionary=None, reverse=False, load_user_dict=True, user_dict_path=USER_DICTIONARY_PATH) |
Construct a translator. dictionary merges over the built-in dictionary. reverse=True translates Monsu to English. load_user_dict=False skips the on-disk user dictionary. |
Translator.translate(text) |
Translate a string, preserving punctuation, whitespace, and unknown words. |
Translator.translate_words(words) |
Translate an iterable of plain words; returns a list. |
Translator.missing_words(text) |
Return sorted unique words in text absent from the dictionary. |
Translator.add_word(english, monsu, persist=True) |
Add a mapping, optionally persisting it. |
Translator.dictionary |
A copy of the active source-direction mapping. |
Translator.reverse |
Whether this instance translates in reverse. |
emtranslator.DEFAULT_DICTIONARY |
The built-in 90-word English-to-Monsu mapping. |
emtranslator.__version__ |
The package version string. |