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

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

Daniel Bourke

Machine Learning Engineer/YouTube Creator

spaCy 설치

# Install spaCy
$ pip install spacy
# Download spaCy language model
$ python -m spacy download en_core_web_sm
Python으로 배우는 음성 언어 처리

spaCy 사용하기

import spacy

# Load spaCy language model nlp = spacy.load("en_core_web_sm")
# Create a spaCy doc
text = ("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.")
doc = nlp(text)
Python으로 배우는 음성 언어 처리

spaCy 토큰

# Show different tokens and positions
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 문장

# Show sentences in doc
for sentence 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 개체명

# Find named entities in doc
for entity in doc.ents:
  print(entity.text, entity.label_)
July 31st DATE
Sydney GPE
4093829 CARDINAL
one CARDINAL
Georgia GPE
yesterday DATE
Python으로 배우는 음성 언어 처리

사용자 지정 개체명

# Check spaCy pipeline
print(nlp.pipe_names)
['tok2vec', 'tagger', 'parser',
 'attribute_ruler', 'lemmatizer', 'ner']
Python으로 배우는 음성 언어 처리

파이프라인 변경하기

# Add entity_ruler to the pipeline, before ner
ruler = nlp.add_pipe("entity_ruler", before="ner")
# Add token pattern to ruler
ruler.add_patterns([{"label":"PRODUCT", "pattern": "smartphone"}])
# Check updated pipeline
nlp.pipe_names
Python으로 배우는 음성 언어 처리

파이프라인 변경하기

['tok2vec', 'tagger', 'parser', 'attribute_ruler',
 'lemmatizer', 'entity_ruler', 'ner']
Python으로 배우는 음성 언어 처리

새 파이프라인 테스트하기

# Recreate the doc with the updated pipeline
doc = nlp(text)
# Test new entity rule
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...