Skip to content

Commit

Permalink
Add thai_text_braille
Browse files Browse the repository at this point in the history
  • Loading branch information
wannaphong committed Mar 11, 2023
1 parent ff14856 commit 293aa2f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ print(thai_word_braille("แมว"))

This function support Thai and number only.

**Thai text to braille**

```python
from thaibraille import thai_text_braille

print(thai_text_braille("แมวกิน ปลา"))

# output: ['⠣⠍⠺', '⠛⠃⠝', ' ', '⠯⠇⠡']
```

This function support Thai and number only.

## License


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="thaibraille",
version="0.1dev0",
version="0.1dev1",
description="Thai Braille for Natural Language Processing.",
long_description=readme,
long_description_content_type="text/markdown",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ def test_thai_word_braille(self):
self.assertEqual(thai_word_braille("ลิ้น"), '⠇⠃⠲⠝')
self.assertEqual(thai_word_braille("ว่าง"), '⠺⠔⠡⠻')
self.assertEqual(thai_word_braille("แก้ม"), '⠣⠛⠲⠍')

def test_thai_text_braille(self):
self.assertEqual(thai_text_braille("แมวกิน ปลา"), ['⠣⠍⠺', '⠛⠃⠝', ' ', '⠯⠇⠡'])
4 changes: 2 additions & 2 deletions thaibraille/text/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-
__all__ = ["thai_braille_mapping_dict", "Braille", "thai_word_braille"]
from thaibraille.text.core import thai_braille_mapping_dict, Braille, thai_word_braille
__all__ = ["thai_braille_mapping_dict", "Braille", "thai_word_braille", "thai_text_braille"]
from thaibraille.text.core import thai_braille_mapping_dict, Braille, thai_word_braille, thai_text_braille
19 changes: 16 additions & 3 deletions thaibraille/text/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"๋":['356'],
"์":['456'],
"ๆ":['3'],
" ":['-1']
}

dict_2 = {
Expand All @@ -93,7 +94,7 @@

_v1=["เ-tอ", "เ-ีtย", "เ-ืtอ", "-ัtว", "เ-tา", "เ-tาะ"]

char_trie = Trie(list(thai_braille_mapping_dict.keys())+_v1)
char_trie = Trie(list(thai_braille_mapping_dict.keys())+_v1+[" "])


_vowel_patterns =[i.replace("-","([ก-ฮ])").replace("t","([่้๊๋])")+",\\1"+i.replace("t","")+"\\2" for i in _v1]
Expand All @@ -106,15 +107,26 @@ def _replace_vowels(word: str) -> str:

return word

def thai_word_braille(word):
def thai_word_braille(word: str) -> str:
word = _replace_vowels(word)
_temp = []
for i in word_tokenize(word,custom_dict=char_trie, engine="mm"):
_temp.append(thai_braille_mapping_dict[i])
if i.isspace() and len(i)>1:
for k in list(i):
_temp.append(thai_braille_mapping_dict[k])
else:
_temp.append(thai_braille_mapping_dict[i])
_b = Braille(_temp)
return _b.tobraille()


def thai_text_braille(text: str) -> list:
_list_braille = []
for word in word_tokenize(text):
_list_braille.append(thai_word_braille(word))
return _list_braille


class Braille:
'''
List Braille to Braille
Expand All @@ -133,6 +145,7 @@ def __init__(self,data):
else:
self.data = sorted(list(data)) # แปลงเป็น list พร้อมเรียงจากน้อยไปมาก
self.db = {
"-1":" ",
'0':'⠀',
'1':'⠁',
'3':'⠂',
Expand Down

0 comments on commit 293aa2f

Please sign in to comment.