大量意图后列出索引超出范围错误

List index out of range error after a lot of intents

我目前正在开发聊天机器人。它适用于较少的意图,但在添加更多意图后,我得到索引错误。它适用于某些模式,但是当我添加更多模式时,事情变得很难看。 同样,它适用于某些模式。但是,有时(我不知道为什么)我会出错。我收到的错误消息是:

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

你能告诉我为什么会出现这个错误吗?这是代码:

    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 

我试过的是:

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

我在一个类似的程序中遇到过这个错误,我能够直接追踪问题是因为返回了一个空列表。我通过在我的代码中添加一个 try 和 except 来解决这个问题:

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

也许你可以做到这一点?或者,我尝试的另一件事是重新训练我的模型,但这可能是 time-consuming.