-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversation.py
70 lines (54 loc) · 2.54 KB
/
conversation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from textblob import TextBlob
from mathBot import isMath
import weather
import shibBot
# import dbQueries
# import hangman
import dbQueries
from reminder import Reminder
import asyncio
# import BasicCommands
def defaultChat(stringInp, rs, userID):
"""Determines the context of the user input as string and returns a response for the bot to output. Updates database."""
#get the current data of the user from RiveScript and update the database
dbData = (rs.reply("localuser", "get database data")).split(" ")
dbQueries.updateDB(userID, dbData)
#Creates a TextBlob of the input using the TextBlob API
blob = TextBlob(stringInp)
polarity = blob.sentiment.polarity #Determines the sentiment of the input i.e. nice or nasty
#Passes the polarity to rivescript to update current 'happiness', floats cannot be passed
#to rivescript so pre and post decimal valyes are passed individually
pre = int(polarity)
post = abs(polarity - pre)
#Determine if the value is positive or negative
if polarity >= 0:
negative = False
elif polarity < 0:
negative = True
polarityToPass = str(negative) + " " + str(post)[2:]
newPolarity = rs.reply("localuser", "setting polarity " + polarityToPass)
#Update the database with the new values
dbQueries.updatePol(userID, newPolarity)
dbData = (rs.reply("localuser", "get database data")).split(" ")
dbQueries.updateDB(userID, dbData)
return(rs.reply("localuser", stringInp))
def keywordToModule(moduleName, stringInp, rs, userID, client, message, function = None):
"""Depending on what rivescript returned, the appropriate module is ran"""
rmndr = Reminder() #Reminder and math modules checked differently so objects are instantiated first
math = isMath(stringInp)
if math.mathQ: #Check the value of the attrabut in the math object wwhich tell whethere it is a math question or not
return math.currentEval
elif(moduleName == "google"):
pass
elif(moduleName == "dog"):
return shibBot.checkDog(stringInp)
elif(moduleName == "weather"):
return weather.weatherInit(stringInp)
elif(moduleName == "game"):
return hangman.init()
elif(rmndr.check(stringInp)): #Boolean is returned, if True then it is a reminder else it isn't
rmndr.listener += function
rmndr.setReminder('This is my message', 2)
return(rmndr.getAnswer(stringInp))
else:
return defaultChat(stringInp, rs, userID)