-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
43 lines (34 loc) · 1.07 KB
/
chatbot.py
File metadata and controls
43 lines (34 loc) · 1.07 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
import json
from string import punctuation
import random
# Dict file with (word: topic) translation
DICT_FILE = 'translate.json'
# Dict file with (topic: [questions]) translation
QUESTION_FILE = 'questions.json'
# Load files
with open(DICT_FILE, 'r') as in_file:
trans_dict = json.load(in_file)
with open(QUESTION_FILE,'r') as in_file:
question_dict = json.load(in_file)
def main():
try:
while True:
user_message = input()
bot_answear = chat(user_message)
print(bot_answear)
except KeyboardInterrupt:
print("Bye...")
def chat(user_message: str):
"""Find appropriate answear for user message."""
# Delete punctuation and format
s = user_message.translate(str.maketrans('', '', punctuation))
s = s.split()
#print(s)
# Mix array with words
for word in random.sample(s, len(s)):
if word in trans_dict:
topic = trans_dict[word]
return random.choice(question_dict[topic])
return random.choice(question_dict["$default"])
if __name__ == '__main__':
main()