# 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, stop_words='english')
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]+sent_tokens[idx+1]
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%.separation anxiety disorder decreases in prevalence from childhood through adolescence and adulthood and is the most prevalent anxiety disorder in children younger than 12 years. What are the diagnostic features of panic attacks? ROBO: features such as onset after age 45 years or the presence of atypical symptoms during a panic attack (e.g., vertigo, loss of consciousness, loss of bladder or bowel control, slurred speech, amnesia) suggest the possibility that another medical condition or a substance may be causing the panic attack symptoms.other mental disorders with panic attacks as an associated feature (e.g., other anxiety disorders and psychotic disorders).panic attacks that occur as a symptom of other anxiety disorders are expected (e.g., triggered by social situations in social anxiety disorder, by phobic objects or situations in specific phobia or agoraphobia, by worry in generalized anxiety disorder, by separation from home or attachment figures in separation anxiety disorder) and thus would not meet criteria for panic disorder. 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).the diagnosis requires endorsement of symptoms occurring in at least two of the following five situations:1) using public transportation, such as automobiles, buses, trains, ships, or planes; 2) being in open spaces, such as parking lots, marketplaces, or bridges; 3) being in enclosed spaces, such as shops, theaters, or cinemas; 4) standing in line or being in a crowd; or 5) being outside of the home alone. Does depression and anxiety have high comorbidity? ROBO: comorbidity with depression is high also in older adults.substances may be used as self-medication for social fears, but the symptoms of substance intoxication or withdrawal, such as trembling, may also be a source of (further) social fear. 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.the disturbance interferes with educational or occupational achievement or with social communication.c. Bye ROBO: Bye! take care..