我使用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组件(刚刚修改为包含我自己的自定义名词块)上对此进行了建模(上面的代码),因此我可以确认内置组件给出了相同的结果
有没有办法保存单词矢量信息?我是否需要将术语/平均向量添加到向量存储中
retokenizer应该将
span.vector
设置为新合并令牌的向量。与spacy==3.0.3
和en_core_web_md==3.0.0
一起:输出:
像
tag
和dep
这样的属性在默认情况下也设置为span.root
的属性,因此如果要覆盖默认值,只需指定它们相关问题 更多 >
编程相关推荐