我正在尝试创建一个聊天机器人,但我正处于显示错误的部分:'similarity_scores_list' 未定义。让我给你看一些代码

I am trying to create a chatbot and I am at a part in which an error is showing: 'similarity_scores_list' is not defined. Let me show u some code

    #Creating bot response
def bot_response(user_input):
  user_input = user_input.lower()
  sentence_list.append(user_input)
  bot_response = ''
  cm = CountVectorizer().fit_transform(sentence_list)
  similarity_scores = cosine_similarity(cm[-1], cm)
  similarity_scores_list = similarity_scores.flatten()
  index = index_sort(similarity_scores_list)



  user_input = ' Hello world '
  sentence_list.append(user_input)
  bot_response = ''
  cm = CountVectorizer().fit_transform(sentence_list)
  similarity_scores = cosine_similarity(cm[-1], cm)
  similarity_scores_list = similarity_scores.flatten()
  index = index_sort(similarity_scores_list)






similarity_scores_list 

现在的错误是:

NameError                                 Traceback (most recent call last)
<ipython-input-2-3a37cf5851ae> in <module>()
----> 1 similarity_scores_list

NameError: name 'similarity_scores_list' is not defined

当我 运行: similarity_scores_list

时出现此错误

我正在使用 google colaboratory。

请帮助我,因为我是初学者。我正在关注 YouTube 视频:https://www.youtube.com/watch?v=9KZwRBg4-P0&t=1066s

简答 - 你没有运行这些代码:

user_input = ' Hello world '
sentence_list.append(user_input)
bot_response = ''
cm = CountVectorizer().fit_transform(sentence_list)
similarity_scores = cosine_similarity(cm[-1], cm)
similarity_scores_list = similarity_scores.flatten()
index = index_sort(similarity_scores_list)

只需将这些代码复制到一个新单元格中,然后像视频一样单击开始按钮。那你运行

similarity_scores_list

长答案 - 他在视频中所做的基本上是复制外部函数中的代码,并将函数输入替换为手动输入(“Hello World”)。也许您 运行 函数代码,但这仅意味着您定义了函数。定义函数不会运行代码。所以你必须把代码复制到外面然后再运行。