# chatbot-robo
#It is a very simple bot with hardly any cognitive skills. Though 'ROBO' responds to user input, it won't fool your friends. This example will help us to think through the design and challenge of creating a chatbot.
# coding: utf-8
# # Meet Robo: your friend
import nltk
import random
import string # to process standard python strings
f=open('anxietydsm.txt','r',errors = 'ignore')
raw=f.read()
raw=raw.lower()# converts to lowercase
raw=raw.replace('\n','')# removes weird characters
raw=raw.replace('- ','')# removes weird characters
#nltk.download('punkt') # first-time use only
#nltk.download('wordnet') # first-time use only
sent_tokens = nltk.sent_tokenize(raw)# converts to list of sentences
word_tokens = nltk.word_tokenize(raw)# converts to list of words
sent_tokens[:5]
['anxiety disorders include disorders that share features of excessive fear and anxiety and related behavioral disturbances.', 'fear is the emotional response to real or perceived imminent threat, whereas anxiety is anticipation of future threat.', 'obviously, these two states overlap, but they also differ, with fear more often associated with surges of autonomic arousal necessary for fight or flight, thoughts of immediate danger, and escape behaviors, and anxiety more often associated with muscle tension and vigilance in preparation for future danger and cautious or avoidant behaviors.', 'sometimes the level of fear or anxiety is reduced by pervasive avoidance behaviors.', 'panic attacks feature prominently within the anxiety disorders as a particular type of fear response.']
word_tokens[:5]
['anxiety', 'disorders', 'include', 'disorders', 'that']
lemmer = nltk.stem.WordNetLemmatizer()
#WordNet is a semantically-oriented dictionary of English included in NLTK.
def LemTokens(tokens):
return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
GREETING_INPUTS = ("hello", "hi", "greetings", "sup", "what's up","hey",)
GREETING_RESPONSES = ["hi", "hey", "*nods*", "hi there", "hello", "I am glad! You are talking to me"]
# Checking for greetings
def greeting(sentence):
"""If user's input is a greeting, return a greeting response"""
for word in sentence.split():
if word.lower() in GREETING_INPUTS:
return random.choice(GREETING_RESPONSES)
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# Generating response
def response(user_response):
robo_response=''
TfidfVec = TfidfVectorizer(tokenizer=LemNormalize)
tfidf = TfidfVec.fit_transform(sent_tokens)
vals = cosine_similarity(tfidf[-1], tfidf)
idx=vals.argsort()[0][-2]
flat = vals.flatten()
flat.sort()
req_tfidf = flat[-2]
if(req_tfidf==0):
robo_response=robo_response+"I am sorry! I don't understand you"
return robo_response
else:
robo_response = robo_response+sent_tokens[idx]
return robo_response
flag=True
print("ROBO: My name is Robo. I will answer your queries about Anxiety Disorders in the DSM-5. If you want to exit, type Bye!")
while(flag==True):
user_response = input()
user_response=user_response.lower()
if(user_response!='bye'):
if(user_response=='thanks' or user_response=='thank you' ):
flag=False
print("ROBO: You are welcome..")
else:
if(greeting(user_response)!=None):
print("ROBO: "+greeting(user_response))
else:
sent_tokens.append(user_response)
word_tokens=word_tokens+nltk.word_tokenize(user_response)
final_words=list(set(word_tokens))
print("ROBO: ",end="")
print(response(user_response))
sent_tokens.remove(user_response)
else:
flag=False
print("ROBO: Bye! take care..")
ROBO: My name is Robo. I will answer your queries about Anxiety Disorders in the DSM-5. If you want to exit, type Bye! What is the prevalence of separation anxiety disorder in the United States? ROBO: in adolescents in the united states, the 12-month prevalence is 1.6%. What are the diagnostic features of panic attacks? ROBO: panic attacks are uncommon, and unexpected panic attacks are rare, in preadolescent children. What is the essential feature of agoraphobia? ROBO: if an individual’s presentation meets criteria for panic disorder and agoraphobia, both diagnoses should be assigned.diagnostic featuresthe essential feature of agoraphobia is marked, or intense, fear or anxiety triggered by the real or anticipated exposure to a wide range of situations (criterion a). Does depression and anxiety have high comorbidity? ROBO: comorbidity with depression is high also in older adults. Is there a relationship between speaking and anxiety? ROBO: depressive and bipolar disorders are also comorbid with separation anxiety disorder in adults.selective mutismdiagnostic criteria 313.23 (f94.0)a. consistent failure to speak in specific social situations in which there is an expectation for speaking (e.g., at school) despite speaking in other situations.b. Bye ROBO: Bye! take care..