Skip to content

Commit 8ab13d2

Browse files
authored
Merge pull request #157 from bact/dev
artagger installation workaround on Windows test (AppVeyor) + add unit test
2 parents fed1ceb + 917e357 commit 8ab13d2

File tree

10 files changed

+68
-44
lines changed

10 files changed

+68
-44
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ before_install:
1313
# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
1414
install:
1515
- pip install -r requirements.txt
16-
- pip install .[icu,ipa,ner,thai2vec]
16+
- pip install .[artagger,icu,ipa,ner,thai2vec]
1717
- pip install coveralls
1818

1919
os:

README.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,21 @@ $ pip install pythainlp[extra1,extra2,...]
5151
```
5252

5353
where ```extras``` can be
54-
- ```artagger``` (to support artagger part-of-speech tagger)
54+
- ```artagger``` (to support artagger part-of-speech tagger)*
5555
- ```deepcut``` (to support deepcut machine-learnt tokenizer)
5656
- ```icu``` (for ICU support in transliteration and tokenization)
5757
- ```ipa``` (for International Phonetic Alphabet support in transliteration)
58-
- ```ml``` (to support ULMFit models, like one for sentiment analyser)
58+
- ```ml``` (to support ULMFiT models, like one for sentiment analyser)
5959
- ```ner``` (for named-entity recognizer)
6060
- ```thai2rom``` (for machine-learnt romanization)
6161
- ```thai2vec``` (for Thai word vector)
6262
- ```full``` (install everything)
6363

64-
see ```extras``` and ```extras_require``` in [```setup.py```](https://github.com/PyThaiNLP/pythainlp/blob/dev/setup.py) for details.
64+
* Note: standard ```artagger``` package from PyPI will not work on Windows, please ```pip install https://github.com/wannaphongcom/artagger/tarball/master#egg=artagger``` instead.
6565

66-
Development release:
66+
** see ```extras``` and ```extras_require``` in [```setup.py```](https://github.com/PyThaiNLP/pythainlp/blob/dev/setup.py) for package details.
67+
68+
### Development release:
6769

6870
```sh
6971
$ pip install https://github.com/PyThaiNLP/pythainlp/archive/dev.zip
@@ -94,7 +96,7 @@ PyThaiNLP เป็นไลบารีภาษาไพทอนเพื่
9496

9597
> เพราะโลกขับเคลื่อนต่อไปด้วยการแบ่งปัน
9698
97-
รองรับ Python 3.4 ขึ้นไป
99+
รองรับ Python 3.6 ขึ้นไป
98100

99101
ตั้งแต่รุ่น 1.7 PyThaiNLP จะเลิกสนับสนุน Python 2 (บางฟังก์ชันอาจยังทำงานได้ แต่จะไม่ได้รับการสนับสนุน) และตั้งแต่รุ่น 1.8 จะยุติการรองรับ Python 2 ทั้งหมด
100102
ผู้ใช้ Python 2 ยังสามารถใช้ PyThaiNLP 1.6 ได้
@@ -117,19 +119,39 @@ PyThaiNLP เป็นไลบารีภาษาไพทอนเพื่
117119

118120
## ติดตั้ง
119121

120-
รุ่นเสถียร
122+
### รุ่นเสถียร
121123

122124
```sh
123125
$ pip install pythainlp
124126
```
125127

126-
รุ่นกำลังพัฒนา
128+
สำหรับความสามารถเพิ่มเติมบางอย่าง เช่น word vector จำเป็นต้องติดตั้งแพคเกจสนับสนุนเพิ่มเติม ติดตั้งแพคเพจเหล่านั้นได้ ด้วยการระบุออปชันเหล่านี้ตอน pip install:
127129

128130
```sh
129-
$ pip install https://github.com/PyThaiNLP/pythainlp/archive/dev.zip
131+
$ pip install pythainlp[extra1,extra2,...]
130132
```
131133

132-
หมายเหตุ: เนื่องจาก ulmfit sentiment analyser ต้องใช้ PyTorch จึงต้อง ```pip install torch``` เพื่อติดตั้ง PyTorch ก่อน มอดูลที่อาศัยการเรียนรู้ของเครื่องอื่นๆ อาจจำเป็นต้องติดตั้ง gensim และ keras ก่อนเช่นกัน
134+
โดยที่ ```extras``` คือ
135+
- ```artagger``` (สำหรับตัวติดป้ายกำกับชนิดคำ artagger)*
136+
- ```deepcut``` (สำหรับตัวตัดคำ deepcut)
137+
- ```icu``` (สำหรับการถอดตัวสะกดเป็นสัทอักษรและการตัดคำด้วย ICU)
138+
- ```ipa``` (สำหรับการถอดตัวสะกดเป็นสัทอักษรสากล (IPA))
139+
- ```ml``` (สำหรับการรองรับโมเดล ULMFiT ซึ่งใช้ในฟังก์ชันเช่นการวิเคราะห์อารมณ์)
140+
- ```ner``` (สำหรับการติดป้ายชื่อเฉพาะ (named-entity))
141+
- ```thai2rom``` (สำหรับการถอดตัวสะกดเป็นอักษรละติน)
142+
- ```thai2vec``` (สำหรับ word vector)
143+
- ```full``` (ติดตั้งทุกอย่าง)
144+
145+
* หมายเหตุ: แพคเกจ ```artagger``` มาตรฐานจาก PyPI อาจมีปัญหาการถอดรหัสข้อความบน Windows กรุณาติดตั้งรุ artagger รุ่นแก้ไขด้วยคำสั่ง ```pip install https://github.com/wannaphongcom/artagger/tarball/master#egg=artagger``` แทน ก่อนจะติดตั้ง PyThaiNLP
146+
147+
** นักพัฒนาสามารถดู ```extras``` และ ```extras_require``` ใน [```setup.py```](https://github.com/PyThaiNLP/pythainlp/blob/dev/setup.py) สำหรับรายละเอียดแพคเกจของเสริม
148+
149+
150+
### รุ่นกำลังพัฒนา
151+
152+
```sh
153+
$ pip install https://github.com/PyThaiNLP/pythainlp/archive/dev.zip
154+
```
133155

134156
## เอกสารการใช้งาน
135157

appveyor.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@ environment:
55
- PYTHON: "C:/Python36"
66
PYTHON_VERSION: "3.6"
77
PYTHON_ARCH: "32"
8-
PYICU_WHEEL: "https://get.openlp.org/win-sdk/PyICU-1.9.7-cp36-cp36m-win32.whl"
8+
PYICU_PKG: "https://get.openlp.org/win-sdk/PyICU-1.9.7-cp36-cp36m-win32.whl"
9+
ARTAGGER_PKG: "https://github.com/wannaphongcom/artagger/tarball/master#egg=artagger"
910

1011
#- PYTHON: "C:/Python36-x64"
1112
# PYTHON_VERSION: "3.6"
1213
# PYTHON_ARCH: "64"
13-
# PYICU_WHEEL: ""
14+
# PYICU_PKG: ""
1415

1516
#- PYTHON: "C:/Python37"
1617
# PYTHON_VERSION: "3.7"
1718
# PYTHON_ARCH: "32"
18-
# PYICU_WHEEL: ""
19+
# PYICU_PKG: ""
1920

2021
#- PYTHON: "C:/Python37-x64"
2122
# PYTHON_VERSION: "3.7"
2223
# PYTHON_ARCH: "64"
23-
# PYICU_WHEEL: ""
24+
# PYICU_PKG: ""
2425

2526
init:
2627
- "ECHO %PYTHON% %PYTHON_VERSION% %PYTHON_ARCH%"
@@ -31,8 +32,9 @@ install:
3132
- "set PYTHONIOENCODING=utf-8"
3233
# - "set ICU_VERSION=62"
3334
- "%PYTHON%/python.exe -m pip install --upgrade pip"
34-
- "%PYTHON%/python.exe -m pip install %PYICU_WHEEL%"
35-
- "%PYTHON%/python.exe -m pip install -e .[icu,ipa,ner,thai2vec]"
35+
- "%PYTHON%/python.exe -m pip install %PYICU_PKG%"
36+
- "%PYTHON%/python.exe -m pip install %ARTAGGER_PKG%"
37+
- "%PYTHON%/python.exe -m pip install -e .[artagger,icu,ipa,ner,thai2vec]"
3638

3739
test_script:
3840
- "%PYTHON%/python.exe -m pip --version"

pythainlp/tag/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
Part-Of-Speech tagger
44
"""
55

6-
_ARTAGGER_URL = "https://github.com/wannaphongcom/artagger/archive/master.zip"
7-
86

97
def pos_tag(words, engine="unigram", corpus="orchid"):
108
"""
119
Part of Speech tagging function.
1210
13-
:param list words: takes in a list of tokenized words (put differently, a list of strings)
11+
:param list words: a list of tokenized words
1412
:param str engine:
1513
* unigram - unigram tagger (default)
1614
* perceptron - perceptron tagger

pythainlp/tag/perceptron.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def tag(words, corpus="pud"):
2929
if not words:
3030
return []
3131

32+
# perceptron tagger cannot handle empty string
3233
words = [word.strip() for word in words if word.strip()]
3334

3435
if corpus == "orchid":

pythainlp/transliterate/royin.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,16 @@
110110
}
111111

112112
_RE_CONSONANT = re.compile(r"[ก-ฮ]")
113-
_RE_KARAN = re.compile(r"จน์|มณ์|ณฑ์|ทร์|ตร์|[ก-ฮ]์|[ก-ฮ][ะ-ู]์")
114-
_RE_KARAN2 = re.compile(r"\w" + r"์")
115-
_RE_YAMOK_PAIYANNOI = re.compile(r"[ๆฯ]")
116-
_RE_TONE = re.compile(r"[่-๋]")
113+
_RE_NORMALIZE = re.compile(
114+
r"จน์|มณ์|ณฑ์|ทร์|ตร์|[ก-ฮ]์|[ก-ฮ][ะ-ู]์"
115+
# yamok, paiyannoi, thanthakhat, yamakkan, tonemarks, other signs
116+
+ r"|[\u0e2f\u0e46\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f\u0e5a\u0e5b]"
117+
)
117118

118119

119120
def _normalize(text):
120121
"""ตัดอักษรที่ไม่ออกเสียง (การันต์ ไปยาลน้อย ไม้ยมก*) และวรรณยุกต์ทิ้ง"""
121-
text = _RE_KARAN.sub("", text)
122-
text = _RE_YAMOK_PAIYANNOI.sub("", text)
123-
text = _RE_TONE.sub("", text)
124-
if re.search(_RE_KARAN2, text):
125-
karans = re.findall(_RE_KARAN2, text)
126-
for karan in karans:
127-
text = re.sub(karan, "", text)
128-
return text
122+
return _RE_NORMALIZE.sub("", text)
129123

130124

131125
def _replace_vowels(word):
@@ -173,11 +167,14 @@ def romanize(word):
173167
return ""
174168

175169
word2 = _replace_vowels(_normalize(word))
176-
res = re.findall(_RE_CONSONANT, word2)
170+
res = _RE_CONSONANT.findall(word2)
171+
177172
# 2-character word, all consonants
178173
if len(word2) == 2 and len(res) == 2:
179174
word2 = list(word2)
180175
word2.insert(1, "o")
181176
word2 = "".join(word2)
177+
182178
word2 = _replace_consonants(word2, res)
179+
183180
return word2

pythainlp/ulmfit/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ def merge_wgts(em_sz, wgts, itos_pre, itos_cls):
197197
# feature extractor
198198
def document_vector(ss, m, stoi, tok_engine="newmm"):
199199
"""
200-
:meth: `document_vector` get document vector using pretrained ULMFit model
200+
:meth: `document_vector` get document vector using pretrained ULMFiT model
201201
:param str ss: sentence to extract embeddings
202202
:param m: pyTorch model
203203
:param dict stoi: string-to-integer dict e.g. {'_unk_':0, '_pad_':1,'first_word':2,'second_word':3,...}
204-
:param str tok_engine: tokenization engine (recommend using `newmm` if you are using pretrained ULMFit model)
204+
:param str tok_engine: tokenization engine (recommend using `newmm` if you are using pretrained ULMFiT model)
205205
:return: `numpy.array` of document vector sized 300
206206
"""
207207
s = word_tokenize(ss)

pythainlp/word_vector/thai2vec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def about():
6969
return """
7070
thai2vec
7171
State-of-the-Art Language Modeling, Text Feature Extraction and Text Classification in Thai Language.
72-
Created as part of pyThaiNLP with ULMFit implementation from fast.ai
72+
Created as part of PyThaiNLP with ULMFiT implementation from fast.ai
7373
7474
Development: Charin Polpanumas
7575
GitHub: https://github.com/cstorm125/thai2vec

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ tag = True
77
description-file = README.md
88

99
[bumpversion:file:setup.py]
10-
search = version='{current_version}'
11-
replace = version='{new_version}'
10+
search = version = '{current_version}'
11+
replace = version = '{new_version}'
1212

1313
[bumpversion:file:pythainlp/__init__.py]
1414
search = __version__ = '{current_version}'

tests/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,14 @@ def test_pos_tag(self):
314314
self.assertEqual(pos_tag(None), [])
315315
self.assertEqual(pos_tag([]), [])
316316

317-
self.assertIsNotNone(pos_tag(tokens, engine="unigram", corpus="orchid"))
318-
self.assertIsNotNone(pos_tag(tokens, engine="unigram", corpus="pud"))
319-
self.assertIsNotNone(pos_tag([""], engine="unigram", corpus="pud"))
320-
321317
self.assertEqual(unigram.tag(None, corpus="pud"), [])
322318
self.assertEqual(unigram.tag([], corpus="pud"), [])
323319
self.assertEqual(unigram.tag(None, corpus="orchid"), [])
324320
self.assertEqual(unigram.tag([], corpus="orchid"), [])
325321

322+
self.assertIsNotNone(pos_tag(tokens, engine="unigram", corpus="orchid"))
323+
self.assertIsNotNone(pos_tag(tokens, engine="unigram", corpus="pud"))
324+
self.assertIsNotNone(pos_tag([""], engine="unigram", corpus="pud"))
326325
self.assertEqual(
327326
pos_tag(word_tokenize("คุณกำลังประชุม"), engine="unigram"),
328327
[("คุณ", "PPRS"), ("กำลัง", "XVBM"), ("ประชุม", "VACT")],
@@ -335,8 +334,13 @@ def test_pos_tag(self):
335334
self.assertEqual(perceptron.tag(None, corpus="orchid"), [])
336335
self.assertEqual(perceptron.tag([], corpus="orchid"), [])
337336

338-
# self.assertIsNotNone(pos_tag(tokens, engine="artagger", corpus="orchid"))
339-
# self.assertIsNotNone(pos_tag(tokens, engine="artagger", corpus="pud"))
337+
self.assertIsNotNone(pos_tag(None, engine="artagger"))
338+
self.assertIsNotNone(pos_tag([], engine="artagger"))
339+
self.assertIsNotNone(pos_tag(tokens, engine="artagger"))
340+
self.assertEqual(
341+
pos_tag(word_tokenize("คุณกำลังประชุม"), engine="artagger"),
342+
[("คุณ", "PPRS"), ("กำลัง", "XVBM"), ("ประชุม", "VACT")],
343+
)
340344

341345
self.assertEqual(pos_tag_sents(None), [])
342346
self.assertEqual(pos_tag_sents([]), [])
@@ -502,7 +506,7 @@ def test_romanize(self):
502506
self.assertIsNotNone(romanize("ทีปกร", engine="royin"))
503507
self.assertIsNotNone(romanize("กรม", engine="royin"))
504508
self.assertIsNotNone(romanize("ธรรพ์", engine="royin"))
505-
self.assertIsNotNone(romanize("กฏa์", engine="royin"))
509+
self.assertIsNotNone(romanize("กฏa์1์ ์", engine="royin"))
506510
# self.assertIsNotNone(romanize("บัว", engine="thai2rom"))
507511

508512
def test_transliterate(self):

0 commit comments

Comments
 (0)