PresidioDocumentCleaner
PresidioDocumentCleaner
PresidioDocumentCleaner는 문서의 텍스트 내용에서 개인 식별 정보(PII)를 <PERSON>이나 <EMAIL_ADDRESS> 같은 엔티티 타입 플레이스홀더로 바꿔 줘요. 원본 문서는 변경하지 않아요. 텍스트 내용이 없는 문서는 그대로 통과하죠.
출처: 문서
본문
| 항목 | 내용 |
|---|---|
| 파이프라인에서 가장 흔한 위치 | 인덱싱 파이프라인에서 Document Store에 문서를 쓰기 전 |
| 필수 run 변수 | documents: Document 객체 목록 |
| 출력 변수 | documents: PII가 교체된 Document 객체 목록 |
| API reference | Presidio |
| GitHub 링크 | presidio 통합 |
| 패키지 이름 | presidio-haystack |
개요
Microsoft Presidio는 PII 감지와 익명화를 위한 오픈소스 프레임워크예요. PresidioDocumentCleaner는 Presidio의 Analyzer와 Anonymizer 엔진을 사용해 문서 텍스트를 스캔하고, 감지된 엔티티를 <PERSON>이나 <EMAIL_ADDRESS> 같은 타입 플레이스홀더로 교체해요.
이 기능은 문서의 정제된 버전을 Document Store에 저장하고 싶을 때 유용해요. 예를 들어 민감한 정보가 인덱싱되거나 검색 결과로 반환되는 것을 막고 싶을 때죠.
텍스트를 수정하지 않고 PII를 주석만 달고 싶다면 PresidioEntityExtractor를 보세요. 사용자 쿼리 같은 일반 문자열을 정제하려면 PresidioTextCleaner를 참고하세요.
구성
| 파라미터 | 기본값 | 설명 |
|---|---|---|
language |
"en" |
PII 감지를 위한 ISO 639-1 언어 코드. 지원 언어에 대해 적절한 spaCy 모델이 자동 선택돼요. Presidio 지원 언어 참고 |
entities |
None |
감지·익명화할 PII 엔티티 타입 목록 (예: ["PERSON", "EMAIL_ADDRESS"]). None이면 지원되는 모든 타입을 감지해요. 지원 엔티티 참고 |
score_threshold |
0.35 |
감지된 엔티티가 익명화되기 위한 최소 신뢰 점수(0–1) |
models |
None |
고급 오버라이드: spaCy 모델 구성의 명시적 목록 (예: [{"lang_code": "fr", "model_name": "fr_core_news_md"}]). 특정 모델 변형이나 내장 매핑에 없는 언어가 필요할 때만 사용하세요. None이면 language에 따라 모델이 자동 선택돼요 |
사용법
PresidioDocumentCleaner를 사용하려면 presidio-haystack 패키지를 설치하세요.
pip install presidio-haystack
단독으로 사용하기
from haystack import Document
from haystack_integrations.components.preprocessors.presidio import (
PresidioDocumentCleaner,
)
cleaner = PresidioDocumentCleaner()
result = cleaner.run(
documents=[
Document(content="Contact Alice Smith at [email protected] or 212-555-1234."),
],
)
print(result["documents"][0].content)
# Contact <PERSON> at <EMAIL_ADDRESS> or <PHONE_NUMBER>.
파이프라인에서 사용하기
from haystack import Document, Pipeline
from haystack.components.writers import DocumentWriter
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.preprocessors.presidio import (
PresidioDocumentCleaner,
)
document_store = InMemoryDocumentStore()
indexing_pipeline = Pipeline()
indexing_pipeline.add_component("cleaner", PresidioDocumentCleaner())
indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
indexing_pipeline.connect("cleaner", "writer")
indexing_pipeline.run(
{
"cleaner": {
"documents": [
Document(content="Alice Smith's email is [email protected]"),
Document(content="Call Bob at 212-555-9876"),
],
},
},
)
커스텀 파라미터 사용하기
entities를 사용해 실제로 관심 있는 PII 타입에만 익명화를 제한할 수 있어요. 필요 없는 recognizer를 건너뛰므로 오탐(거짓 양성)을 줄이고 성능도 높여 주죠.
score_threshold로 정밀도-재현율 트레이드오프를 조정할 수 있어요. 기본값 0.35는 넓게 잡기 때문에 일부 오탐을 익명화할 수 있어요. 텍스트를 교체하기 전에 높은 신뢰도가 필요하면 높이고요(예: 0.7), PII를 놓치는 것이 더 큰 위험이라면 낮추면 돼요.
from haystack_integrations.components.preprocessors.presidio import (
PresidioDocumentCleaner,
)
cleaner = PresidioDocumentCleaner(
language="de",
entities=["PERSON", "EMAIL_ADDRESS"], # only anonymize names and emails
score_threshold=0.7, # higher precision, fewer false positives
)
비영어 언어
내장 매핑에 있는 언어라면 language만 설정하면 돼요. 적절한 spaCy 모델이 워밍업 시점에 자동으로 선택되고 로드되죠.
from haystack import Document
from haystack_integrations.components.preprocessors.presidio import (
PresidioDocumentCleaner,
)
# No `models` parameter needed — de_core_news_lg is selected automatically
cleaner = PresidioDocumentCleaner(language="de")
result = cleaner.run(
documents=[
Document(
content="Mein Name ist Hans Müller und meine E-Mail ist [email protected]",
),
],
)
print(result["documents"][0].content)
# Mein Name ist <PERSON> und meine E-Mail ist <EMAIL_ADDRESS>
지원 언어와 기본 모델은 PresidioDocumentCleaner.SPACY_DEFAULT_MODELS에 나열돼 있어요. 그 매핑에 없는 언어를 models 없이 사용하면 워밍업 시점에 지원 언어 코드 목록과 함께 ValueError가 발생해요.
기본이 아닌 모델 변형이나 내장 매핑 밖의 언어를 사용하려면 models를 명시적으로 전달하세요:
cleaner = PresidioDocumentCleaner(
language="fr",
models=[{"lang_code": "fr", "model_name": "fr_core_news_md"}],
)