-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChatbot_examples.py
More file actions
85 lines (78 loc) · 2.81 KB
/
Chatbot_examples.py
File metadata and controls
85 lines (78 loc) · 2.81 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
import intentparser as ip
import datetime
import random
import sys
__author__ = 'Nonthakon Jitchiranant'
allIntent = []
HelloIntent = ip.intentParser({
'description' : {
"type" : 'HelloIntent',
"args" : [],
"keyword" : [
(ip.REQUIRE, "hello_keyword"),
]},
'hello_keyword' : ['hello', 'hi'],
})
HelloIntent.teachWords(["Hello, How are you?", "Hi, Mr.John", "Hello, nice to meet you!"])
allIntent.append(HelloIntent)
ByeIntent = ip.intentParser({
'description' : {
"type" : 'ByeIntent',
"args" : [],
"keyword" : [
(ip.REQUIRE, "bye_keyword"),
]},
'bye_keyword' : ['bye'],
})
ByeIntent.teachWords(["Good bye.", "Bye, Josh.", "Good bye, Be safe."])
allIntent.append(ByeIntent)
TimeIntent = ip.intentParser({
'description' : {
"type" : 'TimeIntent',
"args" : [(ip.OPTIONAL, "scopes")],
"keyword" : [
(ip.REQUIRE, "time_keyword"),
(ip.OPTIONAL, "scopes")
]},
'time_keyword' : ['is', 'are', 'what', 'how', 'clock', 'how\'s'],
'scopes' : [
"day",
"time"
]
})
TimeIntent.teachWords(["What time is it?", "How's the clock", "What is this day"])
allIntent.append(TimeIntent)
while True:
text = input('User said : ').lower()
temp = []
for i in allIntent:
_temp = i.getResult(text)
try:
temp.append((_temp['confidence'], _temp['type']))
except Exception as e:
pass
try:
candidate = max(temp)
if candidate[1] == 'HelloIntent':
print(random.choice(['Chatbot said : Hello!',
'Chatbot said : How are you?',
'Chatbot said : What\'s up!']))
elif candidate[1] == 'ByeIntent':
print(random.choice(['Chatbot said : Good bye!',
'Chatbot said : Good Luck!',
'Chatbot said : See ya!']))
sys.exit(0)
elif candidate[1] == 'TimeIntent':
typeOfTime = TimeIntent.getResult(text)['args']
typeOfTime = [item for item in typeOfTime if item[0] == 'scopes'][0][1]
now = datetime.datetime.now()
if typeOfTime == ['day']:
print("Chatbot said : Today is {}/{}/{}".format(now.day, now.month, now.year))
else:
print("Chatbot said : It's {}:{}".format(now.hour, now.minute if now.minute > 9 else "0" + str(now.minute)))
del typeOfTime, now
else:
print('error')
del text, temp, _temp, candidate
except Exception as e:
print('Error')