spaCy 词表中的词向量
词向量的目的是让计算机能够理解单词。在本练习中,您将练习为给定的单词列表提取词向量。
一个单词列表已保存为 words。en_core_web_md 模型已导入并以 nlp 提供。
en_core_web_md 模型的词表包含 20,000 个单词。若某个单词不在词表中,就无法提取其对应的词向量。在本练习中,为了简化,已确保所有给定单词都存在于该模型的词表中。
本练习是课程的一部分
使用 spaCy 的自然语言处理
练习说明
- 提取给定
words的所有 ID,并将其存入ids列表。 - 对于
ids中的每个 ID,将对应词向量的前 10 个元素存入word_vectors列表。 - 打印
word_vectors中第一个词向量的前 10 个元素。
交互式实操练习
通过完成这段示例代码来试试这个练习。
words = ["like", "love"]
# IDs of all the given words
ids = [nlp.____.____[w] for w in words]
# Store the first ten elements of the word vectors for each word
word_vectors = [nlp.____.____[i][:10] for i in ids]
# Print the first ten elements of the first word vector
print(____[0])