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]
Renders Thai words in the Latin alphabet or “romanization”, using 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) – A Thai word to be romanized. The input should not include whitespace because the function is support subwords by splitting whitespace.
engine (str) – One of ‘royin’ (default), ‘thai2rom’, ‘thai2rom_onnx’, ‘tltk’, and ‘lookup’. See more in options for engine section.
fallback_engine (str) – If engine equals ‘lookup’, use fallback_engine for words that are not in the lookup dictionary. No effect on other engines. Default to ‘royin’.
- Returns:
A string of a Thai word rendered in the Latin alphabet.
- Return type:
- Options for engines:
royin - (default) based on the Royal Thai General System of Transcription issued by Royal Institute of Thailand.
thai2rom - a deep learning-based Thai romanization engine (require PyTorch).
thai2rom_onnx - a deep learning-based Thai romanization engine with ONNX runtime
tltk - TLTK: Thai Language Toolkit
lookup - Look up on 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]
Transliterates Thai text.
- Parameters:
- Returns:
A string of phonetic alphabets indicating how the input text should be pronounced.
- Return type:
- Options for engines:
thaig2p - (default) Thai Grapheme-to-Phoneme, output is IPA (require PyTorch)
icu - pyicu, based on International Components for Unicode (ICU)
ipa - epitran, output is International Phonetic Alphabet (IPA)
tltk_g2p - Thai Grapheme-to-Phoneme from TLTK.,
iso_11940 - Thai text into Latin characters with ISO 11940.
tltk_ipa - tltk, output is International Phonetic Alphabet (IPA)
thaig2p_v2 - Thai Grapheme-to-Phoneme, output is IPA. https://huggingface.co/pythainlp/thaig2p-v2.0
thaig2p_v4 - Thai Grapheme-to-Phoneme (v4), output is IPA. https://huggingface.co/pythainlp/thaig2p-v4
umt5_thaig2p - Thai Grapheme-to-Phoneme, output is IPA, powered by UMT5. 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:
Unsupported modes return the input text unchanged.
- Returns:
Transliterated text.
- Return type:
- Example:
>>> transliterate_wiktionary("แมว", mode="royin") 'maeo'
- pythainlp.transliterate.pronunciate(word: str, engine: str = 'w2p') str[source]
Pronunciates Thai words.
- Parameters:
- Returns:
A string of Thai letters indicating how the input text should be pronounced.
- Return type:
- Options for engines:
w2p - 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 pali word to Thai pronunciation
- Parameters:
word (str) – Pali word (written in Thai script) to be pronunciated.
- Returns:
A string of Thai letters indicating how the input text should be pronounced.
- 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]
Thai Spoonerism
Converts a Thai word to a spoonerism word.
- Parameters:
- Returns:
A string of Thai spoonerism word.
- Return type:
- 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.
- 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.
- pythainlp.transliterate.fastthaig2p.transliterate(text: str) str[source]
Transliterate Thai text to IPA phonemes using FastThaiG2P.
- 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:
- 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 Latin alphabet, using RTGS
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:
A string of Thai words rendered in the Latin alphabet
- Return type:
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.