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])