การประมวลผลภาษาธรรมชาติด้วย spaCy
Azadeh Mobasher
Principal Data Scientist
| POS | คำอธิบาย | ตัวอย่าง |
|---|---|---|
| VERB | กริยา | run, eat, ate, take |
| NOUN | คำนาม | man, airplane, tree, flower |
| ADJ | คำคุณศัพท์ | big, old, incompatible, conflicting |
| ADV | คำกริยาวิเศษณ์ | very, down, there, tomorrow |
| CONJ | คำสันธาน | and, or, but |
spaCy เก็บแท็ก POS ไว้ใน feature pos_ ของ nlp pipelinespacy.explain() ใช้อธิบายแท็ก POS ที่กำหนดverb_sent = "I watch TV."print([(token.text, token.pos_, spacy.explain(token.pos_)) for token in nlp(verb_sent)])
[('I', 'PRON', 'pronoun'),
('watch', 'VERB', 'verb'),
('TV', 'NOUN', 'noun'),
('.', 'PUNCT', 'punctuation')]
noun_sent = "I left without my watch."print([(token.text, token.pos_, spacy.explain(token.pos_)) for token in nlp(noun_sent)])
[('I', 'PRON', 'pronoun'),
('left', 'VERB', 'verb'),
('without', 'ADP', 'adposition'),
('my', 'PRON', 'pronoun'),
('watch', 'NOUN', 'noun'),
('.', 'PUNCT', 'punctuation')]
| ประเภท entity | คำอธิบาย |
|---|---|
| PERSON | บุคคลหรือตระกูลที่มีชื่อ |
| ORG | บริษัท สถาบัน ฯลฯ |
| GPE | หน่วยงานภูมิรัฐศาสตร์ ประเทศ เมือง ฯลฯ |
| LOC | สถานที่ที่ไม่ใช่ GPE เช่น เทือกเขา ฯลฯ |
| DATE | วันที่หรือช่วงเวลาแบบสัมบูรณ์หรือสัมพัทธ์ |
| TIME | เวลาที่น้อยกว่าหนึ่งวัน |
spaCy ดึง named entity โดยใช้คอมโพเนนต์ NER ใน pipelinedoc.entsspaCy จะแท็ก label ให้แต่ละ entity ด้วย (.label_)
import spacy nlp = spacy.load("en_core_web_sm") text = "Albert Einstein was genius." doc = nlp(text)print([(ent.text, ent.start_char, ent.end_char, ent.label_) for ent in doc.ents])
>>> [('Albert Einstein', 0, 15, 'PERSON')]
Doc container ได้เช่นกัน
import spacy nlp = spacy.load("en_core_web_sm") text = "Albert Einstein was genius." doc = nlp(text)print([(token.text, token.ent_type_) for token in doc])
>>> [('Albert', 'PERSON'), ('Einstein', 'PERSON'),
('was', ''), ('genius', ''), ('.', '')]
spaCy มาพร้อมกับเครื่องมือแสดงผลสมัยใหม่: displaCydisplaCy ไฮไลต์ named entity และ label ของแต่ละรายการimport spacy from spacy import displacy text = "Albert Einstein was genius." nlp = spacy.load("en_core_web_sm") doc = nlp(text)displacy.serve(doc, style="ent")
การประมวลผลภาษาธรรมชาติด้วย spaCy