2
我有一個文本以及索引條目,其中一些表示文本中出現的重要多字表達式(MWE)(例如生物學文本的「海綿骨」)。我想用這些條目在SpaCy中構造一個自定義匹配器,以便我可以識別文本中MWE的出現。另一個要求是我需要匹配事件來保留MWE組成詞的詞性化表示和POS標籤。Spacy中的多字表達式識別
我已經看過現有的spaCy例子做類似的事情,但我似乎無法得到模式。
我有一個文本以及索引條目,其中一些表示文本中出現的重要多字表達式(MWE)(例如生物學文本的「海綿骨」)。我想用這些條目在SpaCy中構造一個自定義匹配器,以便我可以識別文本中MWE的出現。另一個要求是我需要匹配事件來保留MWE組成詞的詞性化表示和POS標籤。Spacy中的多字表達式識別
我已經看過現有的spaCy例子做類似的事情,但我似乎無法得到模式。
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)