pythainlp.translate

The pythainlp.translate for machine translation.

Modules

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

Machine Translation

__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 to gpu (Default is False)

**Options for engine*
  • default - The engine default by each a 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 advanced

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 to gpu (Default is False)

__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 to gpu (Default is False)

__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 to gpu (Default is False)

__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 to gpu (Default is False)

__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 from Language Technology Research Group at the University of Helsinki

BLEU 20.4

:param bool use_gpu : load model to gpu (Default is False)

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