Python 在spaCy中合并自定义名词块(使用retokenize)时,如何平均向量?

Python 在spaCy中合并自定义名词块(使用retokenize)时,如何平均向量?,python,nlp,spacy,Python,Nlp,Spacy,我使用spaCy的统计模型(例如,基于词性标记和依赖项的名词块)和基于规则的匹配生成名词块,以捕获(用户提供的)上下文特定的名词块 对于其他下游任务,我正在重新整理这些名词块(span),这在大多数情况下都很好。但是,令牌的向量表示(token.vector)设置为全零。是否有保留向量信息的方法,例如通过平均单个令牌向量并将其分配给重新排序的令牌 我用密码试过了 def tokenise_noun_chunks(doc) if not doc.has_annotation("D

我使用spaCy的统计模型(例如,基于词性标记和依赖项的名词块)和基于规则的匹配生成名词块,以捕获(用户提供的)上下文特定的名词块

对于其他下游任务,我正在重新整理这些名词块(span),这在大多数情况下都很好。但是,令牌的向量表示(token.vector)设置为全零。是否有保留向量信息的方法,例如通过平均单个令牌向量并将其分配给重新排序的令牌

我用密码试过了

def tokenise_noun_chunks(doc)
    if not doc.has_annotation("DEP"):
        return doc

    all_noun_chunks = list(doc.noun_chunks) + doc._.custom_noun_chunks

    with doc.retokenize() as retokenizer:
        for span in all_noun_chunks:
            # if I print(span.vector) here, I get the correctly averaged vector
            attrs = {"tag": span.root.tag, "dep": span.root.dep}
            retokenizer.merge(np, attrs=attrs)
        return doc
…但是当我检查返回的向量中的名词块时,我得到了一个零数组。我已经在内置的merge_Non_chunks组件(刚刚修改为包含我自己的自定义名词块)上对此进行了建模(上面的代码),因此我可以确认内置组件给出了相同的结果


有没有办法保存单词矢量信息?是否需要将术语/平均向量添加到向量存储?

重新排序器应将
span.vector
设置为新合并令牌的向量。使用
spacy==3.0.3
en_core\u web\u md==3.0.0

导入空间
nlp=spacy.load(“en_core\u web\u md”)
doc=nlp(“这是一个句子”)
使用doc.retokenize()作为retokenizer:
对于doc.noun_块中的块:
retokenizer.merge(块)
对于文档中的令牌:
打印(令牌,令牌.vector[:5])
输出:

This[-0.087595 0.35502 0.063868 0.29292-0.23635]
is[-0.084961 0.502 0.0023823-0.16755 0.30721]
一句话[-0.093156 0.1371495-0.3072550.2993 0.1383735]
. [ 0.012001  0.20751  -0.12578  -0.59325   0.12525 ]
默认情况下,
tag
dep
等属性也设置为
span.root
的属性,因此如果要覆盖默认值,只需指定它们