SpacyNamedEntityExtractor

SpacyNamedEntityExtractor

SpacyNamedEntityExtractor 는 텍스트에서 미리 정의된 개체(엔티티)를 추출해 문서의 meta 필드에 기록하는 컴포넌트예요. spaCy 모델을 이용해 사람 이름, 기관, 장소 같은 개체를 자동으로 인식하고 분류해요.

출처: 문서

본문

개요 (Overview)

SpacyNamedEntityExtractor 는 텍스트 안의 개체(텍스트의 span)를 찾아요. 추출기는 개체를 클래스에 따라(사람 이름, 기관, 장소 등) 자동으로 인식하고 그룹화해요. 정확한 클래스는 컴포넌트를 초기화할 때 쓰는 모델이 결정해요.

SpacyNamedEntityExtractor 는 문서 목록을 입력으로 받아, 그 문서들의 meta 데이터에 NamedEntityAnnotations 를 채워 넣어 같은 문서 목록을 반환해요. NamedEntityAnnotation 은 개체 타입과 span의 시작·끝 위치로 구성돼요. 예: NamedEntityAnnotation(entity='PERSON', start=11, end=16, score=None).

SpacyNamedEntityExtractor 를 초기화할 때 model 을 설정해야 해요. 선택적으로 spaCy 파이프라인에 전달되는 pipeline_kwargs 를 설정할 수도 있고, 컴포넌트를 실행하는 데 사용할 device 도 함께 지정할 수 있어요.

사용법 (Usage)

SpacyNamedEntityExtractor 를 쓰려면 spacy-haystack 패키지를 설치하세요:

pip install spacy-haystack

이 컴포넌트는 NER 컴포넌트를 포함한 모든 spaCy 모델과 함께 동작해요.

SpacyNamedEntityExtractor 는 Documents 목록을 입력으로 받아요. 추출기는 문서의 원문을 어노테이션하고, 그 어노테이션을 문서의 meta 딕셔너리의 named_entities 키 아래에 저장해요.

from haystack.dataclasses import Document
from haystack_integrations.components.extractors.spacy import (
    SpacyNamedEntityExtractor,
)

extractor = SpacyNamedEntityExtractor(model="en_core_web_sm")
documents = [
    Document(content="My name is Clara and I live in Berkeley, California."),
    Document(content="I'm Merlin, the happy pig!"),
    Document(content="New York State is home to the Empire State Building."),
]
result = extractor.run(documents)
print(result["documents"])

예시 결과는 다음과 같아요:

[Document(id=aec840d1b6c85609f4f16c3e222a5a25fd8c4c53bd981a40c1268ab9c72cee10, content: 'My name is Clara and I live in Berkeley, California.', meta: {'named_entities': [NamedEntityAnnotation(entity='PERSON', start=11, end=16, score=None), NamedEntityAnnotation(entity='GPE', start=31, end=39, score=None), NamedEntityAnnotation(entity='GPE', start=41, end=51, score=None)]}),
 Document(id=98f1dc5d0ccd9d9950cd191d1076db0f7af40c401dd7608f11c90cb3fc38c0c2, content: 'I'm Merlin, the happy pig!', meta: {'named_entities': [NamedEntityAnnotation(entity='PERSON', start=4, end=10, score=None)]}),
 Document(id=44948ea0eec018b33aceaaedde4616eb9e93ce075e0090ec1613fc145f84b4a9, content: 'New York State is home to the Empire State Building.', meta: {'named_entities': [NamedEntityAnnotation(entity='GPE', start=0, end=14, score=None), NamedEntityAnnotation(entity='ORG', start=26, end=51, score=None)]})]

저장된 어노테이션 가져오기 (Get stored annotations)

이 컴포넌트는 Document 에 저장된 어노테이션을 투명하게 조회할 수 있는 get_stored_annotations 헬퍼 클래스 메서드를 포함해요:

from haystack.dataclasses import Document
from haystack_integrations.components.extractors.spacy import (
    SpacyNamedEntityExtractor,
)

extractor = SpacyNamedEntityExtractor(model="en_core_web_sm")
documents = [
    Document(content="My name is Clara and I live in Berkeley, California."),
    Document(content="I'm Merlin, the happy pig!"),
    Document(content="New York State is home to the Empire State Building."),
]
result = extractor.run(documents)
annotations = [
    SpacyNamedEntityExtractor.get_stored_annotations(doc) for doc in result["documents"]
]
print(annotations)

# If a Document doesn't contain any annotations, this returns None.
new_doc = Document(content="In one of many possible worlds...")
assert SpacyNamedEntityExtractor.get_stored_annotations(new_doc) is None

더 알아보기 (Learn more)