시작하기무료로 시작하기

spaCy에서 확장 문법으로 매칭하기

규칙 기반 정보 추출은 어떤 NLP 파이프라인에서도 핵심입니다. Matcher 클래스는 중괄호 안에 일부 연산자를 사용해 패턴을 더 표현력 있게 만들 수 있어요. 이 연산자들은 확장 비교를 위해 쓰이며, Python의 in, not in 및 비교 연산자와 유사하게 보입니다. 이 연습 문제에서는 예시 텍스트에서 주어진 용어를 찾기 위해 spaCy의 매칭 기능인 Matcher를 연습해 보겠습니다.

Matcher 클래스는 이미 spacy.matcher 라이브러리에서 임포트되어 있습니다. 이 연습에서는 예시 텍스트의 Doc 컨테이너인 doc을 사용합니다. 미리 로드된 spaCy 모델은 nlp에서 접근할 수 있어요.

이 연습은 강의의 일부입니다

spaCy로 배우는 자연어 처리

강의 보기

연습 안내

  • Matchernlp를 사용해 matcher 객체를 정의하세요.
  • IN 연산자를 사용해 tiny squarestiny mouthful을 매칭하는 패턴을 정의하세요.
  • 이 패턴으로 doc에서 매치를 찾으세요.
  • 각 매치의 시작·끝 토큰 인덱스와 해당 텍스트 구간을 출력하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

nlp = spacy.load("en_core_web_sm")
doc = nlp(example_text)

# Define a matcher object
matcher = Matcher(nlp.____)
# Define a pattern to match tiny squares and tiny mouthful
pattern = [{"lower": ____}, {"lower": {____: ["squares", "mouthful"]}}]

# Add the pattern to matcher object and find matches
matcher.____("CustomMatcher", [____])
matches = ____(____)

# Print out start and end token indices and the matched text span per match
for match_id, start, end in matches:
    print("Start token: ", ____, " | End token: ", ____, "| Matched text: ", doc[____:____].text)
코드 편집 및 실행