如何让 SpaCy 选择由 "and" 或“,”分隔的名词块作为一个

How do I make SpaCy choose noun chunks separated by "and" or "," as one

我对标题感到抱歉,我真的不知道如何表达它,但希望这个例子能让人明白。

基本上,

对于下面的句子:

Ashley 和 Brian 在喝水。

我希望名词块是“Ashley and Brian”而不是“Ashley”、“Brian”

又如:

衣服类型包括衬衫、裤子和裤子。

我希望名词块是“衬衫、裤子和裤子”,而不是“衬衫”“裤子”“裤子”

如何解决这个问题?

你描述的不是名词块。 conjuncts 功能更接近您的要求。

这可能不适用于复杂的句子,但至少它会涵盖您的示例和典型案例。

import spacy

nlp = spacy.load("en_core_web_sm")

texts = [
        "Ashley and Brian are drinking water.",
        "Types of clothes include shirts, pants and trousers.",
        ]

for text in texts:
    print("-----")
    print(text)
    checked = 0
    doc = nlp(text)
    for tok in doc:
        if tok.i < checked: continue
        if tok.pos_ not in ('NOUN', 'PROPN'): continue

        if tok.conjuncts:
            print(doc[tok.left_edge.i:tok.right_edge.i+1])
            checked = tok.right_edge.i + 1