mardi 16 janvier 2018

python if else statement throwing error, duckduckgo error

I am working on connecting ALICE chatbot wit duckduckgo API, here is my code.

its all working, just the control statements looks bad, which is throwing me an error.

I am building a chatbot using ALICE (AIML). It is working good with .brn or static xml files, I want to provide it the connections so that it can connect to the web and search for the query and display the results.

Below is my code and error log.

from flask import Flask, render_template, request, jsonify
import aiml
import os

app = Flask(__name__)

@app.route("/")
def hello():
    return render_template('chat.html')

@app.route("/ask", methods=['POST'])
def ask():
    message = str(request.form['messageText'])

    kernel = aiml.Kernel()

    if os.path.isfile("bot_brain.brn"):
        kernel.bootstrap(brainFile = "bot_brain.brn")
    else:
        kernel.bootstrap(learnFiles = os.path.abspath("aiml/std-startup.xml"), commands = "load aiml b")
        kernel.saveBrain("bot_brain.brn")

    # kernel now ready for use
    while True:
        if message == "quit":
            exit()
        elif message == "save":
            kernel.saveBrain("bot_brain.brn")

        elif "what is" in message:
            answer = web_search(message)
            print answer    

        else :
            print "the following query is not found, please try with other messages"



def web_search(query):
    answer = duckduckgo.query(query)
    answer = answer.abstract.text
    #answer=answer.split('(')[0]

    if("..." in answer):
        answer = answer + "wait! Are you testing me?!?!"
    if('http' in answer):
        answer = "TBH i don't know...but i can give you a link....go find it there :)\n"+answer
    else:
        answer = answer
    return answer


client="Jing"    
bot.respond("My name is "+client)
answer = "Hello, Myself Intelligent Alice"


if __name__ == "__main__":
    app.run(debug=True)

and, this is my error log;

  File "main.py", line 30
    elif "what is" in message:
       ^
SyntaxError: invalid syntax

Aucun commentaire:

Enregistrer un commentaire