Create new file
Browse files- chatbot.py +104 -0
chatbot.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from scipy import rand
|
2 |
+
from machine_learning import model, words, labels, ans_data, npl
|
3 |
+
import datetime
|
4 |
+
import random
|
5 |
+
import numpy
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
def bag_of_words(s, words):
|
10 |
+
bag = [0 for _ in range(len(words))]
|
11 |
+
s_words = npl(s.lower())
|
12 |
+
s_words = [word.lemma_ for word in s_words]
|
13 |
+
|
14 |
+
for se in s_words:
|
15 |
+
for i, w in enumerate(words):
|
16 |
+
if w == se:
|
17 |
+
bag[i] = 1
|
18 |
+
|
19 |
+
return numpy.array(bag)
|
20 |
+
|
21 |
+
unknown = ["I'm afraid I don't follow; could you perhaps give more detail?",
|
22 |
+
"I'm sorry, but I need you to elaborate a little bit more.", "I don't understand, can you try another question?"]
|
23 |
+
|
24 |
+
def chat():
|
25 |
+
print("Start talking with the bot (type quit to stop)!")
|
26 |
+
print("\n---------------------------------------------\nCellanet: Hello there!")
|
27 |
+
while True:
|
28 |
+
inp = input("You: ")
|
29 |
+
if inp.lower() == "quit":
|
30 |
+
break
|
31 |
+
|
32 |
+
results = model.predict([bag_of_words(inp, words)])
|
33 |
+
# print(f"Predict: {results}\n")
|
34 |
+
results_index = numpy.argmax(results)
|
35 |
+
|
36 |
+
max_result = numpy.max(results)
|
37 |
+
# print(f"max: {max_result}")
|
38 |
+
|
39 |
+
if max_result < 0.65:
|
40 |
+
tag = 'unknown'
|
41 |
+
else:
|
42 |
+
tag = labels[results_index]
|
43 |
+
|
44 |
+
responses = []
|
45 |
+
print(f"({tag})")
|
46 |
+
|
47 |
+
if tag in ans_data:
|
48 |
+
if tag == 'what time':
|
49 |
+
responses.append(f"Now is {datetime.datetime.now()}.")
|
50 |
+
else:
|
51 |
+
for x in ans_data[tag]:
|
52 |
+
for z in x:
|
53 |
+
responses.append(z)
|
54 |
+
else:
|
55 |
+
for x in unknown:
|
56 |
+
responses.append(x)
|
57 |
+
|
58 |
+
|
59 |
+
print('Cellanet:', random.choice(responses))
|
60 |
+
# return f"Cellanet: {random.choice(responses)}."
|
61 |
+
|
62 |
+
|
63 |
+
def onlineChat(inp):
|
64 |
+
print("\n---------------------------------------------\n")
|
65 |
+
while True:
|
66 |
+
# inp = input("You: ")
|
67 |
+
if inp.lower() == "quit":
|
68 |
+
break
|
69 |
+
|
70 |
+
results = model.predict([bag_of_words(inp, words)])
|
71 |
+
# print(f"Predict: {results}\n")
|
72 |
+
results_index = numpy.argmax(results)
|
73 |
+
|
74 |
+
max_result = numpy.max(results)
|
75 |
+
# print(f"max: {max_result}")
|
76 |
+
|
77 |
+
if max_result < 0.45:
|
78 |
+
tag = 'unknown'
|
79 |
+
else:
|
80 |
+
tag = labels[results_index]
|
81 |
+
|
82 |
+
responses = []
|
83 |
+
print(f"({tag})")
|
84 |
+
|
85 |
+
if tag in ans_data:
|
86 |
+
if tag == 'what time':
|
87 |
+
responses.append(f"Now is {datetime.datetime.now()}.")
|
88 |
+
else:
|
89 |
+
for x in ans_data[tag]:
|
90 |
+
for z in x:
|
91 |
+
responses.append(z)
|
92 |
+
else:
|
93 |
+
for x in unknown:
|
94 |
+
responses.append(f"*********** {x} ***********")
|
95 |
+
|
96 |
+
f = open('question and answer.txt', 'a')
|
97 |
+
f.write(f"- - {inp}\n - {random.choice(responses)}.\n\n\n")
|
98 |
+
f.close()
|
99 |
+
|
100 |
+
return f"Cellanet: {random.choice(responses)}"
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
# chat()
|