"IndexError: list index out of range" When creating an automated response bot

"IndexError: list index out of range" When creating an automated response bot

我正在创建一个聊天机器人,它使用 CSV 文件中的问题并使用 SKlearn 和 NLTK 检查相似性,但是如果输入相同的输入两次我会收到错误消息:

这是获取用户输入并向用户输出答案的主要代码:

import pandas as pd
data=pd.read_csv('FootballQA.csv')
question=data['Q'].tolist()
answer=data['A'].tolist()

lemmer = nltk.stem.WordNetLemmatizer()


#WordNet is a semantically-oriented dictionary of English included in NLTK.
def LemTokens(tokens):
    return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)

def LemNormalize(text):
    return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))



GREETING_INPUTS = ("hello", "hi", "greetings", "sup", "what's up","hey","how are you")
GREETING_RESPONSES = ["hi", "hey", "hi there", "hello", "I am glad! You are talking to me"]
def greeting(sentence):
 
    for word in sentence.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES)


GI = ("how are you")
GR = ["i'm fine","good,how can i help you!"]
def greet(sentence):
 
    for word in sentence.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES)

    def responses(user):
        response=''
        question.append(user)
        TfidfVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words='english')
        tfidf = TfidfVec.fit_transform(question)
        val = cosine_similarity(tfidf[-1], tfidf)
       
        id1=val.argsort()[0][-2]
        flat = val.flatten()
        flat.sort()
        req = flat[-2]
     
        if(req==0):
            robo_response=response+"I am sorry! I don't understand you"
            return robo_response 
        else:
            response = response+answer[id1]
            question.remove(user)
            return response       
    
    command=1
    while(command):
        v = input("Enter your value: ") 
        if(v=="exit"):
            command=0
        else:
            print(responses(str(v))) 

当程序运行时,它会询问用户的输入,但是如果输入相同的输入两次,就会出现问题,如果我输入“football”,它会首先正确显示我想要的输出,但第二次就会停止程序和我给出了这个错误:

Enter your value: scored
Alan shearer holds the goal record in the premier league.

Enter your value: football
I am sorry! I don't understand you

Enter your value: football
Traceback (most recent call last):

  File "C:\Users\Chris\Desktop\chatbot_simple\run.py", line 79, in <module>
    print(responses(str(v)))

  File "C:\Users\Chris\Desktop\chatbot_simple\run.py", line 68, in responses
    response = response+answer[id1]

IndexError: list index out of range

csv:

Q,A
Who has scored the most goals in the premier league?,Alan shearer holds the goal record in the premier league.
Who has the most appearences in the premier league?,Gareth Barry has the most appearences in premier league history.

我试过在每次输入后删除变量,但它仍然以某种方式记住它,有人有什么想法吗? 谢谢 克里斯

answer=data['A'].tolist()

然后是

id1=val.argsort()[0][-2]
response = response+answer[id1]

因此,如果 anwser 中没有 id1,您将得到超出范围的索引。所以在你的情况下 len(answer) >= id1true.