빈 spaCy 모델에서 EntityRuler 사용하기
EntityRuler는 doc.ents에 엔티티를 추가할 수 있게 해줘요. 이는 고유 명칭 인식용 spaCy 파이프라인 컴포넌트인 EntityRecognizer와 결합해 정확도를 높일 수도 있고, 단독으로 사용해 규칙 기반 엔티티 인식 시스템을 구현할 수도 있어요. 이 연습 문제에서는 빈 spaCy 영어 모델에 EntityRuler 컴포넌트를 추가하고, 순수하게 규칙 기반 고유 명칭 인식을 사용해 주어진 text의 엔티티를 분류해 보겠어요.
spaCy 패키지는 이미 임포트되어 있고, 빈 spaCy 영어 모델이 nlp로 준비되어 있어요. 소문자 OpenAI와 Microsoft를 ORG로 분류하기 위한 patterns 목록도 미리 만들어 두었어요.
이 연습은 강의의 일부입니다
spaCy로 배우는 자연어 처리
연습 안내
- 파이프라인에
EntityRuler컴포넌트를 생성해서 추가하세요. - 제공된 패턴을
EntityRuler컴포넌트에 추가하세요. - 주어진
text에 모델을 실행해 해당Doc컨테이너를 만드세요. Doc컨테이너에 있는 모든 엔티티에 대해 (엔티티 텍스트, 엔티티 유형) 튜플을 출력하세요
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
nlp = spacy.blank("en")
patterns = [{"label": "ORG", "pattern": [{"LOWER": "openai"}]},
{"label": "ORG", "pattern": [{"LOWER": "microsoft"}]}]
text = "OpenAI has joined forces with Microsoft."
# Add EntityRuler component to the model
entity_ruler = nlp.____("entity_ruler")
# Add given patterns to the EntityRuler component
entity_ruler.____(____)
# Run the model on a given text
doc = nlp(____)
# Print entities text and type for all entities in the Doc container
print([(ent.____, ent.____) for ent in doc.____])