0

I want to make a chatbot that uses Sentiment analyser script for knowing the sentiment of the user's reply for which I have completed the Chatbot making.

Now only thing I want to do is to use this Script to analyse the reply of user using the chatbot that I have made.
How should I integrate this sentiment_analysis.py script with the chatbot.py file to analyse the sentiment's of user?

Update: The overall performance will be like this :
Chatbot: How was your day?
User: It was an awesome day. I feel so elated and motivated today.
User Reply: Positive
Sentiment score = (some random value)

Thanking you in advance.

1 Answers1

1

Import classes from sentiment analysis script to chatbot script. Then do necessary things according to your requirement. For example. I modified your chatbot script:

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from sentiment_analysis import Splitter, POSTagger, DictionaryTagger  # import all the classes from sentiment_analysis
import os

bot = ChatBot('Bot')
bot.set_trainer(ListTrainer)

# for files in os.listdir('C:/Users/username\Desktop\chatterbot\chatterbot_corpus\data/english/'):
# data = open('C:/Users/username\Desktop\chatterbot\chatterbot_corpus\data/english/' + files, 'r').readlines()
data = [
    "My name is Tony",
    "that's a good name",
    "Thank you",
    "How you doing?",
    "I am Fine. What about you?",
    "I am also fine. Thanks for asking."]

bot.train(data)

# I included 3 functions from sentiment_analysis here for ease of loading. Alternatively you can create a class for them in sentiment_analysis.py and import here.
def value_of(sentiment):
    if sentiment == 'positive': return 1
    if sentiment == 'negative': return -1
    return 0

def sentence_score(sentence_tokens, previous_token, acum_score):
    if not sentence_tokens:
        return acum_score
    else:
        current_token = sentence_tokens[0]
        tags = current_token[2]
        token_score = sum([value_of(tag) for tag in tags])
        if previous_token is not None:
            previous_tags = previous_token[2]
            if 'inc' in previous_tags:
                token_score *= 2.0
            elif 'dec' in previous_tags:
                token_score /= 2.0
            elif 'inv' in previous_tags:
                token_score *= -1.0
        return sentence_score(sentence_tokens[1:], current_token, acum_score + token_score)

def sentiment_score(review):
    return sum([sentence_score(sentence, None, 0.0) for sentence in review])

# create instances of all classes
splitter = Splitter()
postagger = POSTagger()
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml',
                            'dicts/inc.yml', 'dicts/dec.yml', 'dicts/inv.yml'])

print("ChatBot is Ready...")
print("ChatBot : Welcome to my world! What is your name?")
message = input("you: ")
print("\n")

while True:
    if message.strip() != 'Bye'.lower():

        reply = bot.get_response(message)

        # process the text
        splitted_sentences = splitter.split(message)
        pos_tagged_sentences = postagger.pos_tag(splitted_sentences)
        dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)

        # find sentiment score
        score = sentiment_score(dict_tagged_sentences)

        if (score >= 1):
            print('User Reply: Positive')
        else:
            print('User Reply: Negative')

        print("Sentiment score :",score)
        print('ChatBot:',reply)

    if message.strip() == 'Bye'.lower():
        print('ChatBot: Bye')
        break
    message = input("you: ")
    print("\n")

Let me know when you get errors.

Mufeed
  • 3,018
  • 4
  • 20
  • 29
  • This code is working fine after I ran with necessary files on my machine. – Mufeed Jul 04 '18 at 14:48
  • Yes, it is wroking well.Just a lil' bit issue.The Bold Part is asking so many question on the go: 'ChatBot is Ready... ChatBot : Welcome to my world! What is your name? you: Tony. Thank You User Reply: Positive Sentiment score : 1.0 **ChatBot: How you doing?I am Fine. What about you?I am also fine. Thanks for asking.** you: I am doing great. User Reply: Positive Sentiment score : 1.0 ChatBot: that's a good name you: You are so bad Bot. User Reply: Negative Sentiment score : -1.0 ChatBot: that's a good name you: No, I have a bad feeling' – Sarah Sagittarius Jul 04 '18 at 15:12
  • Problem with training data. I updated the code. Please check it – Mufeed Jul 04 '18 at 15:25
  • The problem was we added a bunch of dialogues without comma and trained it. I modified the code by adding comma. You need to delete the old db file and retrain after adding comma. Problem will be slved. – Mufeed Jul 05 '18 at 04:52
  • Yes now it works very well. Thank you. But how does it make the database using 'Sqlite Alchemy' or any other? Another question is I want the chatbot's response as "That's a good name." after any name. How is that possible? – Sarah Sagittarius Jul 05 '18 at 06:24
  • You can go through https://chatterbot.readthedocs.io/en/stable/storage/index.html to get some ideas about integrating other dbs. I don't have enough knowledge to answer your second question. I will go through the docs and let you know if I get anything to help you. – Mufeed Jul 05 '18 at 06:33
  • Sure I will go through this doc. Thanks for being helpful always. – Sarah Sagittarius Jul 05 '18 at 06:46
  • One question though : What Libraries are required for above code and is there a way to know using cmd? – Sarah Sagittarius Jul 07 '18 at 06:51
  • You mean the code I posted here or the code you want to create in future? – Mufeed Jul 07 '18 at 07:04
  • The code you posted here. I have to note down all the libraries that i have used here for later reference. – Sarah Sagittarius Jul 07 '18 at 09:36
  • Please tell me what libraries have been used asap. Thank You in advance. – Sarah Sagittarius Jul 08 '18 at 17:27
  • Main libraries - Nltk, chatterbot, mostly we use sklearn too, other common libraries - os,sys. If you want you can use Tensorflow too – Mufeed Jul 09 '18 at 04:56
  • Could you please help me with the backend database that is being created as dbsqlite to be converted in csv automatically, can you please some method or any API? – Sarah Sagittarius Jul 13 '18 at 06:07
  • What you want to store in your csv? – Mufeed Jul 13 '18 at 06:17
  • When I run my above chatbot code, it creates a backend dbsqlite3 file. I want to convert that dbsqlite3 file to .csv file on its own. – Sarah Sagittarius Jul 13 '18 at 06:29
  • I am working on it. I will let you know if I succeed :) – Mufeed Jul 13 '18 at 07:16
  • Okay. Make it possile asap. Thank you life saviour – Sarah Sagittarius Jul 13 '18 at 07:21
  • Hy, Sorry for the delay. I had some other work to do. I analysed the database file. There are multiple tables in it. You want to create a csv file for each? – Mufeed Jul 13 '18 at 09:09
  • Here I am back to ask for your help! I was thinking to make a plugin for my chatbot. Do you have any suggestion on how to proceed and make that implementable? – Sarah Sagittarius Jul 19 '18 at 11:22
  • Well. Please be specific. I didn't quiet understand your requirement. – Mufeed Jul 20 '18 at 06:28
  • i want this chatbot to be runnable from any browser. Any suggestion? – Sarah Sagittarius Jul 23 '18 at 19:15
  • You can convert your chat bot to a simple web application using Flask framework. – Mufeed Jul 24 '18 at 05:45
  • Okay.Thanks for the suggestion. – Sarah Sagittarius Jul 28 '18 at 10:58
  • Which machine learning approach have been used in the above Code? – Sarah Sagittarius Jul 28 '18 at 11:04