|
| 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 |
0 commit comments