開始使用免費開始

字彙中的相似詞

在資訊檢索中,尋找語意上相似的詞有許多應用。在這個練習中,你將練習從 en_core_web_md 模型的字彙中,找出與單字 computer 語意最相近的詞。

computer 的詞向量已經擷取並存成 word_vectoren_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)
編輯並執行程式碼