Skip to content

Commit fc91b16

Browse files
authored
Merge pull request #1194 from PyThaiNLP/copilot/improve-pythainlp-docs
Add missing docstrings and examples to utility and tokenization functions
2 parents 52b0ddc + f6225a5 commit fc91b16

6 files changed

Lines changed: 197 additions & 0 deletions

File tree

pythainlp/tokenize/core.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,26 @@ def word_tokenize(
329329

330330

331331
def indices_words(words):
332+
"""Convert a list of words to a list of character index pairs.
333+
334+
This function takes a list of words and returns the start and end
335+
character indices for each word in the original text.
336+
337+
:param list words: list of words
338+
:return: list of tuples (start_index, end_index) for each word
339+
:rtype: list[tuple[int, int]]
340+
341+
:Example:
342+
::
343+
344+
from pythainlp.tokenize import indices_words
345+
346+
indices_words(['สวัสดี', 'ครับ'])
347+
# output: [(0, 5), (6, 9)]
348+
349+
indices_words(['hello', 'world'])
350+
# output: [(0, 4), (5, 9)]
351+
"""
332352
indices = []
333353
start_index = 0
334354
for word in words:
@@ -340,6 +360,26 @@ def indices_words(words):
340360

341361

342362
def map_indices_to_words(index_list, sentences):
363+
"""Map character index pairs to actual words from sentences.
364+
365+
This function takes a list of character index pairs and a list of
366+
sentences, then extracts the corresponding words from the sentences.
367+
368+
:param list index_list: list of tuples (start_index, end_index)
369+
:param list sentences: list of sentences (strings)
370+
:return: list of lists containing extracted words for each sentence
371+
:rtype: list[list[str]]
372+
373+
:Example:
374+
::
375+
376+
from pythainlp.tokenize import map_indices_to_words
377+
378+
indices = [(0, 5), (6, 9)]
379+
sentences = ['สวัสดีครับ']
380+
map_indices_to_words(indices, sentences)
381+
# output: [['สวัสดี', 'ครับ']]
382+
"""
343383
result = []
344384
c = deque(index_list)
345385
n_sum = 0
@@ -735,6 +775,17 @@ def syllable_tokenize(
735775
<https://github.com/ponrawee/ssg>`_.
736776
* *tltk* - syllable tokenizer from tltk. See `tltk \
737777
<https://pypi.org/project/tltk/>`_.
778+
779+
:Example:
780+
::
781+
782+
from pythainlp.tokenize import syllable_tokenize
783+
784+
syllable_tokenize("สวัสดีครับ", engine="dict")
785+
# output: ['สวัส', 'ดี', 'ครับ']
786+
787+
syllable_tokenize("ประเทศไทย", engine="dict")
788+
# output: ['ประ', 'เทศ', 'ไทย']
738789
"""
739790
if engine not in ["dict", "han_solo", "ssg", "tltk"]:
740791
raise ValueError(
@@ -890,6 +941,15 @@ def word_tokenize(self, text: str) -> list[str]:
890941
:param str text: text to be tokenized
891942
:return: list of words, tokenized from the text
892943
:rtype: list[str]
944+
945+
:Example:
946+
::
947+
948+
from pythainlp.tokenize import Tokenizer
949+
950+
tokenizer = Tokenizer()
951+
tokenizer.word_tokenize("สวัสดีครับ")
952+
# output: ['สวัสดี', 'ครับ']
893953
"""
894954
return word_tokenize(
895955
text,
@@ -904,5 +964,15 @@ def set_tokenize_engine(self, engine: str) -> None:
904964
905965
:param str engine: choose between different options of tokenizer engines
906966
(i.e. *newmm*, *mm*, *longest*, *deepcut*)
967+
968+
:Example:
969+
::
970+
971+
from pythainlp.tokenize import Tokenizer
972+
973+
tokenizer = Tokenizer()
974+
tokenizer.set_tokenize_engine("newmm")
975+
tokenizer.word_tokenize("สวัสดีครับ")
976+
# output: ['สวัสดี', 'ครับ']
907977
"""
908978
self.__engine = engine

pythainlp/util/date.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ def convert_years(year: str, src="be", target="ad") -> str:
139139
because Thailand has change the Thai calendar in 1941.
140140
If you are the time traveler or the historian, \
141141
you should care about the correct calendar.
142+
143+
:Example:
144+
::
145+
146+
from pythainlp.util import convert_years
147+
148+
# Convert Buddhist Era (BE) to Anno Domini (AD)
149+
convert_years("2566", src="be", target="ad")
150+
# output: '2023'
151+
152+
# Convert AD to BE
153+
convert_years("2023", src="ad", target="be")
154+
# output: '2566'
155+
156+
# Convert BE to Rattanakosin Era (RE)
157+
convert_years("2566", src="be", target="re")
158+
# output: '242'
142159
"""
143160
output_year = None
144161
if src == "be":

pythainlp/util/digitconv.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ def arabic_digit_to_thai_digit(text: str) -> str:
117117
def digit_to_text(text: str) -> str:
118118
""":param str text: Text with digits such as '1', '2', '๓', '๔'
119119
:return: Text with digits spelled out in Thai
120+
121+
:Example:
122+
::
123+
124+
from pythainlp.util import digit_to_text
125+
126+
digit_to_text("เบอร์โทร 0812345678")
127+
# output: 'เบอร์โทร ศูนย์แปดหนึ่งสองสามสี่ห้าหกเจ็ดแปด'
128+
129+
digit_to_text("123")
130+
# output: 'หนึ่งสองสาม'
131+
132+
digit_to_text("๕๖๗")
133+
# output: 'ห้าหกเจ็ด'
120134
"""
121135
if not text or not isinstance(text, str):
122136
raise TypeError("The text must be str type.")

pythainlp/util/normalize.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ def remove_zw(text: str) -> str:
153153
:param str text: input text
154154
:return: text without zero-width characters
155155
:rtype: str
156+
157+
:Example:
158+
::
159+
160+
from pythainlp.util import remove_zw
161+
162+
remove_zw("สวัสดี\u200bครับ")
163+
# output: 'สวัสดีครับ'
164+
165+
remove_zw("ภาษา\u200cไทย")
166+
# output: 'ภาษาไทย'
156167
"""
157168
for ch in _ZERO_WIDTH_CHARS:
158169
while ch in text:
@@ -175,6 +186,19 @@ def reorder_vowels(text: str) -> str:
175186
:param str text: input text
176187
:return: text with vowels and tone marks in the standard logical order
177188
:rtype: str
189+
190+
:Example:
191+
::
192+
193+
from pythainlp.util import reorder_vowels
194+
195+
# Two Sara E become Sara Ae
196+
reorder_vowels("เเปลก")
197+
# output: 'แปลก'
198+
199+
# Reorder tone marks and vowels
200+
reorder_vowels("ก้ำ")
201+
# output: 'กำ้'
178202
"""
179203
for pair in _REORDER_PAIRS:
180204
text = re.sub(pair[0], pair[1], text)
@@ -191,6 +215,17 @@ def remove_repeat_vowels(text: str) -> str:
191215
:param str text: input text
192216
:return: text without repeating Thai vowels, tone marks, and signs
193217
:rtype: str
218+
219+
:Example:
220+
::
221+
222+
from pythainlp.util import remove_repeat_vowels
223+
224+
remove_repeat_vowels("นานาาา")
225+
# output: 'นานา'
226+
227+
remove_repeat_vowels("ดีีีี")
228+
# output: 'ดี'
194229
"""
195230
text = reorder_vowels(text)
196231
for pair in _NOREPEAT_PAIRS:

pythainlp/util/thai_lunar_date.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,23 @@ def th_zodiac(year: int, output_type: int = 1) -> str | int:
315315
316316
:return: The Zodiac name or number corresponding to the input year.
317317
:rtype: Union[str, int]
318+
319+
:Example:
320+
::
321+
322+
from pythainlp.util import th_zodiac
323+
324+
# Get Thai zodiac name
325+
th_zodiac(2024, output_type=1)
326+
# output: 'มะโรง'
327+
328+
# Get English zodiac name
329+
th_zodiac(2024, output_type=2)
330+
# output: 'DRAGON'
331+
332+
# Get zodiac number
333+
th_zodiac(2024, output_type=3)
334+
# output: 5
318335
"""
319336
# Calculate zodiac index
320337
result = year % 12
@@ -333,6 +350,18 @@ def to_lunar_date(input_date: date) -> str:
333350
:param date input_date: date of the day.
334351
:return: Thai text lunar date
335352
:rtype: str
353+
354+
:Example:
355+
::
356+
357+
from pythainlp.util import to_lunar_date
358+
from datetime import date
359+
360+
to_lunar_date(date(2024, 1, 1))
361+
# output: 'แรม 5 ค่ำ เดือน 1'
362+
363+
to_lunar_date(date(2024, 12, 31))
364+
# output: 'แรม 9 ค่ำ เดือน 2'
336365
"""
337366
# Check if date is within supported range
338367
if input_date.year < 1903 or input_date.year > 2460:

pythainlp/util/trie.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@
1212

1313

1414
class Trie(Iterable[str]):
15+
"""Trie data structure for efficient prefix-based word search.
16+
17+
A Trie (prefix tree) is a tree-like data structure used to store
18+
a collection of strings. It enables fast retrieval of words with
19+
common prefixes, making it ideal for dictionary-based tokenization
20+
and autocomplete features.
21+
22+
:param Iterable[str] words: An iterable collection of words to initialize the Trie
23+
24+
:Example:
25+
::
26+
27+
from pythainlp.util import Trie
28+
29+
# Create a trie with Thai words
30+
trie = Trie(['สวัสดี', 'สวัส', 'ดี', 'ครับ'])
31+
32+
# Check if word exists
33+
'สวัสดี' in trie
34+
# output: True
35+
36+
# Find all prefixes of a word
37+
trie.prefixes('สวัสดีครับ')
38+
# output: ['สวัส', 'สวัสดี']
39+
40+
# Add a new word
41+
trie.add('สวัสดีตอนเช้า')
42+
43+
# Get number of words in trie
44+
len(trie)
45+
# output: 5
46+
"""
1547
class Node:
1648
__slots__ = "end", "children"
1749

0 commit comments

Comments
 (0)