def testBuildQuery(self):
        """ Test function buildQueryFromFilters(), 
			which is implemented in database.py 
		"""

        date = '25-07-2018'
        rainfall = 90.0
        temperature = 27
        month = 6
        year = 2017

        conn = database.connectToDB()

        validQuery = ("SELECT * FROM climate WHERE"
                      " date = to_date(%s, 'DD-MM-YYYY')"
                      " AND temperature = (%s) AND rainfall = real '%s'"
                      " AND EXTRACT(MONTH FROM date) = (%s)"
                      " AND EXTRACT(YEAR FROM date) = (%s)")

        query, _ = database.buildQueryFromFilters(date, rainfall, temperature,
                                                  month, year)

        self.assertEqual(query.as_string(conn), validQuery)

        conn.close()
import config
import operator
import time
import sendemail
import weather
from textblob import TextBlob
from textblob.classifiers import NaiveBayesClassifier
import random
import sara
#from selenium import webdriver

currentMode = "default"
userInputQuestions = []
userInputKeywords = []
responseID = 0
conn = database.connectToDB()


# Function to get keywords and Questions separately from user input
def userInputQuestionKeyword(userInput):
    userInputArray = lang_processor.split_message(userInput)
    userInputWithOnlyQuestionAndKeywords = lang_processor.removeUnwantedWords(
        userInputArray)
    questions, keywords = lang_processor.seperateQuestionAndKeywords(
        userInputWithOnlyQuestionAndKeywords)
    return questions, keywords


def getReply(userInput):
    global conn
    userInputArray = lang_processor.split_message(userInput)
def currentWorkingMode(userInput):
    image_url = "none"
    questionPart = "none"
    global currentMode
    if (currentMode == "default"):
        if (userInput == "1"):
            currentMode = "chat"
            response = config.chatResponse
            log.writetofile(response)
        elif (userInput == "2"):
            currentMode = "training"
            response = config.trainingResponse1
            log.writetofile(response)
        elif (userInput == "3"):
            currentMode = "feedback"
            response = config.feedbackResponse1
            log.writetofile(response)
        elif (userInput == "4"):
            currentMode = "statistics"
            response = config.statisticsResponse
            log.writetofile(response)
        elif (userInput == "5"):
            currentMode = "weather"
            response = config.weatherResponse
            log.writetofile(response)
        elif (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        else:
            response = "Invalid Input. Please input the number to choose your mode"
            log.writetofile(response)
    elif (currentMode == "chat"):
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        else:
            response, image_url, questionPart = getReply(userInput)
            log.writetofile("Calling function botController getReply")

    elif (currentMode == "training"):
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        else:
            global userInputQuestions, userInputKeywords
            userInputQuestions, userInputKeywords = userInputQuestionKeyword(
                userInput)
            # print type(userInputQuestions)
            # print userInputQuestions
            if (len(userInputQuestions)):
                currentMode = "training2"
                response = config.trainingResponse2
    elif (currentMode == "training2"):
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        else:
            global userInputQuestions, userInputKeywords
            unicodeKeywords = []
            conn = database.connectToDB()
            unicodeKeywords = ','.join(userInputKeywords)
            if (database.checkRowExists(userInputQuestions[0], userInput,
                                        unicodeKeywords, conn)):
                log.writetofile("row already exist so not inserting")
                response = "Thanks for the information. " + config.moreTraining
                currentMode = "moretraining"
            else:
                database.storeNewResponse(userInput, unicodeKeywords,
                                          userInputQuestions[0], conn)
                currentMode = "moretraining"
                response = "Thanks for the information. " + config.moreTraining
    elif (currentMode == "moretraining"):
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        if (userInput.lower() == "yes".lower() or userInput.lower() == "y"):
            currentMode = "training"
            response = config.trainingResponse1
        elif (userInput.lower() == "no".lower() or userInput.lower() == "n"):
            response = defaultMode()
        else:
            response = "Please enter yes or no"

    elif (currentMode == "feedback"):
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        elif (userInput.isalpha()):
            response = "Please enter the number for Response ID"
        else:
            id = int((unicode.encode(userInput)))
            conn = database.connectToDB()
            if (database.checkIDExists(id, conn)):
                global responseID
                responseID = userInput
                currentMode = "feedback2"
                dblist = database.getPastResponse(id, conn)
                for var1 in dblist:
                    response = "Question is: " + var1[
                        'UserQuestion'] + "\n" + config.feedbackResponse2
            else:
                response = "Response ID does not exist"
    elif (currentMode == "feedback2"):
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        else:
            global responseID
            conn = database.connectToDB()
            id = int((unicode.encode(responseID)))
            database.updatePastResponse(id, userInput, conn)
            if (userInput.lower() == "yes".lower()
                    or userInput.lower() == "y"):
                response = config.feedbackResponseYes
                currentMode = "morefeedback"
            if (userInput.lower() == "no".lower() or userInput.lower() == "n"):
                currentMode = "wrongfeedback"
                response = config.feedbackResponseNo
    elif (currentMode == "morefeedback"):
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        if (userInput.lower() == "yes".lower() or userInput.lower() == "y"):
            currentMode = "feedback"
            response = config.feedbackResponse1
        elif (userInput.lower() == "no".lower() or userInput.lower() == "n"):
            response = defaultMode()
        else:
            response = "Please enter yes or no"
    elif (currentMode == "wrongfeedback"):
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        else:
            global responseID
            id = int((unicode.encode(responseID)))
            conn = database.connectToDB()
            dblist = database.getPastResponse(id, conn)

            for var1 in dblist:
                if (database.checkRowExists(var1['QuestionPart'], userInput,
                                            var1['MatchingKeywords'], conn)):
                    log.writetofile("row already exist so not inserting")

                else:
                    log.writetofile("new row inserted")
                    database.storeNewResponse(userInput,
                                              var1['MatchingKeywords'],
                                              var1['QuestionPart'], conn)

            response = config.feedbackResponseYes
            currentMode = "morefeedback"
    elif (currentMode == "weather"):
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        else:
            questions, keywords = userInputQuestionKeyword(userInput)
            response = weather.getWeather(keywords)
            #response = "weather mode initiated"

    else:
        if (userInput.lower() == "Exit".lower()):
            response = defaultMode()
        else:
            #driver = webdriver.Chrome()
            #driver.get('http://localhost:5601/goto/2d0d499fbddc57172334c009f2ab5614')
            #driver.save_screenshot('vivek.png')
            #driver.quit()
            response = "to view statistics visit, visit http://localhost:5601/goto/a7858fc1a3f85a385f6e926a1f776629"

    return response, image_url, questionPart
Beispiel #4
0
 def __init__(self):
     self.conn = database.connectToDB()