# Need help please!

**URL:** <https://forum.rasa.com/t/need-help-please/13002>\
**Category:** Rasa Open Source\
**Created:** [July 16, 2019, 2:22pm UTC](https://forum.rasa.com/t/need-help-please/13002 "2019-07-16T14:22:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![m2cci-sba](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/m2cci-sba/32/4029_2.png) [@m2cci-sba](https://forum.rasa.com/u/m2cci-sba)\
**Post date:** [July 16, 2019, 2:22pm UTC](https://forum.rasa.com/t/need-help-please/13002/1 "2019-07-16T14:22:30Z")

</div>

i’ve solved thank you i have another question , i have alwayes this result however i changed the message

always detecting a postive sentiment with confidence 0.333

{‘intent’: {‘name’: ‘insultes’, ‘confidence’: 0.35888357915887636}, ‘entities’: [{‘value’: ‘pos’, ‘confidence’: 0.3333333333333333, ‘entity’: ‘sentiment’, ‘extractor’: ‘sentiment\_extractor’}], ‘intent\_ranking’: [{‘name’: ‘insultes’, ‘confidence’: 0.35888357915887636}, {‘name’: ‘salutations’, ‘confidence’: 0.07922486366609584}, {‘name’: ‘au\_revoir’, ‘confidence’: 0.06963384023417138}, {‘name’: ‘out’, ‘confidence’: 0.057168641232126574}, {‘name’: ‘positive\_answer’, ‘confidence’: 0.05699548462320999}, {‘name’: ‘congrats’, ‘confidence’: 0.05146011802312083}, {‘name’: ‘salaire’, ‘confidence’: 0.05105493986389435}, {‘name’: ‘annulation’, ‘confidence’: 0.04765725667849151}, {‘name’: ‘negative\_answer’, ‘confidence’: 0.04180718673910084}, {‘name’: ‘merci’, ‘confidence’: 0.03432302984684233}], ‘text’: ‘connard’}

---

<div class="post-metadata">

**Author:** ![erohmensing](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/erohmensing/32/1789_2.png) [@erohmensing](https://forum.rasa.com/u/erohmensing)\
**Post date:** [July 16, 2019, 3:54pm UTC](https://forum.rasa.com/t/need-help-please/13002/2 "2019-07-16T15:54:52Z")

</div>

Hi @m2cci-sba. So to be clear, your issue is that no matter what you send, your custom sentiment analyser always returns positive with confidence 0.333?

---

<div class="post-metadata">

**Author:** ![m2cci-sba](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/m2cci-sba/32/4029_2.png) [@m2cci-sba](https://forum.rasa.com/u/m2cci-sba)\
**Post date:** [July 16, 2019, 9:16pm UTC](https://forum.rasa.com/t/need-help-please/13002/3 "2019-07-16T21:16:23Z")

</div>

yes !!!

---

<div class="post-metadata">

**Author:** ![erohmensing](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/erohmensing/32/1789_2.png) [@erohmensing](https://forum.rasa.com/u/erohmensing)\
**Post date:** [July 16, 2019, 10:45pm UTC](https://forum.rasa.com/t/need-help-please/13002/4 "2019-07-16T22:45:27Z")

</div>

Sounds like something is wrong within the custom sentiment analyser component then, does the code work on its own?

---

<div class="post-metadata">

**Author:** ![m2cci-sba](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/m2cci-sba/32/4029_2.png) [@m2cci-sba](https://forum.rasa.com/u/m2cci-sba)\
**Post date:** [July 17, 2019, 7:17am UTC](https://forum.rasa.com/t/need-help-please/13002/5 "2019-07-17T07:17:13Z")

</div>

yes it’s working , i have just one stupid question 😃 how to do the connexion between my labels.txt (that contain the sentiments ) and my data nlu because i’ve remplaced

training\_data = training\_data.training\_examples #list of Message objects tokens = [list(map(lambda x: x.text, t.get(‘tokens’))) for t in training\_data]

with 🙂

nlu = nlu tokens = [list(map(lambda x: x.text, t.get(‘tokens’))) for t in nlu]

nlu is my the data nlu folder .

---

<div class="post-metadata">

**Author:** ![m2cci-sba](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/m2cci-sba/32/4029_2.png) [@m2cci-sba](https://forum.rasa.com/u/m2cci-sba)\
**Post date:** [July 17, 2019, 7:32am UTC](https://forum.rasa.com/t/need-help-please/13002/6 "2019-07-17T07:32:29Z")

</div>

I’ve created also a registry file 🙂from rasa\_nlu.customcomponents.sentiment import MySentimentAnalyzer

from data import sentiment

# all import needed

component\_classes = [

# 

# all rasa components added no change

# 

MySentimentAnalyzer

]

---

<div class="post-metadata">

**Author:** ![m2cci-sba](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/m2cci-sba/32/4029_2.png) [@m2cci-sba](https://forum.rasa.com/u/m2cci-sba)\
**Post date:** [July 17, 2019, 8:05am UTC](https://forum.rasa.com/t/need-help-please/13002/7 "2019-07-17T08:05:15Z")

</div>

here is my chatbot-nlp folder

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/2/21716b4c968a148e92647655aca1dfa0a2d8488a.png)

my data contains nlu ans stories .

my config file 🙂# Configuration for Rasa NLU.

# [https://rasa.com/docs/rasa/nlu/components/](https://rasa.com/docs/rasa/nlu/components/)

language: fr\_core\_news\_md pipeline:

- name: “SpacyNLP”
- name: “SpacyTokenizer”
- name: “sentiment.SentimentAnalyzer”
- name: “SpacyFeaturizer”
- name: “WhitespaceTokenizer”
- name: “RegexFeaturizer”
- name: “CRFEntityExtractor”
- name: “EntitySynonymMapper”
- name: “CountVectorsFeaturizer”
- name: “SklearnIntentClassifier”

# Configuration for Rasa Core.

# [https://rasa.com/docs/rasa/core/policies/](https://rasa.com/docs/rasa/core/policies/)

policies:

- name: MemoizationPolicy
- name: KerasPolicy
- name: MappingPolicy
- name: FormPolicy
- name: “FallbackPolicy” nlu\_threshold: 0.25 core\_threshold: 0.3 fallback\_action\_name: “action\_my\_fallback”

labels:

pos pos neu neu neg neg

sentiment.py

from rasa.nlu.components import Component from rasa.nlu import utils from rasa.nlu.model import Metadata

import nltk from nltk.classify import NaiveBayesClassifier import os

import typing from typing import Any, Optional, Text, Dict

SENTIMENT\_MODEL\_FILE\_NAME = “sentiment\_classifier.pkl”

class SentimentAnalyzer(Component): “”“A custom sentiment analysis”“” name = “sentiment” provides = [“entities”] requires = [“tokens”] defaults = {} language\_list = [“fr\_core\_news\_md”] print(‘initialised the class’)

```
def __init__ (self, component_config=None):
    super(SentimentAnalyzer, self). __init__ (component_config)
def train(self, training_data, cfg, **kwargs):

    """Load the sentiment polarity labels from the text
       file, retrieve training tokens and after formatting
       data train the classifier."""
    with open('labels.txt','r') as f:
        labels = f.read().splitlines()
    training_data = training_data.training_examples #list of Message objects
    tokens = [list(map(lambda x: x.text, t.get('tokens'))) for t in training_data]
    processed_tokens = [self.preprocessing(t) for t in tokens]
    labeled_data = [(t, x) for t,x in zip(processed_tokens, labels)]
    self.clf = NaiveBayesClassifier.train(labeled_data)
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 preprocessing(self, tokens):
    """Create bag-of-words representation of the training examples."""
    
    return ({word: True for word in tokens})
def process(self, message, **kwargs):
    """Retrieve the tokens of the new message, pass it to the classifier
        and append prediction results to the message class."""
    
    if not self.clf:
        # component is either not trained or didn't
        # receive enough training data
        entity = None
    else:
        tokens = [t.text for t in message.get("tokens")]
        tb = self.preprocessing(tokens)
        pred = self.clf.prob_classify(tb)

        sentiment = pred.max()
        confidence = pred.prob(sentiment)

        entity = self.convert_to_rasa(sentiment, confidence)

        message.set("entities", [entity], add_to_output=True)
def persist(self, file_name, model_dir):
    """Persist this model into the passed directory."""
    classifier_file = os.path.join(model_dir, SENTIMENT_MODEL_FILE_NAME)
    utils.json_pickle(classifier_file, self)
    return {"classifier_file": SENTIMENT_MODEL_FILE_NAME}

@classmethod
def load(cls,
         meta: Dict[Text, Any],
         model_dir=None,
         model_metadata=None,
         cached_component=None,
         **kwargs):
    file_name = meta.get("classifier_file")
    classifier_file = os.path.join(model_dir, file_name)
    return utils.json_unpickle(classifier_file)

```

---

<div class="post-metadata">

**Author:** ![erohmensing](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/erohmensing/32/1789_2.png) [@erohmensing](https://forum.rasa.com/u/erohmensing)\
**Post date:** [July 17, 2019, 4:52pm UTC](https://forum.rasa.com/t/need-help-please/13002/8 "2019-07-17T16:52:06Z")

</div>

> [@m2cci-sba](#):
>
> i’ve remplaced
> 
> training\_data = training\_data.training\_examples #list of Message objects tokens = [list(map(lambda x: x.text, t.get(‘tokens’))) for t in training\_data]
> 
> with 🙂
> 
> nlu = nlu tokens = [list(map(lambda x: x.text, t.get(‘tokens’))) for t in nlu]

where and why did you change this? I think that might be causing problems since you’re not using the training data to train the model?

---

<div class="post-metadata">

**Author:** ![m2cci-sba](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/m2cci-sba/32/4029_2.png) [@m2cci-sba](https://forum.rasa.com/u/m2cci-sba)\
**Post date:** [July 17, 2019, 5:25pm UTC](https://forum.rasa.com/t/need-help-please/13002/9 "2019-07-17T17:25:45Z")

</div>

I should use my data nlu to connect it with labels, and I made those changes in the sentiment.py file but I also try to run it without changing the file but still not working how can I connect my data file to sentiment.py and labels. Txt please

---

<div class="post-metadata">

**Author:** ![erohmensing](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/erohmensing/32/1789_2.png) [@erohmensing](https://forum.rasa.com/u/erohmensing)\
**Post date:** [July 17, 2019, 6:11pm UTC](https://forum.rasa.com/t/need-help-please/13002/10 "2019-07-17T18:11:23Z")

</div>

Right, but you only changed the code in your custom code right, not in the rest of the code?

---

<div class="post-metadata">

**Author:** ![m2cci-sba](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/m2cci-sba/32/4029_2.png) [@m2cci-sba](https://forum.rasa.com/u/m2cci-sba)\
**Post date:** [July 17, 2019, 6:28pm UTC](https://forum.rasa.com/t/need-help-please/13002/11 "2019-07-17T18:28:04Z")

</div>

Yes I added the custom component the same as the tutorial, registry file, labels.txt and I’ve added tlin the pipeline the new custom component that’s it
