简体   繁体   中英

Create a AI chatbot that learns how to chat without learning how to answer every question given by the user

My code is only this and it would only recognize specific questions... else it asks for the answer... but I want it to be able to learn and try to self-answer my new question.

example:

input("hello")

output("Hi, how are you?")

without teaching the ai chatbot about what to respond to "hi", I would want it to be able to respond back too

Like:

 input("Hi")

 output("Hello, how are you?")

It is very difficult for me and that is why I am requesting for an answer for it

My current code:

said = []
output = []
text = ""

global n

while True:
    text = input("Say something: ")
    if not(text == ""):
        if not(text == "print(input, output)"):
            input_contain = text.lower() in said

            if input_contain:
                n = 0
                found = False
                while not found:
                    if said[n] == str(text.lower()):
                        print(output[n])
                        found = True
                    else:
                        n = n + 1
            else:

                output_add = input("What should I respond to that? ")
                if not output_add == "":
                    said.append(text.lower())
                    output.append(output_add)
                else:
                    print("Error in output")
        else:

            for qn in said:
                if not qn == int(len(said)) - 1:
                    print(str(qn), end=", ")
                else:
                    print(qn)

            for out in output:
                if not out == int(len(output)) - 1:
                    print(str(out), end=", ")
                else:
                    print(out)

    else:
        print("Error in input") 

If you have a prebuilt set of responses, such as the "Hi, how are you", you could build a set of appropiate prompt questions for such responses, and compare the similarity (using eg spacy nlp ) between the user input and your known prompt questions.

You could take the highest similar prompt question in order to identify the most similar question that has previoulsy been mapped to a response - then answer that questions answer, as the response.


For example, "Hello" and "Hi" have much higher similarity (0.9 or so) than "Hello" and "What is a scary animal?" do (near 0)..

So if you had

"What is a scary animal?" ==> "Tiger",

"Hi" => "Hello, how are you"?

"Hello" would map 0.9 to "Hi" and near 0 to "What is a scary animal", so pick "Hello"s answer, which is "Hello, how are you?"

"What is a fierce animal" would map closer to "What is a scary animal", so pick Tiger for that..


To allow your program to improve, you would need to save the mappings somewhere and add more.. this is roughly how GOFAI chat bots work

Machine Learning is a statistical mechanism. Here is a link somewhat related: "Estimate" the amount of training needed in advance

Personally, that is not "intelligence". Imagine having to retrain the bot on the live, when the user says something to be reused in future questions.

There is another approach to replying questions in chatbot fashion, that is called Natural Language Processing (check stackoverflow tag NLP). Specially Logic/Semantic processing can break down the user input into its constituents and from there develop the appropriate response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM