pythainlp.transliterate

The pythainlp.transliterate module is dedicated to the transliteration of Thai text into romanized form, effectively spelling it out with the English alphabet. This functionality is invaluable for making Thai text more accessible to non-Thai speakers and for various language processing tasks.

Modules

pythainlp.transliterate.romanize(text: str, engine: str = 'royin', fallback_engine: str = 'royin') → str[source]

Render Thai words in the Latin alphabet (“romanization”).

The default engine uses the Royal Thai General System of Transcription (RTGS) [1].

RTGS is the official system published by the Royal Institute of Thailand. (Thai: ถอดเสียงภาษาไทยเป็นอักษรละติน)

Parameters:
  • text (str) – Thai text to be romanized. Words separated by whitespace are romanized one by one

  • engine (str) – romanization engine (see the options below)

  • fallback_engine (str) – engine for words that are not in the lookup dictionary, used only if engine is “lookup” (default is “royin”)

Returns:

text rendered in the Latin alphabet

Return type:

str

Options for engine:
  • royin (default) - based on the Royal Thai General System of Transcription issued by the Royal Institute of Thailand

  • thai2rom - deep learning-based Thai romanization engine (requires PyTorch)

  • thai2rom_onnx - deep learning-based Thai romanization engine with ONNX runtime

  • tltk - TLTK: Thai Language Toolkit

  • lookup - look up in the Thai-English Transliteration dictionary v1.4 compiled by Wannaphong

Example:
>>> from pythainlp.transliterate import romanize
>>> romanize("สามารถ", engine="royin")
'samant'
>>> romanize("สามารถ", engine="thai2rom")
'samat'
>>> romanize("สามารถ", engine="tltk")
'samat'
>>> romanize("ภาพยนตร์", engine="royin")
'phapn'
>>> romanize("รส ดี", engine="royin")  # subwords
'rot di'
>>> romanize("ภาพยนตร์", engine="thai2rom")
'phapphayon'
>>> romanize("ภาพยนตร์", engine="thai2rom_onnx")
'phapphayon'
>>> romanize("ก็อปปี้", engine="lookup")
'copy'

The romanize function allows you to transliterate Thai text, converting it into a phonetic representation using the English alphabet. It’s a fundamental tool for rendering Thai words and phrases in a more familiar format.

pythainlp.transliterate.transliterate(text: str, engine: str = 'thaig2p') → str[source]

Transliterate Thai text.

Parameters:
  • text (str) – Thai text to be transliterated

  • engine (str) – transliteration engine (see the options below)

Returns:

transliterated text: a phonetic alphabet (such as IPA) for the phonetic engines, or Latin script for icu and iso_11940

Return type:

str

Options for engine:
  • thaig2p (default) - Thai Grapheme-to-Phoneme, output is IPA (requires PyTorch)

  • icu - pyicu, based on International Components for Unicode (ICU), output is Latin script

  • ipa - epitran, output is International Phonetic Alphabet (IPA)

  • tltk_g2p - Thai Grapheme-to-Phoneme from TLTK

  • iso_11940 - Thai text into Latin script with ISO 11940

  • tltk_ipa - tltk, output is International Phonetic Alphabet (IPA)

  • thaig2p_v2 - Thai Grapheme-to-Phoneme, output is IPA. See https://huggingface.co/pythainlp/thaig2p-v2.0

  • thaig2p_v4 - Thai Grapheme-to-Phoneme (v4), output is IPA. See https://huggingface.co/pythainlp/thaig2p-v4

  • umt5_thaig2p - Thai Grapheme-to-Phoneme, output is IPA, powered by UMT5. See https://huggingface.co/B-K/umt5-thai-g2p-v2-0.5k

  • fastthaig2p - FastThaiG2P, output is IPA (pure Python dictionary lookup with text normalization and fallback)

Example:
>>> from pythainlp.transliterate import transliterate
>>> transliterate("สามารถ", engine="icu")
's̄āmārt̄h'
>>> transliterate("สามารถ", engine="ipa")
'saːmaːrot'
>>> transliterate("สามารถ", engine="thaig2p")
's aː ˩˩˦ . m aː t̚ ˥˩'
>>> transliterate("สามารถ", engine="fastthaig2p")
'/saː˩˩˦.maːt̚˥˩/'
>>> transliterate("สามารถ", engine="tltk_ipa")
'saː5.maːt3'
>>> transliterate("สามารถ", engine="tltk_g2p")
'saa4~maat2'
>>> transliterate("สามารถ", engine="iso_11940")
's̄āmārt̄h'
>>> transliterate("ภาพยนตร์", engine="icu")
'p̣hāphyntr̒'
>>> transliterate("ภาพยนตร์", engine="ipa")
'pʰaːpjanot'
>>> transliterate("ภาพยนตร์", engine="thaig2p")
'pʰ aː p̚ ˥˩ . pʰ a ˦˥ . j o n ˧'
>>> transliterate("ภาพยนตร์", engine="iso_11940")
'p̣hāphyntr̒'

The transliterate function serves as a versatile transliteration tool, offering a range of transliteration engines to choose from. It provides flexibility and customization for your transliteration needs.

pythainlp.transliterate.transliterate_wiktionary(text: str, mode: str = 'ipa') → str[source]

Transliterate Thai text using Wiktionary th-pron logic.

Parameters:
  • text (str) – Thai text to be transliterated, a single word or a text fragment

  • mode (str) – output mode, one of paiboon, royin, or ipa (default). An unsupported mode returns the text unchanged

Returns:

transliterated text

Return type:

str

Example:
>>> transliterate_wiktionary("แมว", mode="royin")
'maeo'
pythainlp.transliterate.pronunciate(word: str, engine: str = 'w2p') → str[source]

Convert a Thai word to its pronunciation in Thai letters.

Parameters:
  • word (str) – Thai word to be converted

  • engine (str) – pronunciation engine (see the options below)

Returns:

Thai letters indicating how the word is pronounced

Return type:

str

Options for engine:
  • w2p (default) - Thai Word-to-Phoneme

Example:
>>> from pythainlp.transliterate import pronunciate
>>> pronunciate("สามารถ", engine="w2p")
'สา-มาด'
>>> pronunciate("ภาพยนตร์", engine="w2p")
'พาบ-พะ-ยน'

This function provides assistance in generating phonetic representations of Thai words, which is particularly useful for language learning and pronunciation practice.

pythainlp.transliterate.pronunciate_pali(word: str) → str[source]

Convert a Pali word to its Thai pronunciation.

Parameters:

word (str) – Pali word, written in Thai script

Returns:

Thai letters indicating how the word is pronounced

Return type:

str

Example:
>>> from pythainlp.transliterate import pronunciate_pali
>>> pronunciate_pali("ภควา")
'ภะคะวา'
>>> pronunciate_pali("สมฺมา")
'สัมมา'
>>> pronunciate_pali("มยํ")
'มะยัง'
>>> pronunciate_pali("สฺวากฺขา")
'สวากขาโต'
pythainlp.transliterate.puan(word: str, show_pronunciation: bool = True) → str[source]

Convert a Thai word to a spoonerism word.

Syllables without an initial consonant stay in place.

Parameters:
  • word (str) – Thai word to be converted

  • show_pronunciation (bool) – if True (default), return the pronunciation with syllables separated by hyphens; otherwise return the syllables joined without hyphens

Returns:

spoonerism word

Return type:

str

Example:
>>> from pythainlp.transliterate import puan
>>> puan("นาริน")
'นิน-รา'
>>> puan("นาริน", False)
'นินรา'

The puan function offers a unique transliteration feature known as “Puan.” It provides a specialized transliteration method for Thai text and is an additional option for rendering Thai text into English characters.

pythainlp.transliterate.get_word_dict(word: str) → dict[str, str][source]

Return Wiktionary transliteration outputs in all supported systems.

Parameters:

word (str) – Thai word to be transliterated

Returns:

dictionary with the keys word, paiboon, royin, and ipa

Return type:

dict[str, str]

Example:
>>> get_word_dict("แมว")
{'word': 'แมว', 'paiboon': 'mɛɛo', 'royin': 'maeo', 'ipa': 'mɛːw˧'}
class pythainlp.transliterate.fastthaig2p.FastThaiG2P(ipa_dict_path: str | None = None)[source]

FastThaiG2P: Lightning-fast Thai grapheme-to-phoneme conversion.

Uses a 4-stage pipeline: 1. Text normalization: expands numbers, units, abbreviations, emails, etc. 2. Tokenization: segments normalized text with PyThaiNLP’s newmm dictionary engine. 3. Dictionary lookup: retrieves IPA phonemes from the 62k-word dictionary. 4. Fallback G2P: handles out-of-vocabulary words using rule-based fallback.

The FastThaiG2P class provides lightning-fast Thai Grapheme-to-Phoneme conversion using a 62k IPA dictionary, text normalization, and rule-based fallback.

__init__(ipa_dict_path: str | None = None) → None[source]

Initialize FastThaiG2P engine.

Parameters:

ipa_dict_path (Optional[str]) – Optional path to custom IPA dictionary JSON. If None, loads the default dictionary from pythainlp.corpus.

convert(text: str) → str[source]

Convert Thai text to IPA phonemes.

Parameters:

text (str) – Thai text to convert.

Returns:

String of IPA phonemes separated by spaces.

Return type:

str

Example:
>>> from pythainlp.transliterate.fastthaig2p import FastThaiG2P
>>> g2p = FastThaiG2P()
>>> g2p.convert("สวัสดีครับ")
'/sa˨˩.wat̚˨˩.diː˧/ /kʰrap̚˦˥/'
pythainlp.transliterate.fastthaig2p.transliterate(text: str) → str[source]

Transliterate Thai text to IPA phonemes using FastThaiG2P.

Parameters:

text (str) – Thai text to transliterate

Returns:

IPA phonemes

Return type:

str

Example:
>>> from pythainlp.transliterate.fastthaig2p import transliterate
>>> transliterate("สวัสดีครับ")
'/sa˨˩.wat̚˨˩.diː˧/ /kʰrap̚˦˥/'
pythainlp.transliterate.fastthaig2p.normalize(text: str) → str[source]

Normalize text for Thai TTS/G2P.

Convert numbers, abbreviations, and special characters to speakable Thai words.

Parameters:

text (str) – Thai text to normalize

Returns:

Normalized speakable Thai text

Return type:

str

Example:
>>> from pythainlp.transliterate.fastthaig2p import normalize
>>> normalize("มี 42 คน")
'มี สี่สิบสอง คน'
>>> normalize("ราคา 1,000 บาท")
'ราคา หนึ่งพัน บาท'
>>> normalize("เด็กๆ")
'เด็กเด็ก'

Transliteration Engines

thai2rom

royin

pythainlp.transliterate.royin.romanize(text: str) → str[source]

Render Thai words in the Latin alphabet, using RTGS.

The Royal Thai General System of Transcription (RTGS) is the official system by the Royal Institute of Thailand.

Parameters:

text (str) – Thai text to be romanized

Returns:

text rendered in the Latin alphabet

Return type:

str

The royin engine focuses on transliterating Thai text into English characters. It provides an alternative approach to transliteration, ensuring accurate representation of Thai words.

Transliterate Engines

This section includes multiple transliteration engines designed to suit various use cases. They offer unique methods for transliterating Thai text into romanized form:

  • icu: Utilizes the ICU transliteration system for phonetic conversion.

  • ipa: Provides International Phonetic Alphabet (IPA) representation of Thai text.

  • thaig2p: (default) Transliterates Thai text into the Grapheme-to-Phoneme (G2P) representation.

  • thaig2p_v2: Transliterates Thai text into the Grapheme-to-Phoneme (G2P) representation. This model is from https://huggingface.co/pythainlp/thaig2p-v2.0

  • thaig2p_v4: Transliterates Thai text into the Grapheme-to-Phoneme (G2P) representation. This model is from https://huggingface.co/pythainlp/thaig2p-v4

  • fastthaig2p: Fast Thai Grapheme-to-Phoneme converter for IPA transcription with text normalization and fallback.

  • tltk: Utilizes the TLTK transliteration system for a specific approach to transliteration.

  • iso_11940: Focuses on the ISO 11940 transliteration standard.

References

The pythainlp.transliterate module offers a comprehensive set of tools and engines for transliterating Thai text into Romanized form. Whether you need a simple transliteration, specific engines for accurate representation, or phonetic rendering, this module provides a wide range of options. Additionally, the module references a publication that highlights the significance of Romanization, Transliteration, and Transcription in making the Thai language accessible to a global audience.