Spacy:检索与特定索引关联的 words/keys

Spacy: retrieve words/keys associated with a particular index

给定与 <some_model>.vocab.vectors.data 中的行对应的索引,我如何检索相应的词/键?

import spacy

nlp = spacy.load('en_core_web_md')

nlp.vocab.vectors.data[6, :]   # gives me the 6th embedding 

什么 words/keys 映射到第 6 个嵌入?我可以通过像

这样遍历 *.vocab.vectors.items() 来进行某种蛮力搜索
for key, vector in nlp.vocab.vectors.items():
    # check if vector at my index matches this vector
         # print(nlp.vocab.strings[key])

但我希望有更好的方法。

nlp.vocab.vectors.key2row 中进行反向查找以获取使用该嵌入的所有单词:

for key, row in nlp.vocab.vectors.key2row.items():
    if row == 6:
        print(nlp.vocab.strings[key])