-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_chatbot_nltk.py
More file actions
66 lines (59 loc) · 2 KB
/
simple_chatbot_nltk.py
File metadata and controls
66 lines (59 loc) · 2 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
import nltk
from nltk.corpus import stopwords
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
# Defining the keywords
users_keyword = {
("hii", "hello", "hey"): "greeting",
("bye", "goodbye"): "goodbye",
("thank", "thanks"): "thanks",
("good","very good"):"news",
}
# Defining the responses
responses = {
"greeting": "Hello! How can I assist you today?",
"goodbye": "Goodbye! Have a great day!",
"thanks": "You're welcome!",
"news":"how are you today?",
"default": "I'm sorry, I didn't understand that. Can you please rephrase?"
}
# Defining the Tokenization
def tokenize(text):
return word_tokenize(text.lower())
# Removing the stopwords
def remove_stopwords(tokens):
stop_words = set(stopwords.words('english'))
return [token for token in tokens if token not in stop_words]
# Lemmatization
def lemmatize(tokens):
lemmatizer = WordNetLemmatizer()
return [lemmatizer.lemmatize(token, get_wordnet_pos(token)) for token in tokens]
# Get WordNet POS tag
def get_wordnet_pos(token):
tag = nltk.pos_tag([token])[0][1][0].upper()
tag_dict = {"J": wordnet.ADJ, "N": wordnet.NOUN, "V": wordnet.VERB, "R": wordnet.ADV}
return tag_dict.get(tag, wordnet.NOUN)
# Match keywords and get response
def get_response(tokens):
for keywords, response_key in users_keyword.items():
for keyword in keywords:
if keyword in tokens:
return responses[response_key]
return responses["default"]
# Main function
def chat():
print("Chatbot: " + responses["greeting"])
while True:
user_input = input("User: ").strip()
if user_input.lower() == 'exit':
print("Chatbot: " + responses["goodbye"])
break
tokens = tokenize(user_input)
tokens = remove_stopwords(tokens)
tokens = lemmatize(tokens)
response = get_response(tokens)
print("Chatbot: " + response)
# Run the chatbot
if __name__ == "__main__":
chat()