Spaces:
Runtime error
Runtime error
Commit ·
1b3124c
1
Parent(s): 2f990e6
test: updated text format and unittest
Browse files- app.py +1 -5
- tests/test_thai_word.py +43 -50
- utils/thai_word.py +6 -4
app.py
CHANGED
|
@@ -39,11 +39,7 @@ def transcribe(audio):
|
|
| 39 |
# pretty text
|
| 40 |
tokens = word_tokenize(text, engine="attacut", join_broken_num=True)
|
| 41 |
print(tokens)
|
| 42 |
-
|
| 43 |
-
if len(tokens) > 0:
|
| 44 |
-
result = f'pretty: {thw.pretty(deque(deepcopy(tokens)))}\n\n original: {text}'
|
| 45 |
-
else:
|
| 46 |
-
result = 'pretty: ไม่สามารถตัดคำได้'
|
| 47 |
else:
|
| 48 |
result = 'โปรดลองพูดอีกครั้ง'
|
| 49 |
except Exception as e:
|
|
|
|
| 39 |
# pretty text
|
| 40 |
tokens = word_tokenize(text, engine="attacut", join_broken_num=True)
|
| 41 |
print(tokens)
|
| 42 |
+
result = f'pretty: {thw.pretty(deque(deepcopy(tokens)))}\n\n original: {text}'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
else:
|
| 44 |
result = 'โปรดลองพูดอีกครั้ง'
|
| 45 |
except Exception as e:
|
tests/test_thai_word.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import unittest
|
| 2 |
from utils.thai_word import ThaiWord
|
|
|
|
| 3 |
|
| 4 |
class TestThaiWord(unittest.TestCase):
|
| 5 |
|
|
@@ -7,65 +8,57 @@ class TestThaiWord(unittest.TestCase):
|
|
| 7 |
self.thw = ThaiWord()
|
| 8 |
|
| 9 |
def test_pretty_text_to_numeric(self):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
)
|
| 15 |
|
| 16 |
def test_pretty_long_words_to_numeric(self):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
'should convert full-words number in thai to numeric in long words (case1)'
|
| 25 |
-
)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
| 44 |
|
| 45 |
def test_pretty_word11_to_numeric(self):
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
)
|
| 56 |
|
| 57 |
def test_pretty_word2x_to_numeric(self):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
)
|
| 69 |
|
| 70 |
def tearDown(self) -> None:
|
| 71 |
self.thw = None
|
|
|
|
| 1 |
import unittest
|
| 2 |
from utils.thai_word import ThaiWord
|
| 3 |
+
from collections import deque
|
| 4 |
|
| 5 |
class TestThaiWord(unittest.TestCase):
|
| 6 |
|
|
|
|
| 8 |
self.thw = ThaiWord()
|
| 9 |
|
| 10 |
def test_pretty_text_to_numeric(self):
|
| 11 |
+
tokens = deque(['ฮา','โหล','หนึ่ง','สอง','สาม','สี่'])
|
| 12 |
+
self.assertEqual(self.thw.pretty(tokens),
|
| 13 |
+
'ฮาโหล 1234',
|
| 14 |
+
'should convert single word number in thai to numeric')
|
|
|
|
| 15 |
|
| 16 |
def test_pretty_long_words_to_numeric(self):
|
| 17 |
+
tokens1 = deque([
|
| 18 |
+
'ปี','นี้','สอง','พัน','ห้า','ร้อย','หก','สิบ','เจ็ด','นะ', ' ',
|
| 19 |
+
'ปี','หน้า','ก็','สอง','พัน','ห้า','ร้อย','หก','สิบ','แปด'
|
| 20 |
+
])
|
| 21 |
+
self.assertEqual(self.thw.pretty(tokens1),
|
| 22 |
+
'ปีนี้ 2,567 นะ ปีหน้าก็ 2,568 ',
|
| 23 |
+
'should convert full-words number in thai to numeric in long words (case1)')
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
tokens2 = deque([
|
| 26 |
+
'อืม', ' ', 'อยาก', 'ได้', 'ราย', 'ได้', 'ยี่', 'สิบ',
|
| 27 |
+
'เอ็ดล้าน', 'แบบ', 'เข้า', 'บ้าง', ' ', 'ทำ', 'ยัง', 'ไง', 'ดี'
|
| 28 |
+
])
|
| 29 |
+
self.assertEqual(self.thw.pretty(tokens2),
|
| 30 |
+
'อืม อยากได้รายได้ 21,000,000 แบบเข้าบ้าง ทำยังไงดี',
|
| 31 |
+
'should convert full-words number in thai to numeric in long words (case2)')
|
|
|
|
| 32 |
|
| 33 |
+
tokens3 = deque([
|
| 34 |
+
'อืม',' ','อยาก','ได้','ราย','ได้','ยี่สิบ','เอ็ด','ล้าน',
|
| 35 |
+
'แบบ', 'ร้าน','พร้อม','ทำ','ยัง','ไง','ดี'
|
| 36 |
+
])
|
| 37 |
+
self.assertEqual(self.thw.pretty(tokens3),
|
| 38 |
+
'อืม อยากได้รายได้ 21,000,000 แบบร้านพร้อมทำยังไงดี',
|
| 39 |
+
'should convert full-words number in thai to numeric in long words (case3)')
|
|
|
|
| 40 |
|
| 41 |
def test_pretty_word11_to_numeric(self):
|
| 42 |
+
tokens1 = deque(['ซื้อ','มา','สิบ','เอ็ด','บาท'])
|
| 43 |
+
self.assertEqual(self.thw.pretty(tokens1),
|
| 44 |
+
'ซื้อมา 11 บาท',
|
| 45 |
+
'should correct specific numeric "สิบ" and "เอ็ด"')
|
| 46 |
+
|
| 47 |
+
tokens2 = deque(['ซื้อ','มา','สิบเอ็ด','บาท'])
|
| 48 |
+
self.assertEqual(self.thw.pretty(tokens2),
|
| 49 |
+
'ซื้อมา 11 บาท',
|
| 50 |
+
'should correct specific numeric "สิบเอ็ด"')
|
|
|
|
| 51 |
|
| 52 |
def test_pretty_word2x_to_numeric(self):
|
| 53 |
+
tokens1 = deque(['ซื้อ','มา','ยี่','สิบ','ห้า','บาท'])
|
| 54 |
+
self.assertEqual(self.thw.pretty(tokens1),
|
| 55 |
+
'ซื้อมา 25 บาท',
|
| 56 |
+
'should correct specific numeric "ยี่" and "สิบ"')
|
|
|
|
| 57 |
|
| 58 |
+
token2 = deque(['ซื้อ','มา','ยี่สิบ','ห้า','บาท'])
|
| 59 |
+
self.assertEqual(self.thw.pretty(token2),
|
| 60 |
+
'ซื้อมา 25 บาท',
|
| 61 |
+
'should correct specific numeric "ยี่สิบ"')
|
|
|
|
| 62 |
|
| 63 |
def tearDown(self) -> None:
|
| 64 |
self.thw = None
|
utils/thai_word.py
CHANGED
|
@@ -34,16 +34,18 @@ class ThaiWord:
|
|
| 34 |
except Exception:
|
| 35 |
for word in words:
|
| 36 |
num = f'{num}{text_to_arabic_digit(word)}'
|
|
|
|
|
|
|
| 37 |
|
| 38 |
return num
|
| 39 |
|
| 40 |
-
def pretty(self,
|
| 41 |
has_start_number = False
|
| 42 |
number = []
|
| 43 |
text = ''
|
| 44 |
|
| 45 |
-
while len(
|
| 46 |
-
word =
|
| 47 |
if has_start_number:
|
| 48 |
if self.is_number(word) or self.is_digit(word):
|
| 49 |
number.append(word)
|
|
@@ -60,7 +62,7 @@ class ThaiWord:
|
|
| 60 |
else:
|
| 61 |
text = f'{text}{word}'
|
| 62 |
|
| 63 |
-
if len(
|
| 64 |
text = f'{text}{self.words_to_number(number)}'
|
| 65 |
|
| 66 |
return text
|
|
|
|
| 34 |
except Exception:
|
| 35 |
for word in words:
|
| 36 |
num = f'{num}{text_to_arabic_digit(word)}'
|
| 37 |
+
|
| 38 |
+
num = f' {num}'
|
| 39 |
|
| 40 |
return num
|
| 41 |
|
| 42 |
+
def pretty(self, tokens: deque) -> str:
|
| 43 |
has_start_number = False
|
| 44 |
number = []
|
| 45 |
text = ''
|
| 46 |
|
| 47 |
+
while len(tokens) > 0:
|
| 48 |
+
word = tokens.popleft()
|
| 49 |
if has_start_number:
|
| 50 |
if self.is_number(word) or self.is_digit(word):
|
| 51 |
number.append(word)
|
|
|
|
| 62 |
else:
|
| 63 |
text = f'{text}{word}'
|
| 64 |
|
| 65 |
+
if len(tokens) == 0 and len(number) > 0:
|
| 66 |
text = f'{text}{self.words_to_number(number)}'
|
| 67 |
|
| 68 |
return text
|