Spacy 문서는 여러 구와 함께 Matcher 클래스 사용시 명확하지 않지만 Github 레포에는 example과 일치하는 멀티 어구가 있습니다.
나는 최근에 똑같은 도전에 직면 해 있었고 아래와 같이 작업을했습니다. 내 텍스트 파일에는 한 줄에 하나의 레코드가 포함되어 있으며 구 및 설명은 '::'로 구분됩니다.
import spacy
import io
from spacy.matcher import PhraseMatcher
nlp = spacy.load('en')
text = nlp(u'Your text here')
rules = list()
# Create a list of tuple of phrase and description from the file
with io.open('textfile','r',encoding='utf8') as doc:
rules = [tuple(line.rstrip('\n').split('::')) for line in doc]
# convert the phrase string to a spacy doc object
rules = [(nlp(item[0].lower()),item[-1]) for item in rules ]
# create a dictionary for accessing value using the string as the index which is returned by matcher class
rules_dict = dict()
for key,val in rules:
rules_dict[key.text]=val
# get just the phrases from rules list
rules_phrases = [item[0] for item in rules]
# match using the PhraseMatcher class
matcher = PhraseMatcher(nlp.vocab,rules_phrases)
matches = matcher(text)
result = list()
for start,end,tag,label,m in matches:
result.append({"start":start,"end":end,"phrase":label,"desc":rules_dict[label]})
print(result)