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
10 changes: 6 additions & 4 deletions pythainlp/classify/param_free.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ def train(self):
return temp_list

def predict(self, x1: str, k: int = 1) -> str:
""":param str x1: the text that we want to predict label for.
:param str k: k
:return: label
"""Predict the label for the given text.

:param str x1: the text that we want to predict label for
:param int k: number of nearest neighbors to consider (default: 1)
:return: predicted label
:rtype: str

:Example:
Expand Down Expand Up @@ -80,7 +82,7 @@ def predict(self, x1: str, k: int = 1) -> str:
sorted_idx = np.argsort(np.array(disance_from_x1))
top_k_class = self.training_data[sorted_idx[:k], 1]
_, counts = np.unique(top_k_class, return_counts=True)
predict_class = top_k_class[counts.argmax()]
predict_class = str(top_k_class[counts.argmax()])

return predict_class

Expand Down
8 changes: 5 additions & 3 deletions pythainlp/transliterate/pyicu.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

def transliterate(text: str) -> str:
"""Use ICU (International Components for Unicode) for transliteration
:param str text: Thai text to be transliterated.
:return: A string of Internaitonal Phonetic Alphabets indicating how the text should be pronounced.

:param str text: Thai text to be transliterated
:return: A string of International Phonetic Alphabets indicating how the text should be pronounced
:rtype: str
"""
return _ICU_THAI_TO_LATIN.transliterate(text)
return str(_ICU_THAI_TO_LATIN.transliterate(text))
23 changes: 17 additions & 6 deletions pythainlp/util/spell_words.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import re
from functools import lru_cache
from typing import Optional

from pythainlp import (
thai_above_vowels,
Expand All @@ -24,8 +25,12 @@
for i, j in zip(list(thai_tonemarks), ["เอก", "โท", "ตรี", "จัตวา"])
}

rule1: list[str] = [i.replace("-", f"([{thai_letters}](thai_tonemarks)?)") for i in _r1]
rule2: list[str] = [i.replace("–", f"([{thai_letters}])").replace(":", "") for i in _r2]
rule1: list[str] = [
i.replace("-", f"([{thai_letters}](thai_tonemarks)?)") for i in _r1
]
rule2: list[str] = [
i.replace("–", f"([{thai_letters}])").replace(":", "") for i in _r2
]
rule3: list[str] = [
i.replace("–", f"([{thai_letters}])").replace(":", f"([{thai_tonemarks}])")
for i in _r2
Expand Down Expand Up @@ -108,12 +113,12 @@ def spell_syllable(text: str) -> list[str]:
return c_only + v_only + t_only + [text]


def spell_word(text: str) -> list[str]:
def spell_word(text: Optional[str]) -> list[str]:
"""Spell out words in Thai word distribution form.

:param str w: Thai words only
:return: List of spelled out words
:rtype: List[str]
:param Optional[str] text: Thai words only, or None
:return: List of spelled out words, empty list if text is None or empty
:rtype: list[str]

:Example:
::
Expand All @@ -122,7 +127,13 @@ def spell_word(text: str) -> list[str]:

print(spell_word("คนดี"))
# output: ['คอ', 'นอ', 'คน', 'ดอ', 'อี', 'ดี', 'คนดี']

print(spell_word(None))
# output: []
"""
if not text:
return []

spellouts = []
tokens = subword_tokenize(text, engine="han_solo")

Expand Down
21 changes: 14 additions & 7 deletions pythainlp/util/thai.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Check if it is Thai text
"""
"""Check if it is Thai text"""

from __future__ import annotations

import string
from collections import defaultdict
from typing import Optional

from pythainlp import (
thai_above_vowels,
Expand Down Expand Up @@ -215,16 +215,17 @@ def display_thai_char(ch: str) -> str:
return ch


def thai_word_tone_detector(word: str) -> list[tuple[str, str]]:
def thai_word_tone_detector(word: Optional[str]) -> list[tuple[str, str]]:
"""Thai tone detector for word.

It uses pythainlp.transliterate.pronunciate for converting word to\
pronunciation.

:param str word: Thai word.
:return: Thai pronunciation with tones in each syllable.\
(l, m, h, r, f or empty if it cannot be detected)
:rtype: Tuple[str, str]
:param Optional[str] word: Thai word, or None
:return: List of tuples containing Thai pronunciation with tones in each syllable.\
Tone values: l (low), m (mid), h (high), r (rising), f (falling), or empty if it cannot be detected.\
Returns [] if word is None or empty.
:rtype: list[tuple[str, str]]

:Example:
::
Expand All @@ -236,7 +237,13 @@ def thai_word_tone_detector(word: str) -> list[tuple[str, str]]:

print(thai_word_tone_detector("มือถือ"))
# output: [('มือ', 'm'), ('ถือ', 'r')]

print(thai_word_tone_detector(None))
# output: []
"""
if not word:
return []

from ..transliterate import pronunciate
from ..util.syllable import tone_detector

Expand Down
4 changes: 2 additions & 2 deletions tests/compact/testc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ def test_thai_word_tone_detector(self):
thai_word_tone_detector("ราคา"), [("รา", "m"), ("คา", "m")]
)
# Edge cases: None and empty string
self.assertEqual(thai_word_tone_detector(None), [("", "")])
self.assertEqual(thai_word_tone_detector(""), [("", "")])
self.assertEqual(thai_word_tone_detector(None), [])
self.assertEqual(thai_word_tone_detector(""), [])
Loading