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]

Translate text using a machine translation engine.

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]

Initialize the translator.

Supported pairs of source and 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 a language code), using the small100 engine

  • xx - th - xx to Thai (xx is a language code), using the small100 engine

Parameters:
  • src_lang (str) – source language code

  • target_lang (str) – target language code

  • engine (str) –

    machine translation engine

    • default - default engine for each language pair (default)

    • small100 - multilingual model covering 100 languages

  • use_gpu (bool) – load the model on a GPU (default: False)

Example:

Translate text from Thai to English:

>>> from pythainlp.translate import Translate
>>> th2en = Translate("th", "en")
>>> th2en.translate("ฉันรักแมว")
I love cat.

Translate text with excluded words:

>>> th2en.translate(
...     "ฉันรักแมว", exclude_words=["แมว"]
... )
I love แมว.
load_model() → None[source]

Load the translation model for the language pair and engine.

translate(text: str, exclude_words: list[str] | None = None) → str[source]

Translate text from the source to the target language.

Parameters:
  • text (str) – text to translate

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

Returns:

translated text

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) – word to translate

  • src (str) – source language code

  • target (str) – target language code

  • engine (str) –

    word translation engine

    • word2word - word2word library (default)

Returns:

list of translated words, or None

Return type:

Optional[list[str]]

Example:

Translate word from Thai to English:

>>> from pythainlp.translate import word_translate
>>> print(word_translate("แมว", "th", "en"))
['cat', 'cats', 'kitty', 'kitten', 'Cat']

Translate word from English to Thai:

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

Translate Thai to Chinese.

The model is from Lalita @ AI builder.

Parameters:

use_gpu (bool) – load the model on a GPU (default: 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', revision: str | None = None) → None[source]

Initialize the Thai-to-Chinese translator.

Parameters:
  • use_gpu (bool) – load the model on a GPU

  • pretrained (str) – name of the pretrained model

  • revision (Optional[str]) – revision of the pretrained model

translate(text: str, exclude_words: list[str] | None = None) → str[source]

Translate text from Thai to Chinese.

Parameters:
  • text (str) – text to translate

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

Returns:

translated text

Return type:

str

Example:

Translate text from Thai to Chinese:

>>> from pythainlp.translate import (
...     ThZhTranslator,
... )
>>> thzh = ThZhTranslator()
>>> thzh.translate("ผมรักคุณ")
我爱你

Translate text from Thai to Chinese with excluded words:

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

Translate Chinese to Thai.

The model is from Lalita @ AI builder.

Parameters:

use_gpu (bool) – load the model on a GPU (default: 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', revision: str | None = None) → None[source]

Initialize the Chinese-to-Thai translator.

Parameters:
  • use_gpu (bool) – load the model on a GPU

  • pretrained (str) – name of the pretrained model

  • revision (Optional[str]) – revision of the pretrained model

translate(text: str, exclude_words: list[str] | None = None) → str[source]

Translate text from Chinese to Thai.

Parameters:
  • text (str) – text to translate

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

Returns:

translated text

Return type:

str

Example:

Translate text from Chinese to Thai:

>>> from pythainlp.translate import (
...     ZhThTranslator,
... )
>>> zhth = ZhThTranslator()
>>> zhth.translate("我爱你")
ผมรักคุณนะ

Translate text from Chinese to Thai with excluded words:

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

Translate Thai to French.

The model is trained on the OPUS corpus. It is from the Language Technology Research Group at the University of Helsinki.

BLEU 20.4

Parameters:

use_gpu (bool) – load the model on a GPU (default: 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', revision: str | None = None) → None[source]

Initialize the Thai-to-French translator.

Parameters:
  • use_gpu (bool) – load the model on a GPU

  • pretrained (str) – name of the pretrained model

  • revision (Optional[str]) – revision of the pretrained model

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) – text to translate

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

Returns:

translated text

Return type:

str

Example:

Translate text from Thai to French:

>>> from pythainlp.translate.th_fr import (
...     ThFrTranslator,
... )
>>> thfr = ThFrTranslator()
>>> thfr.translate("ทดสอบระบบ")
"Test du système."

Translate text from Thai to French with excluded words:

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