ทำ POS tagging ภาษาไทยโดยใช้ Neural Network ด้วย nlpnet

nlpnet เป็นเครื่องมือสำหรับใช้ทำ part-of-speech tagging, semantic role labeling และ dependency parsing โดยใช้ Neural Network โดยรันบน Python 2.7 (ไม่รองรับ Windows) และเป็น MIT License

การติดตั้ง
ใช้คำสั่ง $ pip install nlpnet

การเตรียมข้อมูลภาษาไทย
เราใช้ข้อมูล orchid มาทำเป็น CoNLL format ตั้งชื่อว่า thaipostag.txt ที่มี 4 column ตามตัวอย่างนี้

1    การ    _    FIXN
2    ประชุม    _    VACT
3    ทาง    _    NCMN
4    วิชาการ    _    NCMN
5    <space>    _    PUNC
6    ครั้ง    _    CFQC
7    ที่1    _    DONM 
column แรกเป็นลำดับประโยค column ที่ 2 เป็นคำ และอันสุดท้ายเป็น POS tagging กำกับแต่ละคำ

โหลดไฟล์ข้อมูลที่จัดทำไว้ได้จาก https://gist.github.com/wannaphongcom/a8224d8fb19eb32c3c86335e180bcc9e

จากนั้น ทำการโหลดไฟล์ nlpnet-train.py จาก https://github.com/erickrf/nlpnet/raw/master/bin/nlpnet-train.py มาไว้

แล้วเปิดคอมมาไลน์ ใช้คำสั่ง $ python nlpnet-train.py pos --gold ที่ตั้งไฟล์thaipostag.txt -e จำนวนที่ต้องการtrain(ค่าเริ่มต้นคือ15)

โดยเมื่อสั่ง
$ python nlpnet-train.py pos --gold /home/wannaphong/thainlp/nlpnet/thaipostag.txt
Reading training data...
Loading vocabulary
Created dictionary with 7394 types
Dictionary saved in ./vocabulary-pos.txt
Creating new network...
Generating word type features...
Created new network with the following layer sizes: 250, 100, 46

Training for up to 15 epochs
1 epochs   Error: 1.817773   Accuracy: 0.599158   0 corrections skipped   learning rate: 0.001000
2 epochs   Error: 1.415986   Accuracy: 0.662283   0 corrections skipped   learning rate: 0.001000
3 epochs   Error: 1.256999   Accuracy: 0.684470   0 corrections skipped   learning rate: 0.001000
4 epochs   Error: 1.167212   Accuracy: 0.701549   0 corrections skipped   learning rate: 0.001000
5 epochs   Error: 1.107231   Accuracy: 0.711078   0 corrections skipped   learning rate: 0.001000
6 epochs   Error: 1.062287   Accuracy: 0.719276   0 corrections skipped   learning rate: 0.001000
7 epochs   Error: 1.025902   Accuracy: 0.727907   0 corrections skipped   learning rate: 0.001000
8 epochs   Error: 0.995274   Accuracy: 0.734762   0 corrections skipped   learning rate: 0.001000
9 epochs   Error: 0.968602   Accuracy: 0.739940   0 corrections skipped   learning rate: 0.001000
10 epochs   Error: 0.945042   Accuracy: 0.748870   0 corrections skipped   learning rate: 0.001000
11 epochs   Error: 0.923928   Accuracy: 0.753975   0 corrections skipped   learning rate: 0.001000
12 epochs   Error: 0.904769   Accuracy: 0.758639   0 corrections skipped   learning rate: 0.001000
13 epochs   Error: 0.887420   Accuracy: 0.762363   0 corrections skipped   learning rate: 0.001000
14 epochs   Error: 0.871517   Accuracy: 0.768334   0 corrections skipped   learning rate: 0.001000
15 epochs   Error: 0.856865   Accuracy: 0.772651   0 corrections skipped   learning rate: 0.001000
Finished training
พบว่ายังมีข้อผิดพลาดอยู่มาก และไม่มีถูกเลย
จะได้ไฟล์ metadata-pos.pickle , pos-network.hdf5 , pos-tags.txt และ vocabulary-pos.txt นี่คือ ไฟล์ model ของ POS tagging ที่ผ่านการ Training

แต่เมื่อลอง train 30 epochs ดู
Reading training data...
Loading vocabulary
Created dictionary with 7394 types
Dictionary saved in ./vocabulary-pos.txt
Creating new network...
Generating word type features...
Created new network with the following layer sizes: 250, 100, 46

Training for up to 30 epochs
1 epochs   Error: 1.778611   Accuracy: 0.613756   0 corrections skipped   learning rate: 0.001000
2 epochs   Error: 1.381480   Accuracy: 0.674118   0 corrections skipped   learning rate: 0.001000
3 epochs   Error: 1.231589   Accuracy: 0.692636   0 corrections skipped   learning rate: 0.001000
4 epochs   Error: 1.146607   Accuracy: 0.705944   0 corrections skipped   learning rate: 0.001000
5 epochs   Error: 1.088973   Accuracy: 0.719203   0 corrections skipped   learning rate: 0.001000
6 epochs   Error: 1.045459   Accuracy: 0.726246   0 corrections skipped   learning rate: 0.001000
7 epochs   Error: 1.010350   Accuracy: 0.734053   0 corrections skipped   learning rate: 0.001000
8 epochs   Error: 0.980895   Accuracy: 0.741230   0 corrections skipped   learning rate: 0.001000
9 epochs   Error: 0.955466   Accuracy: 0.747642   0 corrections skipped   learning rate: 0.001000
10 epochs   Error: 0.933105   Accuracy: 0.751124   0 corrections skipped   learning rate: 0.001000
11 epochs   Error: 0.913082   Accuracy: 0.755825   0 corrections skipped   learning rate: 0.001000
12 epochs   Error: 0.895173   Accuracy: 0.759871   0 corrections skipped   learning rate: 0.001000
13 epochs   Error: 0.878771   Accuracy: 0.765824   0 corrections skipped   learning rate: 0.001000
14 epochs   Error: 0.863766   Accuracy: 0.769519   0 corrections skipped   learning rate: 0.001000
15 epochs   Error: 0.849873   Accuracy: 0.772884   0 corrections skipped   learning rate: 0.001000
16 epochs   Error: 0.837107   Accuracy: 0.777601   0 corrections skipped   learning rate: 0.001000
17 epochs   Error: 0.825161   Accuracy: 0.781407   0 corrections skipped   learning rate: 0.001000
18 epochs   Error: 0.814009   Accuracy: 0.782667   0 corrections skipped   learning rate: 0.001000
19 epochs   Error: 0.803533   Accuracy: 0.785799   3 corrections skipped   learning rate: 0.001000
20 epochs   Error: 0.793727   Accuracy: 0.789727   15 corrections skipped   learning rate: 0.001000
21 epochs   Error: 0.784474   Accuracy: 0.791563   16 corrections skipped   learning rate: 0.001000
22 epochs   Error: 0.775705   Accuracy: 0.793661   16 corrections skipped   learning rate: 0.001000
23 epochs   Error: 0.767374   Accuracy: 0.797280   16 corrections skipped   learning rate: 0.001000
24 epochs   Error: 0.759514   Accuracy: 0.800532   16 corrections skipped   learning rate: 0.001000
25 epochs   Error: 0.752024   Accuracy: 0.800832   16 corrections skipped   learning rate: 0.001000
26 epochs   Error: 0.744931   Accuracy: 0.803345   16 corrections skipped   learning rate: 0.001000
27 epochs   Error: 0.738133   Accuracy: 0.805814   16 corrections skipped   learning rate: 0.001000
28 epochs   Error: 0.731620   Accuracy: 0.805718   16 corrections skipped   learning rate: 0.001000
29 epochs   Error: 0.725466   Accuracy: 0.808406   16 corrections skipped   learning rate: 0.001000
30 epochs   Error: 0.719511   Accuracy: 0.810718   16 corrections skipped   learning rate: 0.001000
Finished training
สามารถทำถูก 16 อัน

ลองใช้งาน
จะเห็นข้อผิดพลาดที่เกิดขึ้น สิ่งนี้แก้ไขได้ด้วยเพิ่มจำนวนการ train เข้าไป ความแม่นยำจะเพิ่มขึ้น

เขียนโดย นาย วรรณพงษ์ ภัททิยไพบูลย์
นักศึกษาชั้นปีที่ 1 สาขาวิทยาการคอมพิวเตอร์และสารสนเทศ คณะวิทยาศาสตร์ประยุกต์และวิศวกรรมศาสตร์ มหาวิทยาลัยขอนแก่น วิทยาเขตหนองคาย

ความคิดเห็น

  1. โหลดไฟล์ model ได้จาก https://drive.google.com/drive/folders/0B4OpNSoUT1G4X0NyVzEySllVUjA?usp=sharing

    ตอบลบ
  2. แต่ละ POStag คืออะไรครับ ผมมี data คล้ายๆกันแต่ไม่ได้ทำด้วย NN นะแต่สร้าง tag จากคำข้างเคียง
    ปัญหาที่เจอคือ 1 คำมีหลาย tag มากขึ้นอยุ่กับคำข้างเคียง แนะนำว่าเอาประโยคหน้าและประโยคหลังมาวิเคราะห์ด้วยครับ

    ตอบลบ

แสดงความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

สร้าง Corpus ใหม่ที่เหมือน BEST I Corpus

ลองทำ Text classification สำหรับงานด้าน Quantum Natural Language Processing