Last active
September 22, 2021 09:36
-
-
Save JustinaPetr/600eb14eab19997cf5129b159e9fa677 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rasa.nlu.components import Component | |
from rasa.nlu import utils | |
from rasa.nlu.model import Metadata | |
import nltk | |
from nltk.sentiment.vader import SentimentIntensityAnalyzer | |
import os | |
class SentimentAnalyzer(Component): | |
"""A pre-trained sentiment component""" | |
name = "sentiment" | |
provides = ["entities"] | |
requires = [] | |
defaults = {} | |
language_list = ["en"] | |
def __init__(self, component_config=None): | |
super(SentimentAnalyzer, self).__init__(component_config) | |
def train(self, training_data, cfg, **kwargs): | |
"""Not needed, because the the model is pretrained""" | |
pass | |
def convert_to_rasa(self, value, confidence): | |
"""Convert model output into the Rasa NLU compatible output format.""" | |
entity = {"value": value, | |
"confidence": confidence, | |
"entity": "sentiment", | |
"extractor": "sentiment_extractor"} | |
return entity | |
def process(self, message, **kwargs): | |
"""Retrieve the text message, pass it to the classifier | |
and append the prediction results to the message class.""" | |
sid = SentimentIntensityAnalyzer() | |
res = sid.polarity_scores(message.text) | |
key, value = max(res.items(), key=lambda x: x[1]) | |
entity = self.convert_to_rasa(key, value) | |
message.set("entities", [entity], add_to_output=True) | |
def persist(self, model_dir): | |
"""Pass because a pre-trained model is already persisted""" | |
pass | |
Hey @JustinaPetr. Ran the exact code above on a basic rasa init project ( didnt change anything else). Got the KeyError as mentioned by @crodiguez1a. Thanks to him rectified it. But i currently get a rasa.utils.endpoints.ClientResponseError: 500 with the message " name \'entity\' is not defined","reason":"ConversationError"
Rasa Version: 1.9.6. Everything else is the latest version. Did a rasa init today.
Was not able to find the solution anywhere. Kindly help !
Thank you @crodriguez1a your comment literally saved my life
@asamagaio glad I could help
@crodriguez1a how can i use start and end? because your link is not available any more?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @JustinaPetr, just reading through your custom component post. Excellent!
I ran into a minor thing during end-to-end evaluation (and I realize it has been a little while since that post).
I'm running 1.7.1, and It does seem that when
entity
is defined (line 46 above), we should also include thestart
andend
parameters as seen here as to avoid aKeyError
fromnlu/training_data/formats/markdown.py
. Does that sound accurate?Cheers.