如何将情绪分析脚本与聊天机器人集成以在同一控制台屏幕中分析用户的回复?
How to integrate the sentiment analysis script with the chatbot for analysing the user's reply in the same console screen?
我想制作一个聊天机器人,它使用情绪分析器脚本来了解我已完成聊天机器人制作的用户回复的情绪。
现在我唯一想做的就是使用这个脚本来分析使用我制作的聊天机器人的用户的回复。
我应该如何将此 sentiment_analysis.py 脚本与 chatbot.py 文件集成以分析用户的情绪?
更新:
整体表现会是这样:
聊天机器人:今天过得怎么样?
用户:这是很棒的一天。今天我感到非常高兴和有动力。
用户回复:肯定
情绪得分=(一些随机值)
提前致谢。
将 类 从情绪分析脚本导入聊天机器人脚本。然后根据您的要求做必要的事情。例如。我修改了你的聊天机器人脚本:
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")
遇到错误时请告诉我。
我想制作一个聊天机器人,它使用情绪分析器脚本来了解我已完成聊天机器人制作的用户回复的情绪。
现在我唯一想做的就是使用这个脚本来分析使用我制作的聊天机器人的用户的回复。
我应该如何将此 sentiment_analysis.py 脚本与 chatbot.py 文件集成以分析用户的情绪?
更新:
整体表现会是这样:
聊天机器人:今天过得怎么样?
用户:这是很棒的一天。今天我感到非常高兴和有动力。
用户回复:肯定
情绪得分=(一些随机值)
提前致谢。
将 类 从情绪分析脚本导入聊天机器人脚本。然后根据您的要求做必要的事情。例如。我修改了你的聊天机器人脚本:
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")
遇到错误时请告诉我。