Search code examples
pythonchatbot

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 

Now the error is:

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

NameError: name 'similarity_scores_list' is not defined

This error comes when I run: similarity_scores_list

I am using google colaboratory.

Please help me as i am a beginner. I am following a youtube video: https://www.youtube.com/watch?v=9KZwRBg4-P0&t=1066s


Solution

  • Short Answer - you did not run these code:

    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)
    

    Just copy those code to a new cell and click the start button like the video. Then you run

    similarity_scores_list
    

    Long Answer - What he did in the video is basically copying the code in the function outside and replace the function input with a manual input ("Hello World"). Maybe you run the function code but that just means you defined the function. Defining the function will not run the code. So you have to copy the code outside and ran again.