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 package
nltk.download('punkt', quiet=True)

article = Article('https://www.mayoclinic.org/diseases-conditions/depression/symptoms-causes/syc-20356007')
article.download()
article.parse()
article.nlp()
corpus = article.text

#print(corpus)

text = corpus
#Creating a list of sentences
sentence_list = nltk.sent_tokenize(text)
#print(sentence_list)

#Function to return a random greeting response to a users greeting

def greeting_response(text):
    text = text.lower()
    #bots greeting response
    bot_greetings = ['howdy', 'hi', 'holla', 'hey you', 'hi there']
    #Users greeting
    user_greetings = ['hi', 'hey', 'hello', 'hola', 'greetings', 'wassup']

    for word in text.split():
        if word in user_greetings:
            return random.choice(bot_greetings)

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

def bot_response(user_input):
    user_input = user_input.lower()
    sentence_list.append(user_input)
    bot_response = ''
    cm = CountVectorizer().fit_transform(sentence_list) #cm = count matrix
    similarity_scores = cosine_similarity(cm[-1], cm)
    similarity_scores_list = similarity_scores.flatten()
    index = index_sort(similarity_scores_list)
    index = index[1:]
    response_flag = 0

    d = 0
    for i in range(len(index)):
        if similarity_scores_list[index[i]] > 0.0:
            bot_response = bot_response+' '+sentence_list[index[i]]
            response_flag = 1
            d = d+1
        if d > 2:
            break

    if response_flag == 0:
        bot_response = bot_response+ ' '+'I apologize, I dont understand'

    sentence_list.remove(user_input)
    return bot_response

Intro = 'Doc Bot: I am Doctor Bot or Doc Bot for short. I will answer your questions about depression. To end the conversation, just type bye'
#Starting the chat
print(Intro)

exit_list = ['exit', 'see you later', 'bye', 'quit', 'break']

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))
        else:
            print('Doc Bot: ' + bot_response(user_input))
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 package
nltk.download('punkt', quiet=True)

article = Article('https://www.mayoclinic.org/diseases-conditions/depression/symptoms-causes/syc-20356007')
article.download()
article.parse()
article.nlp()
corpus = article.text

#print(corpus)

text = corpus
#Creating a list of sentences
sentence_list = nltk.sent_tokenize(text)
#print(sentence_list)

#Function to return a random greeting response to a users greeting

def greeting_response(text):
    text = text.lower()
    #bots greeting response
    bot_greetings = ['howdy', 'hi', 'holla', 'hey you', 'hi there']
    #Users greeting
    user_greetings = ['hi', 'hey', 'hello', 'hola', 'greetings', 'wassup']

    for word in text.split():
        if word in user_greetings:
            return random.choice(bot_greetings)

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

def bot_response(user_input):
    user_input = user_input.lower()
    sentence_list.append(user_input)
    bot_response = ''
    cm = CountVectorizer().fit_transform(sentence_list) #cm = count matrix
    similarity_scores = cosine_similarity(cm[-1], cm)
    similarity_scores_list = similarity_scores.flatten()
    index = index_sort(similarity_scores_list)
    index = index[1:]
    response_flag = 0

    d = 0
    for i in range(len(index)):
        if similarity_scores_list[index[i]] > 0.0:
            bot_response = bot_response+' '+sentence_list[index[i]]
            response_flag = 1
            d = d+1
        if d > 2:
            break

    if response_flag == 0:
        bot_response = bot_response+ ' '+'I apologize, I dont understand'

    sentence_list.remove(user_input)
    return bot_response

Intro = 'Doc Bot: I am Doctor Bot or Doc Bot for short. I will answer your questions about depression. To end the conversation, just type bye'
#Starting the chat
print(Intro)

exit_list = ['exit', 'see you later', 'bye', 'quit', 'break']

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))
        else:
            print('Doc Bot: ' + bot_response(user_input))
#Download the punkt package #print(corpus) #Creating a list of sentences #print(sentence_list) #Function to return a random greeting response to a users greeting #bots greeting response #Users greeting #swap #cm = count matrix #Starting the chat