pythainlp.khavee

The pythainlp.khavee module is a powerful toolkit designed for working with Thai poetry. The term “khavee” corresponds to “กวี” in the Thai language, which translates to “Poetry” in English. This toolkit equips users with the tools and utilities necessary for the creation, analysis, and verification of Thai poetry.

Modules

KhaveeVerifier

class pythainlp.khavee.KhaveeVerifier[source]
__init__()[source]

KhaveeVerifier: Thai Poetry verifier

check_sara(word: str) str[source]

Check the vowels in the Thai word.

Parameters:

word (str) – Thai word

Returns:

vowel name of the word

Return type:

str

Example:

from pythainlp.khavee import KhaveeVerifier

kv = KhaveeVerifier()

print(kv.check_sara("เริง"))
# output: 'เออ'
check_marttra(word: str) str[source]

Check the Thai spelling Section in the Thai word.

Parameters:

word (str) – Thai word

Returns:

name of spelling Section of the word.

Return type:

str

Example:

from pythainlp.khavee import KhaveeVerifier

kv = KhaveeVerifier()

print(kv.check_marttra("สาว"))
# output: 'เกอว'
is_sumpus(word1: str, word2: str) bool[source]

Check the rhyme between two words.

Parameters:
  • word1 (str) – Thai word

  • word2 (str) – Thai word

Returns:

boolean

Return type:

bool

Example:

from pythainlp.khavee import KhaveeVerifier

kv = KhaveeVerifier()

print(kv.is_sumpus("สรร", "อัน"))
# output: True

print(kv.is_sumpus("สรร", "แมว"))
# output: False
check_karu_lahu(text)[source]
check_klon(text: str, k_type: int = 8) List[str] | str[source]

Check the suitability of the poem according to Thai principles.

Parameters:
  • text (str) – Thai poem

  • k_type (int) – type of Thai poem

Returns:

the check results of the suitability of the poem according to Thai principles.

Return type:

Union[List[str], str]

Example:

from pythainlp.khavee import KhaveeVerifier

kv = KhaveeVerifier()

print(kv.check_klon(
    'ฉันชื่อหมูกรอบ ฉันชอบกินไก่ แล้วก็วิ่งไล่ หมาชื่อนํ้าทอง ลคคนเก่ง เอ๋งเอ๋งคะนอง                 มีคนจับจอง เขาชื่อน้องเธียร',
    k_type=4
))
# output: The poem is correct according to the principle.

print(kv.check_klon(
    'ฉันชื่อหมูกรอบ ฉันชอบกินไก่ แล้วก็วิ่งไล่ หมาชื่อนํ้าทอง ลคคนเก่ง                 เอ๋งเอ๋งเสียงหมา มีคนจับจอง เขาชื่อน้องเธียร',
    k_type=4
))
# output: [
    "Can't find rhyme between paragraphs ('หมา', 'จอง') in paragraph 2",
    "Can't find rhyme between paragraphs ('หมา', 'ทอง') in paragraph 2"
]
check_aek_too(text: List[str] | str, dead_syllable_as_aek: bool = False) List[bool] | List[str] | bool | str[source]

Checker of Thai tonal words

Parameters:
  • text (Union[List[str], str]) – Thai word or list of Thai words

  • dead_syllable_as_aek (bool) – if True, dead syllable will be considered as aek

Returns:

the check result if the word is aek or too or False (not both) or list of check results if input is list

Return type:

Union[List[bool], List[str], bool, str]

Example:

from pythainlp.khavee import KhaveeVerifier

kv = KhaveeVerifier()

# การเช็คคำเอกโท
print(
    kv.check_aek_too("เอง"),
    kv.check_aek_too("เอ่ง"),
    kv.check_aek_too("เอ้ง"),
)
# -> False, aek, too
print(kv.check_aek_too(["เอง", "เอ่ง", "เอ้ง"]))  # ใช้ List ได้เหมือนกัน
# -> [False, 'aek', 'too']
handle_karun_sound_silence(word: str) str[source]

Handle silent sounds in Thai words using ‘์’ character (Karun) by stripping all characters before the ‘Karun’ character that should be silenced

Parameters:

text (str) – Thai word

Returns:

Thai word with silent words stripped

Return type:

str

__dict__ = mappingproxy({'__module__': 'pythainlp.khavee.core', '__init__': <function KhaveeVerifier.__init__>, 'check_sara': <function KhaveeVerifier.check_sara>, 'check_marttra': <function KhaveeVerifier.check_marttra>, 'is_sumpus': <function KhaveeVerifier.is_sumpus>, 'check_karu_lahu': <function KhaveeVerifier.check_karu_lahu>, 'check_klon': <function KhaveeVerifier.check_klon>, 'check_aek_too': <function KhaveeVerifier.check_aek_too>, 'handle_karun_sound_silence': <function KhaveeVerifier.handle_karun_sound_silence>, '__dict__': <attribute '__dict__' of 'KhaveeVerifier' objects>, '__weakref__': <attribute '__weakref__' of 'KhaveeVerifier' objects>, '__doc__': None, '__annotations__': {}})
__module__ = 'pythainlp.khavee.core'

The KhaveeVerifier class is the primary component of the pythainlp.khavee module, dedicated to the verification of Thai poetry. It offers a range of functions and methods for analyzing and validating Thai poetry, ensuring its adherence to the rules and structure of classical Thai poetic forms.

Example

Here’s a basic example of how to use the KhaveeVerifier class to verify Thai poetry:

::

from pythainlp.khavee import KhaveeVerifier

# Initialize a KhaveeVerifier instance verifier = KhaveeVerifier()

# Text to verify poem_text = “ดอกไม้สวยงาม แสนสดใส”

# Verify if the text is Thai poetry is_poetry = verifier.is_khavee(poem_text)

print(f”The provided text is Thai poetry: {is_poetry}”)