Skip to content

Commit eee54c1

Browse files
committed
Sorting tokenizers
- more details on word tokenizers - add alias "longest" for longest-matching tokenizer - remove "dict" tokenizer from document, as there is no implementation in the code - remove "mm" tokenizer from document, as it is not recommended to use (maintenance mode), but keep the code, so people can call it - update doc: pythainlp-dev-thai.md - remove unused import sys
1 parent af1f081 commit eee54c1

File tree

15 files changed

+65
-70
lines changed

15 files changed

+65
-70
lines changed

docs/pythainlp-dev-thai.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,16 @@ word_tokenize(text, engine)
5050
```
5151
text คือ ข้อความในรูปแบบสตริง str เท่านั้น
5252

53-
engine คือ ระบบตัดคำ ปัจจุบัน PyThaiNLP มี 6 engine ดังนี้
53+
engine คือ ระบบตัดคำ ปัจจุบันมี engine ดังนี้
5454

55-
1. newmm (ค่าเริ่มต้น) - ใช้วิธี Maximum Matching โค้ดชุดใหม่[โดยคุณ Korakot Chaovavanich](https://www.facebook.com/groups/408004796247683/permalink/431283740586455/)
56-
2. icu - เรียกใช้ตัวตัดคำจาก ICU ตัวดั้งเดิมของ PyThaiNLP (ความแม่นยำต่ำ)
57-
3. dict - ตัดคำโดยใช้พจานุกรมจาก thaiword.txt ใน corpus (ความแม่นยำปานกลาง) **จะคืนค่า False หากข้อความนั้นไม่สามารถตัดคำได้**
58-
4. longest-matching - ใช้วิธี Longest Matching
59-
5. mm - ใช้วิธี Maximum Matching **(โค้ดชุดเก่า อยู่ในสถานะบำรุงรักษาเท่านั้น)**
60-
6. pylexto - เรียกใช้ตัวตัดคำจาก LexTo ซึ่งเป็น Longest Matching
61-
7. deepcut - เรียกใช้ [deepcut](https://github.com/rkcosmos/deepcut) ซึ่งตัดคำจากโมเดลการเรียนรู้ของเครื่อง
62-
8. wordcutpy - เรียกใช้ตัวตัดคำจาก [wordcutpy](https://github.com/veer66/wordcutpy)
55+
- newmm (ค่าเริ่มต้น) - ใช้พจนานุกรม ด้วยวิธี Maximum Matching โค้ดชุดใหม่[โดยคุณ Korakot Chaovavanich](https://www.facebook.com/groups/408004796247683/permalink/431283740586455/)
56+
- longest - ใช้พจนานุกรม ด้วยวิธี Longest Matching
57+
- icu - เรียกใช้ตัวตัดคำจาก ICU ใช้พจนานุกรม (ความแม่นยำต่ำ)
58+
- pylexto - เรียกใช้ตัวตัดคำจาก LexTo ใช้พจนานุกรม ด้วยวิธี Longest Matching
59+
- wordcutpy - เรียกใช้ตัวตัดคำจาก [wordcutpy](https://github.com/veer66/wordcutpy) ใช้พจนานุกรม
60+
- deepcut - เรียกใช้ตัวตัดคำจาก [deepcut](https://github.com/rkcosmos/deepcut) ใช้การเรียนรู้ของเครื่อง
6361

64-
คืนค่าเป็น ''list'' เช่น ['แมว','กิน']
62+
คืนค่าเป็น ''list'' เช่น ['แมว', 'กิน']
6563

6664
**การใช้งาน**
6765

@@ -86,11 +84,11 @@ text คือ ข้อความที่ต้องการตัดค
8684

8785
filename คือ ที่ตั้งไฟล์ที่ต้องการมาเป็นฐานข้อมูลตัดคำ
8886

89-
engine คือ เครื่องมือตัดคำ
90-
- newmm ตัดคำด้วย newmm
91-
- wordcutpy ใช้ [wordcutpy](https://github.com/veer66/wordcutpy) ในการตัดคำ
92-
- mm ตัดคำด้วย mm
93-
- longest-matching ตัดคำโดยใช้ longest matching
87+
engine คือ ระบบตัดคำ (ดูรายละเอียดที่ word_tokenize)
88+
- newmm
89+
- mm
90+
- longest
91+
- wordcutpy
9492

9593
ตัวอย่างการใช้งาน https://gist.github.com/wannaphongcom/1e862583051bf0464b6ef4ed592f739c
9694

examples/tokenize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
print(word_tokenize(text2))
2121
# ['กฎหมายแรงงาน']
2222

23-
print(word_tokenize(text2, engine="longest-matching"))
23+
print(word_tokenize(text2, engine="longest"))
2424
# ['กฎหมาย', 'แรงงาน']

pythainlp/romanization/pyicu.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
import sys
4-
53
try:
64
import icu
75
except ImportError:

pythainlp/sentiment/ulmfit_sent.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Sentiment analyzer based on thai2vec ("ulmfit" engine)
44
Code by https://github.com/cstorm125/thai2vec/tree/master/notebook
55
"""
6-
import sys
76
from collections import defaultdict
87

98
from pythainlp.corpus import download, get_file

pythainlp/tag/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
"""
33
Part-Of-Speech tagger
44
"""
5-
import sys
65

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

98

109
def pos_tag(words, engine="unigram", corpus="orchid"):
@@ -31,11 +30,11 @@ def _tag(text, corpus=None):
3130
except ImportError:
3231
from pythainlp.tools import install_package
3332

34-
install_package(ARTAGGER_URL)
33+
install_package(_ARTAGGER_URL)
3534
try:
3635
from artagger import Tagger
3736
except ImportError:
38-
raise ImportError("Error: Try 'pip install " + ARTAGGER_URL + "'")
37+
raise ImportError("Error: Try 'pip install " + _ARTAGGER_URL + "'")
3938

4039
words = Tagger().tag(" ".join(text))
4140

pythainlp/tag/perceptron.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def pud_data():
2424
return model
2525

2626

27-
def tag(text, corpus):
27+
def tag(text, corpus="pud"):
2828
"""
2929
รับค่าเป็น ''list'' คืนค่าเป็น ''list'' เช่น [('ข้อความ', 'ชนิดคำ')]"""
3030
if corpus == "orchid":

pythainlp/tokenize/__init__.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,48 @@
1616

1717
def word_tokenize(text, engine="newmm", whitespaces=True):
1818
"""
19-
:param str text: the text to be tokenized
20-
:param str engine: the engine to tokenize text
21-
:param bool whitespaces: True to output no whitespace, a common mark of sentence or end of phrase in Thai.
19+
:param str text: text to be tokenized
20+
:param str engine: tokenizer to be used
21+
:param bool whitespaces: True to output no whitespace, a common mark of sentence or end of phrase in Thai
2222
:Parameters for engine:
23-
* newmm - Maximum Matching algorithm + TCC
24-
* icu - IBM ICU
25-
* longest-matching - Longest matching
26-
* mm - Maximum Matching algorithm
27-
* pylexto - LexTo
28-
* deepcut - Deep Neural Network
29-
* wordcutpy - wordcutpy (https://github.com/veer66/wordcutpy)
30-
:return: A list of words, tokenized from a text
23+
* newmm (default) - dictionary-based, Maximum Matching + TCC
24+
* mm - dictionary-based, Maximum Matching
25+
* longest - dictionary-based, Longest Matching
26+
* icu - wrapper for ICU, dictionary-based
27+
* pylexto - wrapper for PyLexTo, dictionary-based, Longest Matching
28+
* wordcutpy - wrapper for wordcutpy, dictionary-based https://github.com/veer66/wordcutpy
29+
* deepcut - wrapper for deepcut, language-model-based https://github.com/rkcosmos/deepcut
30+
:return: list of words, tokenized from the text
3131
3232
**Example**::
33-
from pythainlp.tokenize import word_tokenize
34-
text = "โอเคบ่พวกเรารักภาษาบ้านเกิด"
35-
word_tokenize(text, engine="newmm") # ['โอเค', 'บ่', 'พวกเรา', 'รัก', 'ภาษา', 'บ้านเกิด']
36-
word_tokenize(text, engine="icu") # ['โอ', 'เค', 'บ่', 'พวก', 'เรา', 'รัก', 'ภาษา', 'บ้าน', 'เกิด']
33+
>>> from pythainlp.tokenize import word_tokenize
34+
>>> text = "โอเคบ่พวกเรารักภาษาบ้านเกิด"
35+
>>> word_tokenize(text, engine="newmm")
36+
['โอเค', 'บ่', 'พวกเรา', 'รัก', 'ภาษา', 'บ้านเกิด']
37+
>>> word_tokenize(text, engine="icu")
38+
['โอ', 'เค', 'บ่', 'พวก', 'เรา', 'รัก', 'ภาษา', 'บ้าน', 'เกิด']
3739
"""
38-
if engine == "icu":
40+
if engine == "newmm" or engine == "onecut":
3941
from .pyicu import segment
40-
elif engine == "multi_cut" or engine == "mm":
41-
from .multi_cut import segment
42+
elif engine == "longest" or engine == "longest-matching":
43+
from .longest import segment
4244
elif engine == "ulmfit":
4345
from .newmm import mmcut
46+
4447
def segment(text):
4548
return mmcut(text, trie=FROZEN_DICT_TRIE)
46-
elif engine == "longest-matching":
47-
from .longest import segment
48-
elif engine == "pylexto":
49-
from .pylexto import segment
49+
50+
elif engine == "icu":
51+
from .pyicu import segment
5052
elif engine == "deepcut":
5153
from .deepcut import segment
54+
elif engine == "pylexto":
55+
from .pylexto import segment
5256
elif engine == "wordcutpy":
5357
from .wordcutpy import segment
54-
else: # default, use "newmm" ("onecut") engine
58+
elif engine == "mm" or engine == "multi_cut":
59+
from .multi_cut import segment
60+
else: # default, use "newmm" engine
5561
from .newmm import mmcut as segment
5662

5763
if not whitespaces:
@@ -66,24 +72,26 @@ def dict_word_tokenize(text, custom_dict_trie, engine="newmm"):
6672
6773
:param str text: the text to be tokenized
6874
:param dict custom_dict_trie: คือ trie ที่สร้างจาก create_custom_dict_trie
69-
:param str engine: choose between different options of engine to token (newmm, wordcutpy, mm, longest-matching)
75+
:param str engine: choose between different options of engine to token (newmm, wordcutpy, mm, longest)
7076
:return: A list of words, tokenized from a text.
7177
**Example**::
7278
>>> from pythainlp.tokenize import dict_word_tokenize,create_custom_dict_trie
73-
>>> listword=['แมว',"ดี"]
74-
>>> data_dict=create_custom_dict_trie(listword)
75-
>>> dict_word_tokenize("แมวดีดีแมว",data_dict)
79+
>>> listword = ["แมว", "ดี"]
80+
>>> data_dict = create_custom_dict_trie(listword)
81+
>>> dict_word_tokenize("แมวดีดีแมว", data_dict)
7682
['แมว', 'ดี', 'ดี', 'แมว']
7783
"""
78-
if engine == "mm" or engine == "multi_cut":
79-
from .multi_cut import segment
80-
elif engine == "longest-matching":
84+
if engine == "newmm" or engine == "onecut":
85+
from .pyicu import segment
86+
elif engine == "longest" or engine == "longest-matching":
8187
from .longest import segment
8288
elif engine == "wordcutpy":
8389
from .wordcutpy import segment
8490

8591
return segment(text, custom_dict_trie.keys())
86-
else: # default, use "newmm" ("onecut") engine
92+
elif engine == "mm" or engine == "multi_cut":
93+
from .multi_cut import segment
94+
else: # default, use "newmm" engine
8795
from .newmm import mmcut as segment
8896

8997
return segment(text, custom_dict_trie)

pythainlp/tokenize/deepcut.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"""
33
Wrapper for deepcut Thai word segmentation
44
"""
5-
import sys
65

76
try:
87
import deepcut

pythainlp/tokenize/longest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"""
33
Longest-matching Thai word segmentation
44
5-
Based on code from https://github.com/patorn/thaitokenizer/blob/master/thaitokenizer/tokenizer.py
5+
Based on code from
6+
https://github.com/patorn/thaitokenizer/blob/master/thaitokenizer/tokenizer.py
67
"""
78
import re
89

pythainlp/tokenize/pyicu.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Wrapper for ICU word segmentation
44
"""
55
import re
6-
import sys
76

87
try:
98
import icu

0 commit comments

Comments
 (0)