Skip to content

Commit b55caab

Browse files
committed
add docs
1 parent aa6db8d commit b55caab

File tree

5 files changed

+174
-9
lines changed

5 files changed

+174
-9
lines changed

docs/pythainlp-1-3-thai.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# คู่มือการใช้งาน PyThaiNLP 1.3
2+
3+
## API
4+
5+
### ตัดคำไทย
6+
7+
สำหรับการตัดคำไทยนั้น ใน PyThaiNLP 1.3 ได้ทำเปลี่ยน API ใหม่ ยกเลิก pythainlp.segment ให้ทำการเปลี่ยนไปใช้ API ชุดใหม่
8+
9+
```python
10+
from pythainlp.tokenize import word_tokenize
11+
word_tokenize(text,engine)
12+
```
13+
text คือ ข้อความในรูปแบบสตริง str เท่านั้น
14+
15+
engine คือ ระบบตัดคำไทย ปัจจุบันนี้ PyThaiNLP ได้พัฒนามี 3 engine ให้ใช้งานกันดังนี้
16+
17+
1. icu - engine ตัวดั้งเดิมของ PyThaiNLP (ความแม่นยำต่ำ) และเป็นค่าเริ่มต้น
18+
2. dict - เป็นการตัดคำโดยใช้พจานุกรมจาก thaiword.txt ใน corpus (ความแม่นยำปานกลาง)
19+
3. mm - ใช้ Maximum Matching algorithm ในการตัดคำภาษาไทย
20+
21+
คืนค่าเป็น ''list'' เช่น [('ข้อความ', 'ชนิดคำ')]
22+
23+
**ตัวอย่าง**
24+
25+
```python
26+
from pythainlp.tokenize import word_tokenize
27+
text='ผมรักคุณนะครับโอเคบ่พวกเราเป็นคนไทยรักภาษาไทยภาษาบ้านเกิด'
28+
a=word_tokenize(text,engine='icu') # ['ผม', 'รัก', 'คุณ', 'นะ', 'ครับ', 'โอ', 'เค', 'บ่', 'พวก', 'เรา', 'เป็น', 'คน', 'ไทย', 'รัก', 'ภาษา', 'ไทย', 'ภาษา', 'บ้าน', 'เกิด']
29+
b=word_tokenize(text,engine='dict') # ['ผม', 'รัก', 'คุณ', 'นะ', 'ครับ', 'โอเค', 'บ่', 'พวกเรา', 'เป็น', 'คนไทย', 'รัก', 'ภาษาไทย', 'ภาษา', 'บ้านเกิด']
30+
c=word_tokenize(text,engine='mm') # ['ผม', 'รัก', 'คุณ', 'นะ', 'ครับ', 'โอเค', 'บ่', 'พวกเรา', 'เป็น', 'คนไทย', 'รัก', 'ภาษาไทย', 'ภาษา', 'บ้านเกิด']
31+
```
32+
33+
### Postaggers ภาษาไทย
34+
35+
ตั้งแต่ PyThaiNLP 1.3 เป็นต้นไป ได้ทำการยกเลิก pythainlp.postaggers เดิม เปลี่ยนไปใช้ API ชุดใหม่ดังนี้
36+
37+
```python
38+
from pythainlp.tag import pos_tag
39+
pos_tag(list,engine='old')
40+
```
41+
42+
list คือ list ที่เก็บข้อความหลังผ่านการตัดคำแล้ว
43+
44+
engine คือ ชุดเครื่องมือในการ postaggers มี 2 ตัวดังนี้
45+
46+
1. old เป็น UnigramTagger (ค่าเริ่มต้น)
47+
2. artagger เป็น RDR POS Tagger ละเอียดยิ่งกว่าเดิม รองรับเฉพาะ Python 3 เท่านั้น
48+
49+
### แปลงข้อความเป็น Latin
50+
51+
```python
52+
from pythainlp.romanization import romanization
53+
romanization(str)
54+
```
55+
**ตัวอย่าง**
56+
57+
```python
58+
from pythainlp.romanization import romanization
59+
romanization("แมว") # 'mæw'
60+
```
61+
62+
### เช็คคำผิด *
63+
64+
*ความสามารถนี้รองรับเฉพาะ Python 3
65+
66+
ก่อนใช้งานความสามารถนี้ ให้ทำการติดตั้ง hunspell และ hunspell-th ก่อน
67+
68+
**วิธีติดตั้ง** สำหรับบน Debian , Ubuntu
69+
70+
```
71+
sudo apt-get install hunspell hunspell-th
72+
```
73+
74+
ให้ใช้ pythainlp.spell ตามตัวอย่างนี้
75+
76+
```python
77+
from pythainlp.spell import *
78+
a=spell("สี่เหลียม")
79+
print(a) # ['สี่เหลี่ยม', 'เสียเหลี่ยม', 'เหลี่ยม']
80+
```
81+
### pythainlp.number
82+
83+
```python
84+
from pythainlp.number import *
85+
```
86+
จัดการกับตัวเลข โดยมีดังนี้
87+
88+
- nttn(str) - เป็นการแปลงเลขไทยสู่เลข
89+
- nttt(str) - เลขไทยสู่ข้อความ
90+
- ntnt(str) - เลขสู่เลขไทย
91+
- ntt(str) - เลขสู่ข้อความ
92+
- ttn(str) - ข้อความสู่เลข
93+
- numtowords(float) - อ่านจำนวนตัวเลขภาษาไทย (บาท) รับค่าเป็น ''float'' คืนค่าเป็น 'str'
94+
95+
### เรียงลำดับข้อมูลภาษาไทยใน List
96+
97+
```python
98+
from pythainlp.collation import collation
99+
print(collation(['ไก่','ไข่','','ฮา'])) # ['ก', 'ไก่', 'ไข่', 'ฮา']
100+
```
101+
102+
รับ list คืนค่า list
103+
104+
### รับเวลาปัจจุบันเป็นภาษาไทย
105+
106+
```
107+
from pythainlp.date import now
108+
now() # '30 พฤษภาคม 2560 18:45:24'
109+
```
110+
### WordNet ภาษาไทย
111+
112+
เรียกใช้งาน
113+
114+
```
115+
from pythainlp.corpus import wordnet
116+
```
117+
118+
**รับ Synset**
119+
120+
```
121+
wordnet.getSynset(คำ)
122+
```
123+
124+
เป็นคำสั่ง ใช้รับ Synset รับค่า str ส่งออกเป็น tuple ('Synset', 'synset li')
125+
126+
**รับคำจาก id**
127+
128+
```
129+
wordnet.getWords()
130+
```
131+
132+
เป็นคำสั่ง ใช้รับคำจาก ID รับค่า str ส่งออกเป็น tuple ('Word', 'synsetid li')
133+
134+
### stopword ภาษาไทย
135+
136+
```python
137+
from pythainlp.corpus import stopwords
138+
stopwords = stopwords.words('thai')
139+
```
140+
141+
### หาคำที่มีจำนวนการใช้งานมากที่สุด
142+
143+
```
144+
pythainlp.rank.rank(list)
145+
```
146+
147+
คืนค่าออกมาเป็น dict
148+
149+
**ตัวอย่างการใช้งาน**
150+
151+
```
152+
>>> pythainlp.rank.rank(['แมง','แมง','คน'])
153+
Counter({'แมง': 2, 'คน': 1})
154+
```
155+
156+
### แก้ไขปัญหาการพิมพ์ลืมเปลี่ยนภาษา
157+
158+
```
159+
pythainlp.change.คำสั่ง()
160+
```
161+
162+
มีคำสั่งดังนี้
163+
164+
- texttothai(str) แปลงแป้นตัวอักษรภาษาอังกฤษเป็นภาษาไทย
165+
- texttoeng(str) แปลงแป้นตัวอักษรภาษาไทยเป็นภาษาอังกฤษ
166+
167+
คืนค่าออกมาเป็น str

example/segment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pythainlp.segment import segment
1+
from pythainlp.tokenize import word_tokenize
22
a = 'ฉันรักภาษาไทยเพราะฉันเป็นคนไทยและฉันใช้ภาษาไทย'
3-
b = segment(a)
3+
b = word_tokenize(a)
44
print(b)

pythainlp/postaggers/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import,division,unicode_literals
3-
import pythainlp
43
import codecs
54
import os
65
import json
76
import nltk.tag
87
import nltk.data
9-
templates_dir = os.path.join(os.path.dirname(pythainlp.__file__), 'corpus')
10-
template_file = os.path.join(templates_dir, 'thaipos.json')
118
def data():
9+
import pythainlp
10+
templates_dir = os.path.join(os.path.dirname(pythainlp.__file__), 'corpus')
11+
template_file = os.path.join(templates_dir, 'thaipos.json')
1212
with codecs.open(template_file,'r',encoding='utf-8-sig') as handle:
1313
model = json.load(handle)
1414
return model
1515
def tag(text):
1616
"""
17-
หมายเหตุ API ชุดนี้เตรียมหยุดการใช้งาน
1817
รับค่าเป็น ''list'' คืนค่าเป็น ''list'' เช่น [('ข้อความ', 'ชนิดคำ')]"""
1918
tagger = nltk.tag.UnigramTagger(model=data())# backoff=default_tagger)
2019
return tagger.tag(text)

pythainlp/test/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ def testTag(self):
3030
self.assertEqual(pos_tag(word_tokenize("คุณกำลังประชุม"),engine='old'),[('คุณ', 'PPRS'), ('กำลัง', 'XVBM'), ('ประชุม', 'VACT')])
3131
def testTagnew(self):
3232
if sys.version_info > (3,3):
33-
self.assertEqual(pos_tag(word_tokenize("คุณกำลังประชุม"),engine='artagger'),[('ผม', 'PPRS'), ('รัก', 'VSTA'), ('คุณ', 'PPRS')])
33+
self.assertEqual(pos_tag(word_tokenize("ผมรักคุณ"),engine='artagger'),[('ผม', 'PPRS'), ('รัก', 'VSTA'), ('คุณ', 'PPRS')])
3434
if __name__ == '__main__':
3535
unittest.main()

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,5 @@
3737
'License :: OSI Approved :: Apache Software License',
3838
'Natural Language :: Thai',
3939
'Topic :: Text Processing :: Linguistic',
40-
'Programming Language :: Python :: Implementation'
41-
],
40+
'Programming Language :: Python :: Implementation'],
4241
)

0 commit comments

Comments
 (0)