전사된 텍스트의 개체명 인식

Python으로 배우는 음성 언어 처리

Daniel Bourke

Machine Learning Engineer/YouTube Creator

spaCy 설치

# spaCy 설치
$ pip install spacy
# spaCy 언어 모델 다운로드
$ python -m spacy download en_core_web_sm
Python으로 배우는 음성 언어 처리

spaCy 사용하기

import spacy

# spaCy 언어 모델 로드 nlp = spacy.load("en_core_web_sm")
# spaCy 문서 생성
doc = nlp("I'd like to talk about a smartphone I ordered on July 31st from your 
Sydney store, my order number is 40939440. I spoke to Georgia about it last week.")
Python으로 배우는 음성 언어 처리

spaCy 토큰

# 토큰과 위치 출력
for token in doc:
  print(token.text, token.idx)
I 0
'd 1
like 4
to 9
talk 12
about 17
a 23
smartphone 25...
Python으로 배우는 음성 언어 처리

spaCy 문장

# 문서의 문장 출력
for sentences in doc.sents:
  print(sentence)
I'd like to talk about a smartphone I ordered on July 31st from your Sydney store, 
my order number is 4093829.
I spoke to one of your customer service team, Georgia, yesterday.
Python으로 배우는 음성 언어 처리

spaCy 개체명

spaCy의 기본 개체명 예시:

  • PERSON 사람(가상 인물 포함)
  • ORG 회사, 기관 등
  • GPE 국가, 도시, 주
  • PRODUCT 물체, 차량, 음식 등(서비스 제외)
  • DATE 절대/상대 날짜 또는 기간
  • TIME 하루보다 작은 시간 단위
  • MONEY 금액(단위 포함)
  • CARDINAL 다른 유형이 아닌 수사
Python으로 배우는 음성 언어 처리

spaCy 개체명

# 문서에서 개체명 찾기
for entity in doc.ents:
  print(entity.text, entity.label_)
July 31st DATE
Sydney GPE
4093829 CARDINAL
one CARDINAL
Georgia GPE
yesterday DATE
Python으로 배우는 음성 언어 처리

사용자 정의 개체명

# EntityRuler 클래스 가져오기
from spacy.pipeline import EntityRuler
# spaCy 파이프라인 확인
print(nlp.pipeline)
[('tagger', <spacy.pipeline.pipes.Tagger at 0x1c3aa8a470>),
 ('parser', <spacy.pipeline.pipes.DependencyParser at 0x1c3bb60588>),
 ('ner', <spacy.pipeline.pipes.EntityRecognizer at 0x1c3bb605e8>)]
Python으로 배우는 음성 언어 처리

파이프라인 변경

# EntityRuler 인스턴스 생성
ruler = EntityRuler(nlp)
# 토큰 패턴 추가
ruler.add_patterns([{"label":"PRODUCT", "pattern": "smartphone"}])
# ner 앞에 파이프라인에 추가
nlp.add_pipe(ruler, before="ner")
# 업데이트된 파이프라인 확인
nlp.pipeline
Python으로 배우는 음성 언어 처리

파이프라인 변경

[('tagger', <spacy.pipeline.pipes.Tagger at 0x1c1f9c9b38>),
 ('parser', <spacy.pipeline.pipes.DependencyParser at 0x1c3c9cba08>),
 ('entity_ruler', <spacy.pipeline.entityruler.EntityRuler at 0x1c1d834b70>),
 ('ner', <spacy.pipeline.pipes.EntityRecognizer at 0x1c3c9cba68>)]
Python으로 배우는 음성 언어 처리

새 파이프라인 테스트

# 새 개체 규칙 테스트
for entity in doc.ents:
    print(entity.text, entity.label_)
smartphone PRODUCT
July 31st DATE
Sydney GPE
4093829 CARDINAL
one CARDINAL
Georgia GPE
yesterday DATE
Python으로 배우는 음성 언어 처리

spaCy 연습 시작!

Python으로 배우는 음성 언어 처리

Preparing Video For Download...