翻阅古今
您不妨尝试一下spacy。以下模式将捕获:名词短语后跟is或are可选地跟随not后面跟着一个形容词import spacyfrom spacy.matcher import Matchernlp = spacy.load('en_core_web_sm')output = []doc = nlp('The product is very good')matcher = Matcher(nlp.vocab)matcher.add("mood",None,[{"LOWER":{"IN":["is","are"]}},{"LOWER":{"IN":["no","not"]},"OP":"?"},{"LOWER":"very","OP":"?"},{"POS":"ADJ"}])for nc in doc.noun_chunks: d = doc[nc.root.right_edge.i+1:nc.root.right_edge.i+1+3] matches = matcher(d) if matches: _, start, end = matches[0] output.append((nc.text, d[start+1:end].text)) print(output)[('The product', 'very good')]或者,您可以使用依赖解析器中的信息来扩展匹配模式,这将添加形容词短语的定义:output = []matcher = Matcher(nlp.vocab, validate=True)matcher.add("mood",None,[{"LOWER":{"IN":["is","are"]}},{"LOWER":{"IN":["no","not"]},"OP":"?"},{"DEP":"advmod","OP":"?"},{"DEP":"acomp"}])for nc in doc.noun_chunks: d = doc[nc.root.right_edge.i+1:nc.root.right_edge.i+1+3] matches = matcher(d) if matches: _, start, end = matches[0] output.append((nc.text, d[start+1:end].text)) print(output)[('The product', 'very good')]