Xử lý ngôn ngữ tự nhiên với spaCy
Azadeh Mobasher
Principal Data Scientist
| POS | Mô tả | Ví dụ |
|---|---|---|
| VERB | Động từ | run, eat, ate, take |
| NOUN | Danh từ | man, airplane, tree, flower |
| ADJ | Tính từ | big, old, incompatible, conflicting |
| ADV | Trạng từ | very, down, there, tomorrow |
| CONJ | Liên từ | and, or, but |
spaCy lưu POS trong thuộc tính pos_ của pipeline nlpspacy.explain() giải thích một thẻ POSverb_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')]
| Loại thực thể | Mô tả |
|---|---|
| PERSON | Người hoặc gia đình có tên |
| ORG | Công ty, tổ chức, v.v. |
| GPE | Thực thể địa-chính trị: quốc gia, thành phố, v.v. |
| LOC | Địa điểm không phải GPE: dãy núi, v.v. |
| DATE | Ngày/khung thời gian tuyệt đối hay tương đối |
| TIME | Thời gian nhỏ hơn một ngày |
spaCy trích xuất thực thể có tên bằng thành phần pipeline NERdoc.entsspaCy cũng gán nhãn cho mỗi thực thể (.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
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 có công cụ trực quan hiện đại: displaCydisplaCy làm nổi bật thực thể có tên và nhãn của chúngimport 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")
Xử lý ngôn ngữ tự nhiên với spaCy