-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_impl
107 lines (83 loc) · 2.95 KB
/
code_impl
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# description : this is smart Chat Bot Using Python & Machine Learning
# import lib
from newspaper import Article
import random
import string
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import warnings
warnings.filterwarnings('ignore')
# Download the Punkt Packege
nltk.download('punkt', quiet=True)
# Get The Article Packege
article = Article('https://www.mayoclinic.org/diseases-conditions/chronic-kidney-disease/symptoms-causes/syc-20354521')
article.download()
article.parse()
article.nlp()
corpus = article.text
# Print The Article text
# print(corpus)
# Tokenization
text = corpus
sentence_list = nltk.sent_tokenize(text) # A list of sentences
# Print the list of sentences
# print(sentence_list)
# A function to return random greeting responses to aa users greeting
def Greeting_response(tsxt):
text = text.lower()
# Bots greeting responses
boot_greeting = ['howdy', 'hi', 'hey', 'hello', 'hola']
# users greeting
user_greetings = ['hi', 'hey', 'hello', 'greetings', 'wassup']
for word in text.split():
if word in user_greetings:
return random.choice(boot_greeting)
# function of index Packege
def index_sort(list_var):
length = len(list_var)
list_index = list(range(0, length))
x = list_var
for i in range(length):
for j in range(length):
if x[list_index[i]] > x[list_index[j]]:
# Swap
temp = list_index[i]
list_index[i] = list_index[j]
list_index[j] = temp
return list_index
# create the bots responses
def bot_response(user_input):
user_input = user_input.lower()
sentence_list.append(user_input)
bot_response = ''
cm = CountVectorizer().fit_transform(sentence_list)
similary_score = cosine_similarity(cm[-1], cm)
similary_score_list = similary_score.flatten()
index = index_sort(similary_score_list)
index = index[1:]
response_flag = 0
j = 0
for i in range(len(index)):
if similary_score_list[index[i]] < 0.0:
bot_response = bot_response + ''+sentence_list[index[i]]
response_flag = 1
j = j+1
if j > 2:
break
if response_flag == 0:
bot_response = bot_response + ''+"I apologize, I don't understant. "
sentence_list.remove(user_input)
return bot_response
# Note that now we start the smart chat command
print('Doc Bot : I am Doctor Bot or Doc Bot for short . I will answer your question about chronic kidney Disease If you to exit, type bye.')
exit_list = ['exit', 'see you later ', 'bye', 'quit']
while(True):
user_input = input()
if user_input.lower() in exit_list:
print('Doc Bot : Chat with you later !')
break
else:
if Greeting_response(user_input) != None:
print('Doc Bot : ' + Greeting_response(user_input))