Skip to content

Commit 0207d40

Browse files
authored
Merge pull request #1392 from PyThaiNLP/copilot/update-documentation-and-doctests
docs: convert :Example: RST code blocks to proper Python doctest format
2 parents 9dfae5a + 7892663 commit 0207d40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+1922
-2363
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ and this project adheres to
1717
- Full release notes: <https://github.com/PyThaiNLP/pythainlp/releases>
1818
- Commit history: <https://github.com/PyThaiNLP/pythainlp/compare/v5.3.1...v5.3.3>
1919

20+
## [Unreleased]
21+
22+
### Changed
23+
24+
- Fix doctests, docstring formatting, typos, and outdated content
25+
across all modules (#1392).
26+
2027
## [5.3.3] - 2026-03-26
2128

2229
Security fixes and thai2rom_onnx bug fixes.

pythainlp/ancient/aksonhan.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,24 @@
2929
def aksonhan_to_current(word: str) -> str:
3030
"""Convert AksonHan words to current Thai words
3131
32-
AksonHan (อักษรหัน) writes down two consonants for the \
33-
spelling of the /a/ vowels. (สระ อะ).
32+
AksonHan (อักษรหัน) writes two consonants to spell
33+
the short /a/ vowel (สระ อะ).
3434
35-
Today, รร is an aksonHan word that is still used in Thai.
35+
Today, รร is an aksonhan pattern still used in Thai.
3636
3737
:param str word: Thai word
3838
:return: Thai AksonHan to be converted to current Thai word
3939
:rtype: str
4040
4141
:Example:
42-
::
4342
44-
from pythainlp.ancient import aksonhan_to_current
45-
46-
print(aksonhan_to_current("จกก"))
47-
# output: จัก
48-
49-
print(aksonhan_to_current("บงงคบบ"))
50-
# output: บังคับ
51-
52-
print(aksonhan_to_current("สรรเพชญ")) # รร is still used.
53-
# output: สรรเพชญ
43+
>>> from pythainlp.ancient import aksonhan_to_current
44+
>>> print(aksonhan_to_current("จกก"))
45+
จัก
46+
>>> print(aksonhan_to_current("บงงคบบ"))
47+
บังคับ
48+
>>> print(aksonhan_to_current("สรรเพชญ")) # รร is still used.
49+
สรรเพชญ
5450
5551
"""
5652
if len(word) < 3:

pythainlp/ancient/currency.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,10 @@ def convert_currency(value: float, from_unit: str) -> dict[str, float]:
2626
:rtype: dict[str, float]
2727
2828
:Example:
29-
::
3029
31-
from pythainlp.ancient import convert_currency
32-
33-
print(convert_currency(8, "บาท"))
34-
# output:
35-
# {
36-
# 'เบี้ย': 51200.0,
37-
# 'อัฐ': 512.0,
38-
# 'ไพ': 256.0,
39-
# 'เฟื้อง': 64.0,
40-
# 'สลึง': 32.0,
41-
# 'บาท': 8.0,
42-
# 'ตำลึง': 2.0,
43-
# 'ชั่ง': 0.1
44-
# }
30+
>>> from pythainlp.ancient import convert_currency
31+
>>> print(convert_currency(8, "บาท"))
32+
{'เบี้ย': 51200.0, 'อัฐ': 512.0, 'ไพ': 256.0, 'เฟื้อง': 64.0, 'สลึง': 32.0, 'บาท': 8.0, 'ตำลึง': 2.0, 'ชั่ง': 0.1}
4533
4634
"""
4735
conversion_factors_to_att = {
@@ -57,7 +45,7 @@ def convert_currency(value: float, from_unit: str) -> dict[str, float]:
5745

5846
if from_unit not in conversion_factors_to_att:
5947
raise NotImplementedError(
60-
f"Currency unit '{from_unit}' is not support."
48+
f"Currency unit '{from_unit}' is not supported."
6149
)
6250

6351
# start from 'อัฐ'

pythainlp/augment/lm/phayathaibert.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,13 @@ def augment(
8080
:rtype: List[str]
8181
8282
:Example:
83-
::
8483
85-
from pythainlp.augment.lm import ThaiTextAugmenter
84+
>>> from pythainlp.augment.lm import ThaiTextAugmenter # doctest: +SKIP
8685
87-
aug = ThaiTextAugmenter()
88-
aug.augment("ช้างมีทั้งหมด 50 ตัว บน", num_args=5)
86+
>>> aug = ThaiTextAugmenter() # doctest: +SKIP
87+
>>> aug.augment("ช้างมีทั้งหมด 50 ตัว บน", num_args=5) # doctest: +SKIP
8988
90-
# output = ['ช้างมีทั้งหมด 50 ตัว บนโลกใบนี้ครับ.',
89+
['ช้างมีทั้งหมด 50 ตัว บนโลกใบนี้ครับ.',
9190
'ช้างมีทั้งหมด 50 ตัว บนพื้นดินครับ...',
9291
'ช้างมีทั้งหมด 50 ตัว บนท้องฟ้าครับ...',
9392
'ช้างมีทั้งหมด 50 ตัว บนดวงจันทร์.‼',

pythainlp/augment/lm/wangchanberta.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,13 @@ def augment(self, sentence: str, num_replace_tokens: int = 3) -> list[str]:
7676
:rtype: List[str]
7777
7878
:Example:
79-
::
8079
81-
from pythainlp.augment.lm import Thai2transformersAug
80+
>>> from pythainlp.augment.lm import Thai2transformersAug # doctest: +SKIP
8281
83-
aug = Thai2transformersAug()
82+
>>> aug = Thai2transformersAug() # doctest: +SKIP
8483
85-
aug.augment("ช้างมีทั้งหมด 50 ตัว บน")
86-
# output: ['ช้างมีทั้งหมด 50 ตัว บนโลกใบนี้',
84+
>>> aug.augment("ช้างมีทั้งหมด 50 ตัว บน") # doctest: +SKIP
85+
['ช้างมีทั้งหมด 50 ตัว บนโลกใบนี้',
8786
'ช้างมีทั้งหมด 50 ตัว บนสุด',
8887
'ช้างมีทั้งหมด 50 ตัว บนบก',
8988
'ช้างมีทั้งหมด 50 ตัว บนนั้น',

pythainlp/augment/word2vec/bpemb_wv.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ def augment(
6060
:return: list of synonyms
6161
:rtype: list[str]
6262
:Example:
63-
::
6463
65-
from pythainlp.augment.word2vec.bpemb_wv import BPEmbAug
64+
>>> from pythainlp.augment.word2vec.bpemb_wv import BPEmbAug # doctest: +SKIP
6665
67-
aug = BPEmbAug()
68-
aug.augment("ผมเรียน", n_sent=2, p=0.5)
69-
# output: ['ผมสอน', 'ผมเข้าเรียน']
66+
>>> aug = BPEmbAug() # doctest: +SKIP
67+
>>> aug.augment("ผมเรียน", n_sent=2, p=0.5) # doctest: +SKIP
68+
['ผมสอน', 'ผมเข้าเรียน']
7069
"""
7170
self.sentence: str = sentence.replace(" ", "▁")
7271
self.temp: list[tuple[str, ...]] = self.aug.augment(

pythainlp/augment/word2vec/ltw2v.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ def augment(
5656
:rtype: List[Tuple[str]]
5757
5858
:Example:
59-
::
6059
61-
from pythainlp.augment.word2vec import LTW2VAug
60+
>>> from pythainlp.augment.word2vec import LTW2VAug # doctest: +SKIP
6261
63-
aug = LTW2VAug()
64-
aug.augment("ผมเรียน", n_sent=2, p=0.5)
65-
# output: [('เขา', 'เรียนหนังสือ'), ('เขา', 'สมัครเรียน')]
62+
>>> aug = LTW2VAug() # doctest: +SKIP
63+
>>> aug.augment("ผมเรียน", n_sent=2, p=0.5) # doctest: +SKIP
64+
[('เขา', 'เรียนหนังสือ'), ('เขา', 'สมัครเรียน')]
6665
"""
6766
return self.aug.augment(sentence, n_sent, p)

pythainlp/augment/word2vec/thai2fit.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ def augment(
5757
:rtype: List[Tuple[str]]
5858
5959
:Example:
60-
::
6160
62-
from pythainlp.augment.word2vec import Thai2fitAug
61+
>>> from pythainlp.augment.word2vec import Thai2fitAug # doctest: +SKIP
6362
64-
aug = Thai2fitAug()
65-
aug.augment("ผมเรียน", n_sent=2, p=0.5)
66-
# output: [('พวกเรา', 'เรียน'), ('ฉัน', 'เรียน')]
63+
>>> aug = Thai2fitAug() # doctest: +SKIP
64+
>>> aug.augment("ผมเรียน", n_sent=2, p=0.5) # doctest: +SKIP
65+
[('พวกเรา', 'เรียน'), ('ฉัน', 'เรียน')]
6766
"""
6867
return self.aug.augment(sentence, n_sent, p)

pythainlp/augment/wordnet.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,12 @@ def augment(
191191
:rtype: list[list[str]]
192192
193193
:Example:
194-
::
195194
196-
from pythainlp.augment import WordNetAug
195+
>>> from pythainlp.augment import WordNetAug # doctest: +SKIP
197196
198-
aug = WordNetAug()
199-
aug.augment("เราชอบไปโรงเรียน")
200-
# output: [('เรา', 'ชอบ', 'ไป', 'ร.ร.'),
197+
>>> aug = WordNetAug() # doctest: +SKIP
198+
>>> aug.augment("เราชอบไปโรงเรียน") # doctest: +SKIP
199+
[('เรา', 'ชอบ', 'ไป', 'ร.ร.'),
201200
('เรา', 'ชอบ', 'ไป', 'รร.'),
202201
('เรา', 'ชอบ', 'ไป', 'โรงเรียน'),
203202
('เรา', 'ชอบ', 'ไป', 'อาคารเรียน'),

pythainlp/benchmarks/metrics.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,22 @@ def bleu_score(
131131
:rtype: BleuScore
132132
133133
:Example:
134-
::
135134
136-
from pythainlp.benchmarks import bleu_score
137-
138-
references = ["สวัสดีครับ วันนี้อากาศดีมาก"]
139-
hypotheses = ["สวัสดีค่ะ วันนี้อากาศดี"]
140-
score = bleu_score(references, hypotheses)
141-
print(f"BLEU score: {score['bleu']:.2f}")
142-
143-
::
144-
145-
# Multiple references per hypothesis
146-
references = [
147-
["สวัสดีครับ", "สวัสดีค่ะ"], # two refs for first hypothesis
148-
["ลาก่อนครับ", "ลาก่อนค่ะ"], # two refs for second hypothesis
149-
]
150-
hypotheses = ["สวัสดี", "ลาก่อน"]
151-
score = bleu_score(references, hypotheses)
135+
>>> from pythainlp.benchmarks import bleu_score
136+
137+
>>> references = ["สวัสดีครับ วันนี้อากาศดีมาก"]
138+
>>> hypotheses = ["สวัสดีค่ะ วันนี้อากาศดี"]
139+
>>> score = bleu_score(references, hypotheses)
140+
>>> print(f"BLEU score: {score['bleu']:.2f}")
141+
BLEU score: 28.12
142+
143+
>>> # Multiple references per hypothesis
144+
>>> references = [
145+
... ["สวัสดีครับ", "สวัสดีค่ะ"], # two refs for first hypothesis
146+
... ["ลาก่อนครับ", "ลาก่อนค่ะ"], # two refs for second hypothesis
147+
... ]
148+
>>> hypotheses = ["สวัสดี", "ลาก่อน"]
149+
>>> score = bleu_score(references, hypotheses)
152150
"""
153151
from pythainlp.tokenize import word_tokenize
154152

@@ -282,16 +280,18 @@ def rouge_score(
282280
:rtype: dict[str, RougeScore]
283281
284282
:Example:
285-
::
286-
287-
from pythainlp.benchmarks import rouge_score
288283
289-
reference = "สวัสดีครับ วันนี้อากาศดีมาก"
290-
hypothesis = "สวัสดีค่ะ วันนี้อากาศดี"
291-
scores = rouge_score(reference, hypothesis)
292-
print(f"ROUGE-1 F-measure: {scores['rouge1']['fmeasure']:.4f}")
293-
print(f"ROUGE-2 F-measure: {scores['rouge2']['fmeasure']:.4f}")
294-
print(f"ROUGE-L F-measure: {scores['rougeL']['fmeasure']:.4f}")
284+
>>> from pythainlp.benchmarks import rouge_score
285+
286+
>>> reference = "สวัสดีครับ วันนี้อากาศดีมาก"
287+
>>> hypothesis = "สวัสดีค่ะ วันนี้อากาศดี"
288+
>>> scores = rouge_score(reference, hypothesis)
289+
>>> print(f"ROUGE-1 F-measure: {scores['rouge1']['fmeasure']:.4f}")
290+
ROUGE-1 F-measure: 0.6000
291+
>>> print(f"ROUGE-2 F-measure: {scores['rouge2']['fmeasure']:.4f}")
292+
ROUGE-2 F-measure: 0.2500
293+
>>> print(f"ROUGE-L F-measure: {scores['rougeL']['fmeasure']:.4f}")
294+
ROUGE-L F-measure: 0.6000
295295
"""
296296
from pythainlp.tokenize import word_tokenize
297297

@@ -391,14 +391,14 @@ def word_error_rate(
391391
:rtype: float
392392
393393
:Example:
394-
::
395394
396-
from pythainlp.benchmarks import word_error_rate
395+
>>> from pythainlp.benchmarks import word_error_rate
397396
398-
reference = "สวัสดีครับ วันนี้อากาศดีมาก"
399-
hypothesis = "สวัสดีค่ะ วันนี้อากาศดี"
400-
wer = word_error_rate(reference, hypothesis)
401-
print(f"WER: {wer:.4f}")
397+
>>> reference = "สวัสดีครับ วันนี้อากาศดีมาก"
398+
>>> hypothesis = "สวัสดีค่ะ วันนี้อากาศดี"
399+
>>> wer = word_error_rate(reference, hypothesis)
400+
>>> print(f"WER: {wer:.4f}")
401+
WER: 0.4000
402402
"""
403403
from pythainlp.tokenize import word_tokenize
404404

@@ -469,14 +469,14 @@ def character_error_rate(
469469
:rtype: float
470470
471471
:Example:
472-
::
473472
474-
from pythainlp.benchmarks import character_error_rate
473+
>>> from pythainlp.benchmarks import character_error_rate
475474
476-
reference = "สวัสดีครับ"
477-
hypothesis = "สวัสดีค่ะ"
478-
cer = character_error_rate(reference, hypothesis)
479-
print(f"CER: {cer:.4f}")
475+
>>> reference = "สวัสดีครับ"
476+
>>> hypothesis = "สวัสดีค่ะ"
477+
>>> cer = character_error_rate(reference, hypothesis)
478+
>>> print(f"CER: {cer:.4f}")
479+
CER: 0.3000
480480
"""
481481
# Work with characters directly (no tokenization needed)
482482
ref_chars = list(reference)

0 commit comments

Comments
 (0)