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.
load_model()[source]
translate(text) str[source]

Translate text

Parameters:

text (str) – input text in source language

Returns:

translated text in target language

Return type:

str

pythainlp.translate.en_th.download_model_all() None[source]

Download all translation models in advance

This function facilitates the download of all available English to Thai translation models. It ensures that the required models are accessible for translation tasks, enhancing the usability of the module.

class pythainlp.translate.en_th.EnThTranslator(use_gpu: bool = False)[source]

English-Thai Machine Translation

from VISTEC-depa Thailand Artificial Intelligence Research Institute

Website: https://airesearch.in.th/releases/machine-translation-models/

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

The EnThTranslator class specializes in translating text from English to Thai. It offers a range of methods for translating sentences and text, enabling accurate and meaningful translations between these languages.

__init__(use_gpu: bool = False)[source]
translate(text: str) str[source]

Translate text from English to Thai

Parameters:

text (str) – input text in source language

Returns:

translated text in target language

Return type:

str

Example:

Translate text from English to Thai:

from pythainlp.translate import EnThTranslator

enth = EnThTranslator()

enth.translate("I love cat.")
# output: ฉันรักแมว
class pythainlp.translate.en_th.ThEnTranslator(use_gpu: bool = False)[source]

Thai-English Machine Translation

from VISTEC-depa Thailand Artificial Intelligence Research Institute

Website: https://airesearch.in.th/releases/machine-translation-models/

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

Conversely, the ThEnTranslator class focuses on translating text from Thai to English. It provides functionality for translating Thai text into English, contributing to effective language understanding and communication.

__init__(use_gpu: bool = False)[source]
translate(text: str) str[source]

Translate text from Thai to English

Parameters:

text (str) – input text in source language

Returns:

translated text in target language

Return type:

str

Example:

Translate text from Thai to English:

from pythainlp.translate import ThEnTranslator

then = ThEnTranslator()

then.translate("ฉันรักแมว")
# output: I love cat.
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) str[source]

Translate text from Thai to Chinese

Parameters:

text (str) – input text in source language

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: 我爱你
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) str[source]

Translate text from Chinese to Thai

Parameters:

text (str) – input text in source language

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: ผมรักคุณนะ
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.

__init__(use_gpu: bool = False, pretrained: str = 'Helsinki-NLP/opus-mt-th-fr') None[source]
translate(text: str) str[source]

Translate text from Thai to French

Parameters:

text (str) – input text in source language

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."