Repository navigation
Expand file tree
/
Copy pathChatBot.py
More file actions
96 lines (87 loc) · 3.06 KB
/
Copy pathChatBot.py
File metadata and controls
96 lines (87 loc) · 3.06 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
86
87
88
89
90
91
92
93
94
#Title: ChatBot Dev Version
#Author: Dwight Reid
#Copyright 2018
#from basic import basic_actions
import random, datetime, calendar, os
import xml.etree.ElementTree as ET
class CB:
def __init__(self):
self.allCmds = []
self.allActns = []
self.initialiseSys()
################################################################################
def getCmds(self):
v = []
actns = []
for app in os.listdir('.'):
if os.path.isdir(app):
tree = ET.parse(app + '/commands.xml')
cmds = tree.getroot()
vals = []
#v = []
#actns = []
cnt = 1
for cmd in cmds:
for child in cmd:
#print child.tag, child.text
if child.tag == "var":
vals.append(child.text)
if child.tag == "actn":
actns.append(child.text)
v.append(vals)
vals = []
return v, actns
################################################################################
def redHammingMod(self,s1, s2):
s1 = s1 + ' ' * (len(s2) - len(s1))
s2 = s2 + ' ' * (len(s1) - len(s2))
distance = sum(i == j for i, j in zip(s1, s2))
norm_distance = distance / float(len(s1))
return norm_distance
################################################################################
def evaluateCommand(self,uc):
allCmds = self.allCmds
allActns = self.allActns
maxX = 0
x = 0
cmdSel = 0
for cnt in range(len(allCmds)):
for cmd in allCmds[cnt]:
x = self.redHammingMod(cmd, uc)
if maxX < x:
maxX = x
cmdSel = cnt
## print "maxX is:", maxX
## print "Cmnd is:", cmdSel
## print "Action ID is: ", allActns[cmdSel]
return self.doAction(allActns[cmdSel])
################################################################################
def doAction(self,actnID):
y = actnID.split(".")
x = __import__(y[0] + "." + y[1])
z = y[1] + "." + y[2]
t = getattr(x, y[1])
u = getattr(t, y[2])
res = u()
del(x)
return res
################################################################################
def initialiseSys(self):
#global allCmds
#global allActns
self.allCmds, self.allActns = self.getCmds()
#print self.allCmds
#print self.allActns
#x = self.allActns[0]
#x
print "System Initialised"
#timeCmds = allCmds[0]
#dayCmds = allCmds[1]
##x = CB()
##x.initialiseSys()
##x.evaluateCommand("time")
##print "hello, welcome to my world. I am super chatbot :) "
##promptMsg = ["Enter your request ","How can I help ", "what do you want to know "]
##dispMsg = promptMsg[random.randint(1,3)-1]
##user_cmd = raw_input(dispMsg)
##evaluateCommand(user_cmd)