Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/api/util.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ Modules

The `reorder_vowels` function is a text processing utility for reordering vowel characters in Thai text. It is essential for phonetic analysis and pronunciation guides.

.. autofunction:: rhyme
:noindex:


The `rhyme` function is a utility for find rhyme of Thai word.

.. autofunction:: sound_syllable
:noindex:

Expand Down
2 changes: 2 additions & 0 deletions pythainlp/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"remove_tonemark",
"remove_zw",
"reorder_vowels",
"rhyme",
"text_to_arabic_digit",
"text_to_thai_digit",
"thai_digit_to_arabic_digit",
Expand Down Expand Up @@ -126,3 +127,4 @@
from pythainlp.util.encoding import tis620_to_utf8
from pythainlp.util import spell_words
from pythainlp.util.abbreviation import abbreviation_to_full_text
from pythainlp.util.pronounce import rhyme
47 changes: 47 additions & 0 deletions pythainlp/util/pronounce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016-2023 PyThaiNLP Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List

from pythainlp.corpus import thai_words
from pythainlp.tokenize import syllable_tokenize
from pythainlp.khavee import KhaveeVerifier


kv = KhaveeVerifier()
all_thai_words_dict = [
i for i in list(thai_words()) if len(syllable_tokenize(i)) == 1
]


def rhyme(word: str) -> List[str]:
"""
Find Thai rhyme

:param str word: A Thai word
:return: All list Thai rhyme words
:rtype: List[str]

:Example:
::
from pythainlp.util import rhyme

print(rhyme("จีบ"))
# output: ['กลีบ', 'กีบ', 'ครีบ', ...]
"""
list_sumpus = []
for i in all_thai_words_dict:
if kv.is_sumpus(word, i) and i != word:
list_sumpus.append(i)
return sorted(list_sumpus)
5 changes: 5 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
remove_dup_spaces,
remove_tonemark,
remove_zw,
rhyme,
text_to_arabic_digit,
text_to_thai_digit,
thaiword_to_date,
Expand Down Expand Up @@ -853,5 +854,9 @@ def test_spell_word(self):
self.assertEqual(spell_word("คน"),['คอ', 'นอ', 'คน'])
self.assertEqual(spell_word("คนดี"),['คอ', 'นอ', 'คน', 'ดอ', 'อี', 'ดี', 'คนดี'])

def test_rhyme(self):
self.assertIsInstance(rhyme("แมว"), list)
self.assertTrue(len(rhyme("แมว")) > 2)

# def test_abbreviation_to_full_text(self):
# self.assertIsInstance(abbreviation_to_full_text("รร.ของเราน่าอยู่", list))