Matcher返回一些重复项

问题描述 投票:2回答:1

我想输出为["good customer service","great ambience"],但我却得到了["good customer","good customer service","great ambience"],因为模式也与良好的客户匹配,但这句话没有任何意义。如何删除这些重复项

import spacy
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
doc = nlp("good customer service and great ambience")
matcher = Matcher(nlp.vocab)

# Create a pattern matching two tokens: adjective followed by one or more noun
 pattern = [{"POS": 'ADJ'},{"POS": 'NOUN', "OP": '+'}]

matcher.add("ADJ_NOUN_PATTERN", None,pattern)

matches = matcher(doc)
print("Matches:", [doc[start:end].text for match_id, start, end in matches])

python python-3.x nlp spacy matcher
1个回答
0
投票

您可以通过将元组与起始索引分组并仅保留具有最大终止索引的元组来对匹配进行后处理:

from itertools import *

#...

matches = matcher(doc)
results = [max(list(group),key=lambda x: x[2]) for key, group in groupby(matches, lambda prop: prop[1])]    
print("Matches:", [doc[start:end].text for match_id, start, end in results])
# => Matches: ['good customer service', 'great ambience']

groupby(matches, lambda prop: prop[1])将按起始索引对匹配项进行分组,从而得到[(5488211386492616699, 0, 2), (5488211386492616699, 0, 3)](5488211386492616699, 4, 6)max(list(group),key=lambda x: x[2])将抓取末端索引(值3)最大的项目。

© www.soinside.com 2019 - 2024. All rights reserved.