forked from GallagherAiden/chatbotExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
96 lines (89 loc) · 3.37 KB
/
script.py
File metadata and controls
96 lines (89 loc) · 3.37 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
95
96
import json
import random
import string
def updateFile(filename, data):
stringOfData = json.dumps(data)
with open(filename, "w") as myfile:
myfile.write(stringOfData)
def formatInput(userInput):
# make the input conform to the response file
userInput = userInput.lower()
# remove any punctuation
userInput = userInput.translate(None, string.punctuation)
return userInput
def addInputAsResponse(chatbotInput, userResponse):
if chatbotInput == "":
return
newInput = {
"word": userResponse,
"frequency": 0
}
with open('responses.json') as json_file:
data = json.load(json_file)
if chatbotInput in data:
wordCounter = 0
wordFound = 0
for thisData in data[chatbotInput]:
if userResponse == thisData['word']:
data[chatbotInput][wordCounter]['frequency'] += 1
wordFound = 1
wordCounter += 1
if wordFound == 0:
data[chatbotInput].append(newInput)
else:
data[chatbotInput] = []
data[chatbotInput].append(newInput)
updateFile("responses.json", data)
def getResponseWithNoData():
with open('responses.json') as json_file:
emptyResponses = []
otherResponses = []
data = json.load(json_file)
for thisMatch in data:
if len(data[thisMatch]) == 0:
emptyResponses.append(thisMatch)
else:
otherResponses.append(thisMatch)
if len(emptyResponses) > 0:
selector = random.randint(0, 1)
if selector == 0:
randRespNum = random.randint(0, len(emptyResponses)-1)
return emptyResponses[randRespNum]
else:
randRespNum = random.randint(0, len(otherResponses)-1)
return otherResponses[randRespNum]
else:
randRespNum = random.randint(0, len(otherResponses)-1)
return otherResponses[randRespNum]
def processResponse(thisInput):
with open('responses.json') as json_file:
data = json.load(json_file)
matchFound = 0
# pattern match the input so exact matches aren't needed
for thisMatch in data:
if thisMatch in thisInput:
if len(data[thisMatch]) > 0:
matchFound = 1
if len(data[thisMatch]) == 1:
return data[thisMatch][0]["word"]
else:
randRespNum = random.randint(0, len(data[thisMatch])-1)
return data[thisMatch][randRespNum]["word"]
if matchFound == 0:
# add the input into the responses JSON file with an empty array
data[thisInput] = []
updateFile("responses.json", data)
# ask a random unanswered question from 'responses'
return getResponseWithNoData()
def processInput(userInput, lastResponse):
formattedInput = formatInput(userInput)
addInputAsResponse(lastResponse, formattedInput)
response = processResponse(formattedInput)
print("Chatbot: " + response)
return response
print('Welcome to this chatbot, type something to begin...')
lastResponse = ''
userInput = ''
while userInput != 'bye':
userInput = raw_input("You: ")
lastResponse = processInput(userInput, lastResponse)