Search code examples
pythondeep-learningnltkchatbot

List index out of range error after a lot of intents


I am currently working on a chatbot. It works with fewer intents, but after adding more of them, I get Index error. It works on some patterns, but when I add more, things get ugly. Likewise, it works on some patterns. However, sometimes (I do not know why) I get an error. The error message which I get is:

Traceback (most recent call last):
  File "C:\Users\emrey\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/emrey/OneDrive/Belgeler/AIChatBot/app.py", line 59, in _on_enter_pressed
    self._insert_message(msg, "You")
  File "C:/Users/emrey/OneDrive/Belgeler/AIChatBot/app.py", line 71, in _insert_message
    msg2 = f"{bot_name}: {get_response(predict_class(msg.lower()),intents)}\n\n"
  File "C:\Users\emrey\OneDrive\Belgeler\AIChatBot\WolE.py", line 47, in predict_class
    if results[0][1] < 0.7:
IndexError: list index out of range

Could you tell me why I get this error? Here is the code:

    import random
    import json
    import pickle
    import numpy as np
    import nltk
    from nltk.stem import WordNetLemmatizer
    import csv
    import codecs
    import urllib.request
    
    from tensorflow.keras.models import load_model
    
    base_url = "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/W%C3%BCrzburg/today?unitGroup=metric&key=Y8925G8VV2MZK49WQ38Z2Q3RC"
    
    
    lemmatizer = WordNetLemmatizer() 
    intents = json.loads(open('intentsWolE.json',encoding='utf-8').read())
    
    words = pickle.load(open('words.pk1','rb'))
    classes = pickle.load(open('classses.pk1','rb'))
    model = load_model('chatbot_model.model')
    
    bot_name = "Genesis"
    
    def clean_up_sentence(sentence):
        sentence_words = nltk.word_tokenize(sentence)
        sentence_words = [lemmatizer.lemmatize(word) for word in sentence_words]
        return sentence_words
    
    def bag_of_words(sentence):
        sentence_words = clean_up_sentence(sentence)
        bag = [0] * len(words)
        for w in sentence_words:
            for i, word in enumerate(words):
                if word == w:
                    bag[i] = 1
        return np.array(bag)
    
    def predict_class(sentence):
        global results
        bow = bag_of_words(sentence)
        res = model.predict(np.array([bow]))[0]
        ERROR_THRESHOLD = 0.25
        results = [[i,r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
        results.sort(key=lambda x: x[1], reverse=True)
        if results[0][1] < 0.7:
            return [{'intent': 'not_understand', 'probability': '0.9999999'}]
        return_list = []
        for r in results:
            return_list.append({'intent': classes[r[0]], 'probability': str(r[1])})
            print(return_list)
        return return_list
    
    def weather_report():
        CSVBytes = urllib.request.urlopen(base_url)
        CSVText = csv.reader(codecs.iterdecode(CSVBytes, 'utf-8'))
        for Row in CSVText:
            FirstRow = Row
        return FirstRow[9] + " and " + FirstRow[14]
    
    def get_response(intents_list,intents_json):
        tag = intents_list[0]['intent']
        list_of_intents = intents_json['intents']
        for i in list_of_intents:
            if i['tag'] == tag:
                result = random.choice(i['responses'])
                if result == "weather_report":
                   result = weather_report()
                break
        return result 

The thing I tried is:

START
I am hugnry
2021-07-14 15:17:08.242833: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
What do you mean?
I am hungry
[{'intent': 'hunger', 'probability': '0.9998872'}]
What do you want to eat?
Francnian restrant
Traceback (most recent call last):
  File "C:/Users/emrey/OneDrive/Belgeler/AIChatBot/WolE.py", line 75, in <module>
    ints = predict_class(message.lower())
  File "C:/Users/emrey/OneDrive/Belgeler/AIChatBot/WolE.py", line 46, in predict_class
    if results[0][1] < 0.7:
IndexError: list index out of range

Process finished with exit code 1

Solution

  • I've been having this error in a similar program, and I was able to directly trace the issue to having an empty list being returned. I fixed this by adding a try and except to my code:

    global result
    try:
        tag = intents_list[0]['intent']
        list_of_intents = intents_json['intents']
        for i in list_of_intents:
            if i['tag'] == tag:
                result = random.choice(i['responses'])
                break
    except:
        result = "I cannot understand this statement. Perhaps rephrase it or type it differently?"
    return result
    

    Perhaps you could do this? Or, another thing I tried was retraining my model, but that may be time-consuming.