Skip to content

Commit b7ff45e

Browse files
committed
- simplify bahttext code, remove unused code
- minimized if __name__ == "__main__" part
1 parent cc601f1 commit b7ff45e

File tree

6 files changed

+38
-93
lines changed

6 files changed

+38
-93
lines changed

pythainlp/number/thainum.py

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
Adapted from
66
http://justmindthought.blogspot.com/2012/12/code-php.html
77
"""
8-
import ast
98
import math
109

1110
__all__ = ["bahttext", "num_to_thaiword"]
1211

13-
p = [
12+
_p = [
1413
["ภาษาไทย", "ตัวเลข", "เลขไทย"],
1514
["หนึ่ง", "1", "๑"],
1615
["สอง", "2", "๒"],
@@ -22,18 +21,15 @@
2221
["แปด", "8", "๘"],
2322
["เก้า", "9", "๙"],
2423
]
25-
thaitonum = dict((x[2], x[1]) for x in p[1:])
26-
p1 = dict((x[0], x[1]) for x in p[1:])
27-
d1 = 0
2824

2925

30-
# เลขไทยสู่เลข
26+
# เลขไทยสู่เลขอารบิก
3127
def thai_num_to_num(text):
3228
"""
3329
:param str text: Thai number characters such as '๑', '๒', '๓'
3430
:return: universal numbers such as '1', '2', '3'
3531
"""
36-
thaitonum = dict((x[2], x[1]) for x in p[1:])
32+
thaitonum = dict((x[2], x[1]) for x in _p[1:])
3733
return thaitonum[text]
3834

3935

@@ -42,7 +38,7 @@ def thai_num_to_text(text):
4238
:param str text: Thai number characters such as '๑', '๒', '๓'
4339
:return: Thai numbers, spelled out in Thai
4440
"""
45-
thaitonum = dict((x[2], x[0]) for x in p[1:])
41+
thaitonum = dict((x[2], x[0]) for x in _p[1:])
4642
return thaitonum[text]
4743

4844

@@ -51,7 +47,7 @@ def num_to_thai_num(text):
5147
:param text: universal numbers such as '1', '2', '3'
5248
:return: Thai number characters such as '๑', '๒', '๓'
5349
"""
54-
thaitonum = dict((x[1], x[2]) for x in p[1:])
50+
thaitonum = dict((x[1], x[2]) for x in _p[1:])
5551
return thaitonum[text]
5652

5753

@@ -60,7 +56,7 @@ def num_to_text(text):
6056
:param text: universal numbers such as '1', '2', '3'
6157
:return: Thai numbers, spelled out in Thai
6258
"""
63-
thaitonum = dict((x[1], x[0]) for x in p[1:])
59+
thaitonum = dict((x[1], x[0]) for x in _p[1:])
6460
return thaitonum[text]
6561

6662

@@ -69,7 +65,7 @@ def text_to_num(text):
6965
:param text: Thai numbers, spelled out in Thai
7066
:return: universal numbers such as '1', '2', '3'
7167
"""
72-
thaitonum = dict((x[0], x[1]) for x in p[1:])
68+
thaitonum = dict((x[0], x[1]) for x in _p[1:])
7369
return thaitonum[text]
7470

7571

@@ -78,47 +74,34 @@ def text_to_thai_num(text):
7874
:param text: Thai numbers, spelled out in Thai
7975
:return: Thai numbers such as '๑', '๒', '๓'
8076
"""
81-
thaitonum = dict((x[0], x[2]) for x in p[1:])
77+
thaitonum = dict((x[0], x[2]) for x in _p[1:])
8278
return thaitonum[text]
8379

8480

85-
def number_format(num, places=0):
86-
return "{:20,.2f}".format(num)
87-
88-
89-
def bahttext(amount_number):
81+
def bahttext(number):
9082
"""
9183
Converts a number to Thai text and adds a suffix of "Baht" currency.
84+
Precision will be fixed at two decimal places (0.00) to fits "Satang" unit.
9285
9386
Similar to BAHTTEXT function in Excel
9487
"""
9588
ret = ""
9689

97-
if amount_number is None:
90+
if number is None:
9891
pass
99-
elif amount_number == 0:
92+
elif number == 0:
10093
ret = "ศูนย์บาทถ้วน"
10194
else:
102-
amount_number = number_format(amount_number, 2).replace(" ", "")
103-
pt = amount_number.find(".")
104-
number, fraction = "", ""
105-
amount_number1 = amount_number.split(".")
106-
107-
if not pt:
108-
number = amount_number
109-
else:
110-
amount_number = amount_number.split(".")
111-
number = amount_number[0]
112-
fraction = int(amount_number1[1])
113-
114-
number = ast.literal_eval(number.replace(",", ""))
95+
num_int, num_dec = "{:.2f}".format(number).split(".")
96+
num_int = int(num_int)
97+
num_dec = int(num_dec)
11598

116-
baht = num_to_thaiword(number)
117-
if baht != "":
99+
baht = num_to_thaiword(num_int)
100+
if baht:
118101
ret = "".join([ret, baht, "บาท"])
119102

120-
satang = num_to_thaiword(fraction)
121-
if satang != "" and satang != "ศูนย์":
103+
satang = num_to_thaiword(num_dec)
104+
if satang and satang != "ศูนย์":
122105
ret = "".join([ret, satang, "สตางค์"])
123106
else:
124107
ret = "".join([ret, "ถ้วน"])
@@ -139,7 +122,18 @@ def num_to_thaiword(number):
139122
ret = "ศูนย์"
140123
else:
141124
_POS_CALL = ["แสน", "หมื่น", "พัน", "ร้อย", "สิบ", ""]
142-
_NUM_CALL = ["", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า"]
125+
_NUM_CALL = [
126+
"",
127+
"หนึ่ง",
128+
"สอง",
129+
"สาม",
130+
"สี่",
131+
"ห้า",
132+
"หก",
133+
"เจ็ด",
134+
"แปด",
135+
"เก้า",
136+
]
143137

144138
if number > 1000000:
145139
ret += num_to_thaiword(int(number / 1000000)) + "ล้าน"

pythainlp/rank/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
_STOPWORDS = thai_stopwords()
77

8+
89
# เรียงจำนวนคำของประโยค
910
def rank(data, stopword=False):
1011
"""
@@ -20,5 +21,4 @@ def rank(data, stopword=False):
2021

2122

2223
if __name__ == "__main__":
23-
text = ["แมว", "ชอบ", "ปลา", "แมว", "ชอบ", "นอน", "คน", "เป็น", "ทาส", "แมว"]
24-
print(rank(text))
24+
print(rank(["แมว", "ชอบ", "ปลา", "แมว", "ชอบ", "นอน", "คน", "เป็น", "ทาส", "แมว"]))

pythainlp/romanization/royin.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -180,41 +180,4 @@ def romanize(word):
180180

181181

182182
if __name__ == "__main__":
183-
print(romanize("แมว") == "maeo")
184-
print(romanize("น้าว") == "nao")
185-
print(romanize("รวม") == "ruam")
186-
print(romanize("ไทย") == "thai")
187-
print(romanize("ผัวะ") == "phua")
188-
print(romanize("ใย") == "yai")
189-
print(romanize("ไล่") == "lai")
190-
print(romanize("เมา") == "mao")
191-
print(romanize("ต้น") == "ton")
192-
print(romanize("ตาล") == "tan")
193-
print(romanize("แสง") == "saeng")
194-
print(romanize("เลียน") == "lian")
195-
print(romanize("เลือก") == "lueak")
196-
print(romanize("เธอ") == "thoe")
197-
print(romanize("หรู") == "ru")
198-
print(romanize("ลอม") == "lom")
199-
print(romanize("และ") == "lae")
200-
print(romanize("เลาะ") == "lo")
201-
print(romanize("ลอม") == "lom")
202-
print(romanize("เล็ง") == "leng")
203-
print(romanize("นึก") == "nuek")
204-
print(romanize("มัว") == "mua")
205-
print(romanize("มีด") == "mit")
206-
print(romanize("โค") == "kho")
207-
print(romanize("ขอ") == "kho")
208-
print(romanize("วรร") == "wan")
209-
print(romanize("สรรพ") == "sap")
210-
print(romanize("วัน") + romanize("นะ") + romanize("พง"))
211-
print(romanize("นัด") + romanize("ชะ") + romanize("โนน"))
212-
print(romanize("สรรพ"))
213-
print(romanize("สรร") + romanize("หา"))
214-
print(romanize("สรร") + romanize("หา"))
215-
print(romanize("แมว"))
216183
print(romanize("กร") == romanize("กอน"))
217-
print(romanize("คฤ") + romanize("หาสน์"))
218-
print(romanize("กฤ") + romanize("ศะ") + romanize("ฎา"))
219-
print(romanize("ฤกษ์"))
220-
print(romanize("ฤ") + romanize("ดู") + romanize("กาล"))

pythainlp/sentiment/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ def sentiment(text, engine="old"):
3939

4040
return "pos" if tag else "neg"
4141
else: # default, use "old" vocabulary-based engine
42-
with open(
43-
os.path.join(_SENTIMENT_PATH, "vocabulary.data"), "rb"
44-
) as in_strm:
42+
with open(os.path.join(_SENTIMENT_PATH, "vocabulary.data"), "rb") as in_strm:
4543
vocabulary = dill.load(in_strm)
4644

47-
with open(
48-
os.path.join(_SENTIMENT_PATH, "sentiment.data"), "rb"
49-
) as in_strm:
45+
with open(os.path.join(_SENTIMENT_PATH, "sentiment.data"), "rb") as in_strm:
5046
classifier = dill.load(in_strm)
5147

5248
text = set(word_tokenize(text)) - _STOPWORDS

pythainlp/tokenize/pyicu.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,4 @@ def segment(text):
3131

3232

3333
if __name__ == "__main__":
34-
print(segment("ทดสอบระบบตัดคำด้วยไอซียู"))
35-
print(segment("ผมชอบพูดไทยคำEnglishคำ"))
36-
print(segment("ไทยEnglish540บาท"))
37-
print(segment("ประหยัด ไฟเบอห้า"))
34+
print(segment("พูดไทย2คำEnglishคำ"))

pythainlp/util/keyboard.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,5 @@ def thai_to_eng(text):
124124

125125

126126
if __name__ == "__main__":
127-
a = "l;ylfu8iy["
128-
a = eng_to_thai(a)
129-
a = eng_to_thai(a)
130-
b = "นามรสนอำันี"
131-
b = thai_to_eng(b)
132-
print(a)
133-
print(b)
127+
print(eng_to_thai("l;ylfu8iy["))
128+
print(thai_to_eng("นามรสนอำันี"))

0 commit comments

Comments
 (0)