Skip to content

Instantly share code, notes, and snippets.

@JustinaPetr
Last active September 22, 2021 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinaPetr/600eb14eab19997cf5129b159e9fa677 to your computer and use it in GitHub Desktop.
Save JustinaPetr/600eb14eab19997cf5129b159e9fa677 to your computer and use it in GitHub Desktop.
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
@crodriguez1a
Copy link

crodriguez1a commented Mar 10, 2020

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 the start and end parameters as seen here as to avoid a KeyError from nlu/training_data/formats/markdown.py. Does that sound accurate?

Cheers.

@Wing-e7
Copy link

Wing-e7 commented Apr 16, 2020

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 !

@asamagaio
Copy link

Thank you @crodriguez1a your comment literally saved my life

@crodriguez1a
Copy link

@asamagaio glad I could help

@Fares-Ayed
Copy link

Fares-Ayed commented Sep 22, 2021

@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