-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_reply_completed.py
73 lines (61 loc) · 2.08 KB
/
generate_reply_completed.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
71
72
73
from linguistic import getPOS
from sentiment import getSentiment
import random
greetings = [ "hi", "hello", "hey", "yo", "greetings" ]
greetings_responses = [ "Hi there." , "Greetings human.", "Hello there.", "Hey." ]
# Returns JJ if sentence structure is You Are {word}+ JJ {word}+.
def findYouAreJJ(pos):
foundYou = False
foundYouAre = False
for e in pos:
if e[0].lower() == 'you':
foundYou = True
elif e[0].lower() == 'are' and foundYou:
foundYouAre = True
elif foundYou and not foundYouAre:
foundYou = False
elif foundYouAre and e[1] == 'JJ':
return e[0]
return False
# Generates a bot response from a user message
def generateReply(message):
pos = getPOS(message)
sentiment = getSentiment(message)
# If error occurred getting POS
if not pos:
return "I am not functioning at the moment. Perhaps check your API keys."
# If user greeted
if pos[0][0].lower() in greetings:
return random.choice(greetings_responses)
# If user said 'You are ... {adjective} ...'
youAreJJ = findYouAreJJ(pos)
if youAreJJ:
if sentiment >= 0.5:
return "Thank you, I know I'm "+youAreJJ+"."
else:
return "No! I'm not "+youAreJJ+"!"
# If user said 'I am ... {adjective} ...'
IAmJJ = findIAmJJ(pos)
if IAmJJ:
if sentiment >= 0.5:
return "I'm happy for you that you're "+IAmJJ+"."
else:
return "Don't be mean on yourself. I'm sure you're not really "+IAmJJ+"!"
if sentiment >= 0.5:
return "I'm happy to hear that!"
else:
return "I feel sad about that."
# Returns JJ if sentence structure is I Am {word}+ JJ {word}+.
def findIAmJJ(pos):
foundI = False
foundIAm = False
for e in pos:
if e[0].lower() == 'i':
foundI = True
elif e[0].lower() == 'am' and foundI:
foundIAm = True
elif foundI and not foundIAm:
foundI = False
elif foundIAm and e[1] == 'JJ':
return e[0]
return False