├── .idea
├── .gitignore
├── inspectionProfiles
│ └── profiles_settings.xml
├── json_chatbot.iml
├── misc.xml
├── modules.xml
└── vcs.xml
├── README.md
├── bot.json
├── main.py
└── random_responses.py
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/json_chatbot.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # JSON Chat Bot
2 | A simple Python chat bot project simplifying my previous chat bot by adding a JSON file.
3 |
4 | **NOTE:** All words that need to be recognised in the JSON file should be lowercased.
5 |
--------------------------------------------------------------------------------
/bot.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "response_type": "greeting",
4 | "user_input": ["hello", "hi", "hey"],
5 | "bot_response": "Hey there!",
6 | "required_words": []
7 | },
8 | {
9 | "response_type": "greeting",
10 | "user_input": ["see you", "goodbye", "bye"],
11 | "bot_response": "See you later!",
12 | "required_words": []
13 | },
14 | {
15 | "response_type": "greeting",
16 | "user_input": ["nice", "to", "meet", "you"],
17 | "bot_response": "The pleasure is all mine!",
18 | "required_words": ["nice", "meet", "you"]
19 | },
20 | {
21 | "response_type": "question",
22 | "user_input": ["how", "to", "learn", "code", "coding", "apps"],
23 | "bot_response": "Start by typing: 'How to learn coding' on Google.",
24 | "required_words": ["learn", "code"]
25 | },
26 | {
27 | "response_type": "question",
28 | "user_input": ["refund", "how", "can", "I", "get"],
29 | "bot_response": "We don't offer refunds for free education.",
30 | "required_words": ["refund", "i"]
31 | },
32 | {
33 | "response_type": "question",
34 | "user_input": ["how", "are", "you"],
35 | "bot_response": "I'm great! Thanks for asking.",
36 | "required_words": ["how", "are", "you"]
37 | }
38 | ]
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | import json
2 | import re
3 | import random_responses
4 |
5 |
6 | # Load JSON data
7 | def load_json(file):
8 | with open(file) as bot_responses:
9 | print(f"Loaded '{file}' successfully!")
10 | return json.load(bot_responses)
11 |
12 |
13 | # Store JSON data
14 | response_data = load_json("bot.json")
15 |
16 |
17 | def get_response(input_string):
18 | split_message = re.split(r'\s+|[,;?!.-]\s*', input_string.lower())
19 | score_list = []
20 |
21 | # Check all the responses
22 | for response in response_data:
23 | response_score = 0
24 | required_score = 0
25 | required_words = response["required_words"]
26 |
27 | # Check if there are any required words
28 | if required_words:
29 | for word in split_message:
30 | if word in required_words:
31 | required_score += 1
32 |
33 | # Amount of required words should match the required score
34 | if required_score == len(required_words):
35 | # print(required_score == len(required_words))
36 | # Check each word the user has typed
37 | for word in split_message:
38 | # If the word is in the response, add to the score
39 | if word in response["user_input"]:
40 | response_score += 1
41 |
42 | # Add score to list
43 | score_list.append(response_score)
44 | # Debugging: Find the best phrase
45 | # print(response_score, response["user_input"])
46 |
47 | # Find the best response and return it if they're not all 0
48 | best_response = max(score_list)
49 | response_index = score_list.index(best_response)
50 |
51 | # Check if input is empty
52 | if input_string == "":
53 | return "Please type something so we can chat :("
54 |
55 | # If there is no good response, return a random one.
56 | if best_response != 0:
57 | return response_data[response_index]["bot_response"]
58 |
59 | return random_responses.random_string()
60 |
61 |
62 | while True:
63 | user_input = input("You: ")
64 | print("Bot:", get_response(user_input))
65 |
--------------------------------------------------------------------------------
/random_responses.py:
--------------------------------------------------------------------------------
1 | import random
2 |
3 |
4 | def random_string():
5 | random_list = [
6 | "Please try writing something more descriptive.",
7 | "Oh! It appears you wrote something I don't understand yet",
8 | "Do you mind trying to rephrase that?",
9 | "I'm terribly sorry, I didn't quite catch that.",
10 | "I can't answer that yet, please try asking something else."
11 | ]
12 |
13 | list_count = len(random_list)
14 | random_item = random.randrange(list_count)
15 |
16 | return random_list[random_item]
17 |
--------------------------------------------------------------------------------