【发布时间】:2019-11-12 08:52:36
【问题描述】:
我希望输出为["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])
【问题讨论】:
-
你的意思是说你想让
+表现得“贪婪”,只返回最左边索引中最长的匹配? -
正确的@WiktorStribiżew,我想要最长的匹配。你能帮我怎么做吗
-
谁能帮帮我,真的很紧急。
-
在下面查看我的回答,它对你有用吗?
-
@WiktorStribiżew ,是的,它对我有用,非常感谢您的解决方案。
标签: python python-3.x nlp spacy matcher