TypeError: only size-1 arrays (I am trying to print most similar words)

问题描述 投票:0回答:0

如何返回相似词?

我有这样的 Python 代码,这段代码应该简单地打印给定相似词的数组,例如“日本”

代码:

import spacy
import numpy as np

# Load the larger pre-trained English model
nlp = spacy.load("en_core_web_md")

# Get the vector for "japan"
word_vec = nlp("japan").vector

# Reshape the word_vec array to have a second dimension of size 1
word_vec = np.reshape(word_vec, (1, -1))

# Find the 3 most similar words to "japan"
most_similar = nlp.vocab.vectors.most_similar(word_vec, n=3)

# Extract the words from the most_similar list
similar_words = [nlp.vocab.strings[similar[0]] for similar in most_similar]

print(similar_words)

错误

TypeError: only size-1 arrays can be converted to Python scalars

我该如何解决?

python numpy nlp spacy
© www.soinside.com 2019 - 2024. All rights reserved.