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는 nlp 파이프라인의 pos_ 기능에 품사를 담습니다spacy.explain()은 주어진 품사 태그를 설명합니다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')]
| 개체 유형 | 설명 |
|---|---|
| PERSON | 인물 또는 가족 |
| ORG | 회사, 기관 등 |
| GPE | 지정학적 개체: 국가, 도시 등 |
| LOC | 비-GPE 위치: 산맥 등 |
| DATE | 절대/상대 날짜 또는 기간 |
| TIME | 하루보다 작은 시간 단위 |
spaCy 모델은 NER 파이프라인 구성요소로 개체명을 추출합니다doc.ents 속성으로 확인합니다spaCy는 각 개체에 라벨(.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에는 현대적 시각화 도구 displaCy가 있습니다displaCy 엔티티 뷰어는 개체명과 라벨을 하이라이트합니다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로 배우는 자연어 처리