Ejemplo n.º 1
0
def session(connection):
    i = 0  # counter for how many times we have been round the loop
    #    startMessage = "Starting Bot...\n"

    # Get Config
    conf = utils.get_config()
    DBHOST = conf["MySQL"]["server"]
    DBUSER = conf["MySQL"]["dbuser"]
    DBNAME = conf["MySQL"]["dbname"]

    logging.info("Starting Bot session-thread...")

    # initialize the connection to the database
    logging.info("   session-thread connecting to database...")
    DBconnection = utils.db_connection(DBHOST, DBUSER, DBNAME)
    DBcursor = DBconnection.cursor()
    DBconnectionID = utils.db_connectionID(DBcursor)
    logging.info("   ...connected")

    botSentence = 'Hello!'
    weight = 0

    trainMe = False
    checkStore = False

    #    startMessage = startMessage + ("...started\n")

    def receive(connection):

        logging.debug("   receive(connection): PID {}, thread {} \n".format(
            pid, thread))
        received = connection.recv(1024)
        if not received:
            #logging.info("Closing connection {}".format(thread))
            return False
        else:
            #logging.debug("    Received {}, echoing".format(received))
            return received

    while True:
        pid = os.getpid()
        thread = threading.current_thread()

        # pass received message to chatbot
        received = receive(connection)
        humanSentence = received.decode().strip()

        if humanSentence == '' or humanSentence.strip(punctuation).lower(
        ) == 'quit' or humanSentence.strip(punctuation).lower() == 'exit':
            break

        # Chatbot processing
        botSentence, weight, trainMe, checkStore = chatbot.chat_flow(
            DBcursor, humanSentence, weight)
        logging.debug(
            "   Received botSentence {} from chatbot.chat_flow".format(
                botSentence))

        if trainMe:
            logging.debug("   trainMe is True")
            send = "Please train me - enter a response for me to learn (or \"skip\" to skip)' ".encode(
            )
            connection.send(send)
            previousSentence = humanSentence
            received = receive(connection)
            humanSentence = received.decode().strip()
            logging.debug("   trainMe received {}".format(humanSentence))

            if humanSentence != "skip":
                chatbot.train_me(previousSentence, humanSentence, DBcursor)
                botSentence = "Thanks I've noted that"
                #connection.send(send)
            else:
                botSentence = "OK, moving on..."
                #connection.send(send)
                trainMe = False

        if checkStore:
            logging.debug("CheckStore is True")
            send = 'Shall I store that as a fact for future reference?  ("yes" to store)'.encode(
            )
            connection.send(send)
            previousSentence = humanSentence
            received = receive(connection)
            humanSentence = received.decode().strip()
            logging.debug("   checkStore received {}".format(humanSentence))

            if regexpYes.search(humanSentence.lower()):
                #Store previous Sentence
                logging.debug("   Storing...")
                chatbot.store_statement(previousSentence, DBcursor)
                logging.debug("   Statement Stored.")
                botSentence = random.choice(chatbot.STATEMENT_STORED)
            else:
                botSentence = "OK, moving on..."
                checkStore = False

        DBconnection.commit()
        logging.debug("   sending botSentence back: {}".format(botSentence))
        send = botSentence.encode()

        #        if i == 0:
        #            send = startMessage.encode() + send
        connection.send(send)


#        i = i + 1
    logging.info("   Closing Session")
Ejemplo n.º 2
0
if __name__ == "__main__":

    conf = utils.get_config()
    regexpYes = re.compile(r'yes')

    DBHOST = conf["MySQL"]["server"]
    DBUSER = conf["MySQL"]["dbuser"]
    DBNAME = conf["MySQL"]["dbname"]

    print("Starting Bot...")
    # initialize the connection to the database
    print("Connecting to database...")
    connection = utils.db_connection(DBHOST, DBUSER, DBNAME)
    cursor = connection.cursor()
    connectionID = utils.db_connectionID(cursor)
    print("...connected")

    trainMe = False
    checkStore = False

    botSentence = 'Hello!'
    while True:

        # Output bot's message
        if DEBUG_WEIGHT:
            print('Bot> ' + botSentence + ' DEBUG_WEIGHT:' +
                  str(round(weight, 5)))
        else:
            print('Bot> ' + botSentence)
Ejemplo n.º 3
0
def session(connection):
    i = 0  # counter for how many times we have been round the loop
    startMessage = "Starting Bot...\n"

    # initialize the connection to the database
    conf = utils.get_config()

    DBHOST = conf["MySQL"]["server"]
    DBUSER = conf["MySQL"]["dbuser"]
    DBNAME = conf["MySQL"]["dbname"]

    print("Starting Bot...")
    # initialize the connection to the database
    print("Connecting to database...")
    DBconnection = utils.db_connection(DBHOST, DBUSER, DBNAME)
    DBcursor = DBconnection.cursor()
    DBconnectionID = utils.db_connectionID(DBcursor)
    print("...connected")

    botSentence = 'Hello!'
    weight = 0

    trainMe = False
    botSentence = 'Hello!'
    startMessage = startMessage + ("...started\n")

    def receive(connection):

        if DEBUG_SERVER: print("PID {}, thread {} \n".format(pid, thread))
        received = connection.recv(1024)
        if not received:
            print("Closing connection {}".format(thread))
            return False
        else:
            if DEBUG_SERVER: print("Received {}, echoing".format(received))
            return received

    while True:
        pid = os.getpid()
        thread = threading.current_thread()

        # pass received message to chatbot
        received = receive(connection)
        humanSentence = received.decode().strip()

        if humanSentence == '' or humanSentence.strip(punctuation).lower(
        ) == 'quit' or humanSentence.strip(punctuation).lower() == 'exit':
            break

        # Chatbot processing
        botSentence, weight, trainMe = chatbot.chat_flow(
            DBcursor, humanSentence, weight)

        if trainMe:
            send = "Bot> Please train me - enter a response for me to learn (or \"skip\" to skip)' ".encode(
            )
            connection.send(send)
            previousSentence = humanSentence
            received = receive(connection)
            humanSentence = received.decode().strip()

            if humanSentence != "skip":
                chatbot.train_me(previousSentence, humanSentence, DBcursor)
                botSentence = "Bot> Thanks I've noted that"
                #connection.send(send)
            else:
                botSentence = "Bot> OK, moving on..."
                #connection.send(send)
                trainMe = False

        DBconnection.commit()
        send = botSentence.encode()

        if i == 0:
            send = startMessage.encode() + send

        connection.send(send)

        i = i + 1