BozZRoxX

AI Chatbot in Python

Oct 15th, 2020
874
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from newspaper import Article
  2. import random
  3. import string
  4. import nltk
  5. from sklearn.feature_extraction.text import CountVectorizer
  6. from sklearn.metrics.pairwise import cosine_similarity
  7. import numpy as np
  8. import warnings
  9. warnings.filterwarnings('ignore')
  10.  
  11. nltk.download('punkt', quiet=True)
  12.  
  13. #Get The Article
  14. article = Article('Add your URL')
  15. article.download()
  16. article.parse()
  17. article.nlp()
  18. corpus = article.text
  19.  
  20. #Print Article
  21. print(corpus)
  22.  
  23.  
  24. #Tokenization
  25. text = corpus
  26. sentence_list = nltk.sent_tokenize(text) #Create list of sentences
  27.  
  28. #Print List Of Sentences
  29. print(sentence_list)
  30.  
  31. #Random Greeting Response Function
  32. def greeting_response(text):
  33.     text = text.lower()
  34.  
  35.     #Bots greeting response
  36.     bot_greetings = ['Howdy!', 'Hi!', 'Hello!', 'Greetings!']
  37.  
  38.     #User greeting
  39.     user_greetings = ['hi', 'hey', 'hello', 'greetings', 'wassup']
  40.  
  41.     for word in text.split():
  42.         if word in user_greetings:
  43.             return random.choice(bot_greetings)
  44.  
  45.  
  46. def index_sort(list_var):
  47.     lenght = len(list_var)
  48.     list_index = list(range(0, lenght))
  49.  
  50.     x = list_var
  51.     for i in range(lenght):
  52.         for j in range(lenght):
  53.             if x[list_index[i]] > x[list_index[j]]:
  54.                 #Swap
  55.                 temp = list_index[i]
  56.                 list_index[i] = list_index[j]
  57.                 list_index[j] = temp
  58.  
  59.     return list_index
  60.  
  61. #Bots Response
  62. def bot_response(user_input):
  63.     user_input = user_input.lower()
  64.     sentence_list.append(user_input)
  65.     bot_response = ''
  66.     cm = CountVectorizer().fit_transform(sentence_list)
  67.     similarity_scores = cosine_similarity(cm[-1], cm)
  68.     similarity_scores_list = similarity_scores.flatten()
  69.     index = index_sort(similarity_scores_list)
  70.     index = index[1:]
  71.     response_flag = 0
  72.  
  73.     j = 0
  74.     for i in range(len(index)):
  75.         if similarity_scores_list[index[i]] > 0.0:
  76.             bot_response = bot_response+' '+sentence_list[index[i]]
  77.             response_flag = 1
  78.             j = j+1
  79.         if j > 2:
  80.             break
  81.  
  82.         if response_flag == 0:
  83.             bot_response = bot_response+' '+"I apologize, I don't understand."
  84.         sentence_list.remove(user_input)
  85.  
  86.         return bot_response
  87.  
  88. #Start the chat
  89. print('Doc Bot: Hi I am a Doctor Bot. To exit, type [bye].')
  90.  
  91. exit_list = ['exit', 'see you later', 'bye', 'quit', 'break', 'stop']
  92.  
  93.  
  94. while(True):
  95.     user_input = input()
  96.     if user_input.lower() in exit_list:
  97.         print('Doc Bot: Chat with you later, and remember... stay safe!')
  98.         break
  99.     else:
  100.         if greeting_response(user_input) != None:
  101.             print('Doc Bot: '+greeting_response(user_input))
  102.         else:
  103.             print('Doc Bot: '+bot_response(user_input))
  104.  
  105.            
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.    
  113.    
  114.  
RAW Paste Data