首页 > 解决方案 > 字符串的余弦相似度作为运算符的输入 - FLOAT ERROR

问题描述

我想创建一种比较器,它在输入中给出一个句子,从一个运算符,程序通过余弦相似度评估,如果数据集中有类似的句子。到目前为止我所做的是:

text = pd.read_excel("Database1.xlsx", usecols='C'))
new_input = input('Insert the sentence: ')

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer(analyzer='word', stop_words='english')
tfidf_matrix = tfidf_vectorizer.fit_transform(text)
print(tfidf_matrix.shape)

from sklearn.metrics.pairwise import cosine_similarity
a = cosine_similarity(new_input, tfidf_matrix)
print(a)

但我收到一条错误消息:

ValueError: could not convert string to float: 'hello world'

我尝试通过以下方式解决它:

new_input = float(input('Insert the sentence: '))

如果我正确理解,问题是由于存在空格而无法将输入字符串“hello world”转换为浮点数,但我需要在输入中存在空格。

我该如何解决这种情况?非常感谢您的帮助

标签: pythonstringcompiler-errors

解决方案


推荐阅读