【发布时间】:2017-11-26 07:21:37
【问题描述】:
在 Spacy 2.x 中,我使用匹配器在我的文本语料库中查找特定标记。每个规则都有一个 ID(例如'class-1_0')。在解析期间,我使用回调 on_match 来处理每个匹配项。有没有办法直接在回调中检索用于查找匹配的规则。
这是我的示例代码。
txt = ("Aujourd'hui, je vais me faire une tartine au beurre "
"de cacahuète, c'est un pilier de ma nourriture "
"quotidienne.")
nlp = spacy.load('fr')
def on_match(matcher, doc, id, matches):
span = doc[matches[id][1]:matches[id][2]]
print(span)
# find a way to get the corresponding rule without fuzz
matcher = Matcher(nlp.vocab)
matcher.add('class-1_0', on_match, [{'LEMMA': 'pilier'}])
matcher.add('class-1_1', on_match, [{'LEMMA': 'beurre'}, {'LEMMA': 'de'}, {'LEMMA': 'cacahuète'}])
doc = nlp(txt)
matches = matcher(doc)
在这种情况下matches 返回:
[(12071893341338447867, 9, 12), (4566231695725171773, 16, 17)]
12071893341338447867 是基于 class-1_0 的唯一 ID。即使我在matcher._patterns 中进行了一些自省,我也找不到原始规则名称。
如果有人可以帮助我,那就太好了。 非常感谢。
【问题讨论】: