词表中的相似词
在信息检索中,查找语义相似的术语有多种应用。本练习中,您将练习从 en_core_web_md 模型的词汇表中,找到与单词 computer 语义最相近的术语。
computer 的词向量已提取并存为 word_vector。en_core_web_md 模型已加载为 nlp,NumPy 包已加载为 np。
您可以使用 nlp.vocab.vectors 对象的 .most_similar() 函数来查找语义最相近的术语。对该函数的输出使用 [0][0] 进行索引会返回这些相似术语的词 ID。可以使用 nlp.vocab.strings[<a given word>] 来获取给定单词的词 ID,同样也可以通过给定的词 ID 返回对应的单词。
本练习是课程的一部分
使用 spaCy 的自然语言处理
练习说明
- 从
en_core_web_md的词汇表中查找语义最相近的术语。 - 根据相似术语的词 ID 获取相似词的列表。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Find the most similar word to the word computer
most_similar_words = nlp.vocab.vectors.____(np.asarray([____]), n = 1)
# Find the list of similar words given the word IDs
words = [nlp.____.____[____] for w in most_similar_words[0][0]]
print(words)