spaCy에서 다중 패턴을 사용하는 EntityRuler
EntityRuler를 사용하면 doc.ents에 엔티티를 추가해 개체명 인식 성능을 향상할 수 있어요. 이 연습 문제에서는 기존 nlp 파이프라인에 EntityRuler 컴포넌트를 추가해 여러 엔티티가 올바르게 분류되는지 확인해 보겠습니다.
en_core_web_sm 모델은 이미 로드되어 nlp로 제공돼요. 예시 텍스트는 example_text에서 확인할 수 있으며, 각각 nlp와 doc을 사용해 spaCy 모델과 example_text의 Doc 컨테이너에 접근할 수 있어요.
이 연습은 강의의 일부입니다
spaCy로 배우는 자연어 처리
연습 안내
nlp모델로example_text의 엔티티 텍스트와 유형을 튜플 리스트로 출력하세요.- 소문자
brother와sisters를PERSON레이블에 매칭하는 여러 패턴을 정의하세요. nlp파이프라인에EntityRuler컴포넌트를 추가하고,patterns를EntityRuler에 추가하세요.nlp모델로example_text의 엔티티 텍스트와 유형을 튜플로 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
nlp = spacy.load("en_core_web_md")
# Print a list of tuples of entities text and types in the example_text
print("Before EntityRuler: ", [____ for ____ in nlp(____).____], "\n")
# Define pattern to add a label PERSON for lower cased sisters and brother entities
patterns = [{"label": ____, "pattern": [{"lower": ____}]},
{"label": ____, "pattern": [{"lower": ____}]}]
# Add an EntityRuler component and add the patterns to the ruler
ruler = nlp.____("entity_ruler")
ruler.____(____)
# Print a list of tuples of entities text and types
print("After EntityRuler: ", [(ent.____, ent.____) for ent in nlp(example_text).____])