-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (51 loc) · 1.4 KB
/
main.py
File metadata and controls
61 lines (51 loc) · 1.4 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
import nltk
from nltk.chat.util import Chat, reflections
import wikipedia
pairs = [
['hi', ['Hello!', 'Hi there!', 'Hey!']],
['hello', ['Hi there!', 'Hello!', 'Hey!']],
[
'what is your name?',
['My name is Chatbot.', 'I am Chatbot.', 'You can call me Chatbot.']
],
[
'how are you?',
['I am fine, thank you.', 'I am doing great!', 'I am good.']
],
[
'what can you do?',
['I can answer questions based on my knowledge. Try asking me something.']
], ['quit', ['Bye!', 'Goodbye!', 'Have a nice day!']]
]
def extract_keyword(sentence):
words = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(words)
keyword = None
for word, pos in tagged:
if pos in ['NN', 'NNS']:
keyword = word
break
return keyword
def get_wikipedia_summary(keyword):
summary = None
try:
summary = wikipedia.summary(keyword, sentences=2)
except:
pass
return summary
def chatbot_response(input_text):
keyword = extract_keyword(input_text)
if keyword:
summary = get_wikipedia_summary(keyword)
if summary:
return summary
chatbot = Chat(pairs, reflections)
response = chatbot.respond(input_text)
return response
print("Welcome to Chatbot! Ask me something.")
while True:
user_input = input("You: ")
response = chatbot_response(user_input)
print("Chatbot:", response)
if response in ['Bye!', 'Goodbye!', 'Have a nice day!']:
break