pythainlp.translate

The pythainlp.translate module is dedicated to machine translation capabilities for the PyThaiNLP library. It provides tools for translating text between different languages, making it a valuable resource for natural language processing tasks.

Modules

class pythainlp.translate.Translate(src_lang: str, target_lang: str, engine: str = 'default', use_gpu: bool = False)[source]

Machine Translation

The Translate class is the central component of the module, offering a unified interface for various translation tasks. It acts as a coordinator, directing translation requests to specific language pairs and models.

__init__(src_lang: str, target_lang: str, engine: str = 'default', use_gpu: bool = False) None[source]
Parameters:
  • src_lang (str) – source language

  • target_lang (str) – target language

  • engine (str) – machine translation engine

  • use_gpu (bool) – load model using GPU (Default is False)

**Options for engine*
  • default - The default engine for each language.

  • small100 - A multilingual machine translation model (covering 100 languages)

Options for source & target language
  • th - en - Thai to English

  • en - th - English to Thai

  • th - zh - Thai to Chinese

  • zh - th - Chinese to Thai

  • th - fr - Thai to French

  • th - xx - Thai to xx (xx is language code). It uses small100 model.

  • xx - th - xx to Thai (xx is language code). It uses small100 model.

Example:

Translate text from Thai to English:

from pythainlp.translate import Translate

th2en = Translate("th", "en")

th2en.translate("ฉันรักแมว")
# output: I love cat.

Translate text with excluded words:

th2en.translate("ฉันรักแมว", exclude_words=["แมว"])
# output: I love แมว.
load_model() None[source]
translate(text: str, exclude_words: list[str] | None = None) str[source]

Translate text

Parameters:
  • text (str) – input text in source language

  • exclude_words (list[str]) – words to exclude from translation (optional)

Returns:

translated text in target language

Return type:

str

class pythainlp.translate.word_translate(word: str, src: str, target: str, engine: str = 'word2word')[source]

Translate word from source language to target language.

Parameters:
  • word (str) – text

  • src (str) – src language

  • target (str) – target language

  • engine (str) – Word translate engine (the default engine is word2word)

Returns:

return list word translate or None

Return type:

Union[List[str], None]

Example:

Translate word from Thai to English:

from pythainlp.translate import word_translate

print(word_translate("แมว", "th", "en"))
# output: ['cat', 'cats', 'kitty', 'kitten', 'Cat']

Translate word from English to Thai:

from pythainlp.translate import word_translate

print(word_translate("cat", "en", "th"))
# output: ['แมว', 'แมวป่า', 'ข่วน', 'เลี้ยง', 'อาหาร']
class pythainlp.translate.zh_th.ThZhTranslator(use_gpu: bool = False, pretrained: str = 'Lalita/marianmt-th-zh_cn')[source]

Thai-Chinese Machine Translation

from Lalita @ AI builder

:param bool use_gpu : load model using GPU (Default is False)

The ThZhTranslator class specializes in translating text from Thai to Chinese (Simplified). This class is valuable for bridging language gaps between these two languages, promoting cross-cultural communication.

__init__(use_gpu: bool = False, pretrained: str = 'Lalita/marianmt-th-zh_cn') None[source]
translate(text: str, exclude_words: list[str] | None = None) str[source]

Translate text from Thai to Chinese

Parameters:
  • text (str) – input text in source language

  • exclude_words (list[str]) – words to exclude from translation (optional)

Returns:

translated text in target language

Return type:

str

Example:

Translate text from Thai to Chinese:

from pythainlp.translate import ThZhTranslator

thzh = ThZhTranslator()

thzh.translate("ผมรักคุณ")
# output: 我爱你

Translate text from Thai to Chinese with excluded words:

thzh.translate("ผมรักคุณ", exclude_words=["ผม"])
# output: ผม爱你
class pythainlp.translate.zh_th.ZhThTranslator(use_gpu: bool = False, pretrained: str = 'Lalita/marianmt-zh_cn-th')[source]

Chinese-Thai Machine Translation

from Lalita @ AI builder

:param bool use_gpu : load model using GPU (Default is False)

The ZhThTranslator class is designed for translating text from Chinese (Simplified) to Thai. It assists in making content accessible to Thai-speaking audiences by converting Chinese text into Thai.

__init__(use_gpu: bool = False, pretrained: str = 'Lalita/marianmt-zh_cn-th') None[source]
translate(text: str, exclude_words: list[str] | None = None) str[source]

Translate text from Chinese to Thai

Parameters:
  • text (str) – input text in source language

  • exclude_words (list[str]) – words to exclude from translation (optional)

Returns:

translated text in target language

Return type:

str

Example:

Translate text from Chinese to Thai:

from pythainlp.translate import ZhThTranslator

zhth = ZhThTranslator()

zhth.translate("我爱你")
# output: ผมรักคุณนะ

Translate text from Chinese to Thai with excluded words:

zhth.translate("我爱你", exclude_words=["我"])
# output: 我รักคุณนะ
class pythainlp.translate.th_fr.ThFrTranslator(use_gpu: bool = False, pretrained: str = 'Helsinki-NLP/opus-mt-th-fr')[source]

Thai-French Machine Translation

Trained by OPUS Corpus

Model is from Language Technology Research Group at the University of Helsinki

BLEU 20.4

:param bool use_gpu : load model using GPU (Default is False)

Lastly, the ThFrTranslator class specializes in translating text from Thai to French. It serves as a tool for expanding language accessibility and promoting content sharing in French-speaking communities.

translated: torch.Tensor
__init__(use_gpu: bool = False, pretrained: str = 'Helsinki-NLP/opus-mt-th-fr') None[source]
tokenizer_thfr: AutoTokenizer
model_thfr: AutoModelForSeq2SeqLM
translate(text: str, exclude_words: list[str] | None = None) str[source]

Translate text from Thai to French

Parameters:
  • text (str) – input text in source language

  • exclude_words (list[str]) – words to exclude from translation (optional)

Returns:

translated text in target language

Return type:

str

Example:

Translate text from Thai to French:

from pythainlp.translate.th_fr import ThFrTranslator

thfr = ThFrTranslator()

thfr.translate("ทดสอบระบบ")
# output: "Test du système."

Translate text from Thai to French with excluded words:

thfr.translate("ทดสอบระบบ", exclude_words=["ระบบ"])
# output: "Test du ระบบ."