Skip to content Skip to sidebar Skip to footer

How To Integrate The Sentiment Analysis Script With The Chatbot For Analysing The User's Reply In The Same Console Screen?

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 d

Solution 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_analysisimport 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.defvalue_of(sentiment):
    if sentiment == 'positive': return1if sentiment == 'negative': return -1return0defsentence_score(sentence_tokens, previous_token, acum_score):
    ifnot 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 isnotNone:
            previous_tags = previous_token[2]
            if'inc'in previous_tags:
                token_score *= 2.0elif'dec'in previous_tags:
                token_score /= 2.0elif'inv'in previous_tags:
                token_score *= -1.0return sentence_score(sentence_tokens[1:], current_token, acum_score + token_score)

defsentiment_score(review):
    returnsum([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")

whileTrue:
    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.

Post a Comment for "How To Integrate The Sentiment Analysis Script With The Chatbot For Analysing The User's Reply In The Same Console Screen?"