คุณสมบัติทางภาษาใน spaCy

การประมวลผลภาษาธรรมชาติด้วย spaCy

Azadeh Mobasher

Principal Data Scientist

การแท็ก POS

  • จัดหมวดหมู่คำตามหน้าที่และบริบทในประโยค
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 ด้วย spaCy

 

  • การแท็ก POS ช่วยยืนยันความหมายของคำ
  • คำบางคำ เช่น watch เป็นได้ทั้งคำนามและกริยา
  • spaCy เก็บแท็ก POS ไว้ใน feature pos_ ของ nlp pipeline
  • spacy.explain() ใช้อธิบายแท็ก POS ที่กำหนด

คอมโพเนนต์ POS Tagger

การประมวลผลภาษาธรรมชาติด้วย spaCy

การแท็ก POS ด้วย spaCy

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')]
การประมวลผลภาษาธรรมชาติด้วย spaCy

การรู้จำ named entity

  • named entity คือคำหรือวลีที่อ้างถึงสิ่งที่มีชื่อเฉพาะ
  • การรู้จำ named entity (NER) จำแนก named entity ออกเป็นหมวดหมู่ที่กำหนดไว้ล่วงหน้า
ประเภท entity คำอธิบาย
PERSON บุคคลหรือตระกูลที่มีชื่อ
ORG บริษัท สถาบัน ฯลฯ
GPE หน่วยงานภูมิรัฐศาสตร์ ประเทศ เมือง ฯลฯ
LOC สถานที่ที่ไม่ใช่ GPE เช่น เทือกเขา ฯลฯ
DATE วันที่หรือช่วงเวลาแบบสัมบูรณ์หรือสัมพัทธ์
TIME เวลาที่น้อยกว่าหนึ่งวัน
การประมวลผลภาษาธรรมชาติด้วย spaCy

NER และ spaCy

 

  • โมเดล spaCy ดึง named entity โดยใช้คอมโพเนนต์ NER ใน pipeline
  • เข้าถึง named entity ได้ผ่าน property doc.ents
  • spaCy จะแท็ก label ให้แต่ละ entity ด้วย (.label_)

คอมโพเนนต์ NER

การประมวลผลภาษาธรรมชาติด้วย spaCy

NER และ spaCy

 

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')]
การประมวลผลภาษาธรรมชาติด้วย spaCy

NER และ spaCy

  • เข้าถึงประเภท entity ของแต่ละ token ใน 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

displaCy

 

  • spaCy มาพร้อมกับเครื่องมือแสดงผลสมัยใหม่: displaCy
  • entity visualizer ของ displaCy ไฮไลต์ 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")

ผลลัพธ์ NER จาก displaCy

การประมวลผลภาษาธรรมชาติด้วย spaCy

มาฝึกกันเถอะ!

การประมวลผลภาษาธรรมชาติด้วย spaCy

Preparing Video For Download...