There are two types of chatbots:
- Rule-Based chatbot can only response on constrained rules set beforehand
- Self-Learned chatbot is trained with some neutral networks to react for response
NLTK Library
NLTK library is consist of 5 main components
- Morphological and Lexical Analysis dividing a text into paragraphs, words, and sentences
- Syntactic Analysis proper ordering of words which can affect its meaning
- Semantic Analysis transfers linear sequences of words into structures
- Discourse Integration interpretate sense of the context
- Pragmatic Analysis abstracting or deriving the meaningful use of language in situations
application
following is the main app code combining rule-based learning and self-learning
#lib.py
from nltk.tokenize import word_tokenize
import nltk
from nltk.stem import WordNetLemmatizer
import re
lem = WordNetLemmatizer()
def filter_command(phrase):
tokens=[]
tok=word_tokenize(phrase)
for t in tok:
tokens.append(tok.lower())
tags = nltk.pos_tag(tokens)
work=[]
work_f=[]
subject=[]
number=[]
adj=[]
query=[]
name=0
for tup in tags:
if "VB" in tup[1]:
work.append(tup[0])
if "CD" in tup[1]:
number.append(tup[0])
if "JJ" in tup[1]:
adj.append(tup[0])
if "NN" in tup[1]:
subject.append(tup[0])
if "W" in tup[1] and "that" not in tup[0]:
query.append(tup[0])
for w in work:
work_f.append(lem.lemmatize(w.lower()))
if query:
if "you" in tokens or "your" in tokens:
task=0
elif 'weather' not in tokens or 'news' not in tokens or 'headlines' not in tokens:
task=1
elif 'play' in work_f or 'song' in subject or 'play' in subject:
task=2
elif 'book' in work_f or 'book' in tokens[0]:
task=3
elif 'weather' in subject:
task=4
elif 'news' in subject or 'headlines' in subject:
task=5
else:
if '?' in tokens and 'you' not in tokens and 'your' not in tokens:
task=1
else:
task=0
return task,work_f,subject,number,adj,query
#create_response.py
import json
from preprocess_predicted import predict_c
from lib import filter_command
import random
from support import search_2,play,book,get_weather,get_news_update,scrape
def get_res(res):
with open('intents.json') as file:
intents=json.load(file)
tag = res[0]['intent']
all_tags = intents['intents']
for tags in all_tags:
if(tags['tag']== tag):
result = random.choice(tags['responses'])
break
return result,tag
def response(phrase):
flag_1=0
task,work_f,subject,number,adj,query=filter_command(phrase)
if task==0:
res_1=predict_c(phrase)
result,tag=get_res(res_1)
if tag=="noanswer":
results="Here are some google search results"
search_2(subject,phrase)
if tag=='goodbye':
flag_1=1
elif task==1:
scrape(phrase)
result="Here are some results"
elif task==2:
play(phrase,subject)
result="Here you go"
elif task==3:
book(phrase)
result="Here are some results"
elif task==4:
get_weather()
result="Here are the results"
elif task==5:
get_news_update()
result="Here are the results"
else:
result="Sorry, I don't think i understand"
print(result)
return flag_1
#main.py
from create_response import response
flag=0
while(flag==0):
user_response = input()
flag=response(user_response)