# Extract the same entity in different contexts

**URL:** <https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906>\
**Category:** Rasa Open Source\
**Created:** [November 23, 2018, 11:16am UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906 "2018-11-23T11:16:43Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![tahesse](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@tahesse](https://forum.rasa.com/u/tahesse)\
**Post date:** [November 23, 2018, 11:16am UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/1 "2018-11-23T11:16:44Z")

</div>

Hey there,

I have some kind of alienated behavior for a use-case I want to implement using a chatbot. 🙂  
My issue is pretty similar to [Using single entity for two different intent's](http://forum.rasa.com/t/using-single-entity-for-two-different-intents/216).  
I basically want to ask a user for his forename, surname, birthdate to (almost uniquely) validate his ID in a natural way and then get his old address and his new address (so far I am only extracting the [german] street name). To do so, I switch to using `FormActions` at this point.

If my bot utters “Thanks for providing your data {forename}. Please name me your old street.” and the user responds with just the entity, like “Sesamstraße 5”, I end up with something like this over and over again:

```
DEBUG:rasa_core_sdk.executor:Received request to run 'action_test_form'
DEBUG:rasa_core_sdk.forms:The form '{'name': 'action_test_form', 'validate': True, 'rejected': False}' is active
DEBUG:rasa_core_sdk.forms:Validating user input '{'intent': {'name': 'get_evb', 'confidence': 0.4319833517074585}, 'entities': [{'start': 0, 'end': 13, 'value': 'Sesamstraße 5', 'entity': 'new_street', 'confidence': 0.5471068346577346, 'extractor': 'ner_crf'}], 'intent_ranking': [{'name': 'get_evb', 'confidence': 0.4319833517074585}, {'name': 'deny', 'confidence': 0.40701043605804443}, {'name': 'do_it_myself', 'confidence': 0.3803158700466156}, {'name': 'get_birthdate', 'confidence': 0.37413734197616577}, {'name': 'bye', 'confidence': 0.37190955877304077}, {'name': 'thanks', 'confidence': 0.36665356159210205}, {'name': 'affirm', 'confidence': 0.3496394753456116}, {'name': 'greet', 'confidence': 0.3164404630661011}, {'name': 'change_data', 'confidence': 0.2958131432533264}, {'name': 'need_help', 'confidence': 0.20778490602970123}], 'text': 'Sesamstraße 5'}'
DEBUG:rasa_core_sdk.forms:Extracted 'Sesamstraße 5' for extra slot 'new_street'
DEBUG:rasa_core_sdk.forms:Trying to extract requested slot 'old_street' ...
DEBUG:rasa_core_sdk.forms:Got mapping '{'type': 'from_entity', 'entity': 'old_street', 'intent': [], 'not_intent': []}'
DEBUG:rasa_core_sdk.forms:Got mapping '{'type': 'from_text', 'intent': ['get_old_street'], 'not_intent': []}'
DEBUG:rasa_core_sdk.forms:Failed to extract requested slot 'old_street'
DEBUG:rasa_core_sdk.forms:Request next slot 'old_street'
DEBUG:rasa_core_sdk.executor:Successfully ran 'action_test_form'

```

The intent is completly wrong (which I cannot fully understand, as I am working with many things like lookup, regex, and variations of different intents). Nevertheless, it feels like I cannot rely on the intent… and I am not sure how to distinguish between the entities “old\_street” and “new\_street” correctly if they basically can have the same values.

Does anyone have an idea what’s the best thing to do at this point? I already thought about the need to do something completely different from the bottom up or can I incorporate the bot utterance in `ner_crf`?

Looking forward to your replies and ideas! Thanks 🙂

Kind regards,  
-Thomas

---

<div class="post-metadata">

**Author:** ![Ghostvv](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ghostvv/32/211_2.png) [@Ghostvv](https://forum.rasa.com/u/Ghostvv)\
**Post date:** [November 23, 2018, 1:52pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/2 "2018-11-23T13:52:08Z")

</div>

I believe, you’re already familiar with `slot_mapping` in `FormAction`, you can leverage it by creating `street` entity and populate `old_street` or `new_street`, depending on the previous asked question, and if entity is not extracted use `from_text`, as you do it already for names 😉

Then you can override `validate` method to verify whether extracted information matches some pattern of address

---

<div class="post-metadata">

**Author:** ![tahesse](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@tahesse](https://forum.rasa.com/u/tahesse)\
**Post date:** [November 24, 2018, 7:33am UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/3 "2018-11-24T07:33:47Z")

</div>

Sure, this will help in a first step! But is there a “simple” way to retrieve the last utterance name from the tracker? It is currently not saved (just the latest action name which is `action_listen` and thus not too helpful). The latest utterance name cannot really be saved in the rasa\_core\_sdk either because the tracker is loaded from the deserialized tracker payload from rasa\_core (as I see it).

🙂 Thanks again! 🥳

---

<div class="post-metadata">

**Author:** ![Ghostvv](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ghostvv/32/211_2.png) [@Ghostvv](https://forum.rasa.com/u/Ghostvv)\
**Post date:** [November 27, 2018, 5:47pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/4 "2018-11-27T17:47:45Z")

</div>

You can iterate over `tracker.events` and look for it

---

<div class="post-metadata">

**Author:** ![tahesse](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@tahesse](https://forum.rasa.com/u/tahesse)\
**Post date:** [November 29, 2018, 12:33pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/5 "2018-11-29T12:33:45Z")

</div>

True, but the `tracker.events` do not store the utterances by their ID `utter_{name}` but the text.

---

<div class="post-metadata">

**Author:** ![Ghostvv](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ghostvv/32/211_2.png) [@Ghostvv](https://forum.rasa.com/u/Ghostvv)\
**Post date:** [November 29, 2018, 12:49pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/6 "2018-11-29T12:49:37Z")

</div>

there is `BotUttered` event there [rasa\_core\_sdk/events.py at 47106d959767148d15b328a3a38a95cd697e0616 · RasaHQ/rasa\_core\_sdk · GitHub](https://github.com/RasaHQ/rasa_core_sdk/blob/47106d959767148d15b328a3a38a95cd697e0616/rasa_core_sdk/events.py#L27)

so something along the lines should work:

```auto
        last_utter = None
        for e in reversed(tracker.events):
            if e['event'] == 'bot':
                last_utter = e['text']
                break

```

---

<div class="post-metadata">

**Author:** ![Ghostvv](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ghostvv/32/211_2.png) [@Ghostvv](https://forum.rasa.com/u/Ghostvv)\
**Post date:** [November 29, 2018, 12:50pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/7 "2018-11-29T12:50:45Z")

</div>

Ah, sorry yes if you use `utter_...` template inside a custom action it is hard to find the name of the template

---

<div class="post-metadata">

**Author:** ![tahesse](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@tahesse](https://forum.rasa.com/u/tahesse)\
**Post date:** [November 29, 2018, 1:03pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/8 "2018-11-29T13:03:28Z")

</div>

Yes, and I figured that I’ll have a hard time to obtain the templates ID’s from each utterance text. ☹

---

<div class="post-metadata">

**Author:** ![Ghostvv](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ghostvv/32/211_2.png) [@Ghostvv](https://forum.rasa.com/u/Ghostvv)\
**Post date:** [November 29, 2018, 1:45pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/9 "2018-11-29T13:45:04Z")

</div>

but why do you need last utterance, can’t you use action name?

---

<div class="post-metadata">

**Author:** ![tahesse](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@tahesse](https://forum.rasa.com/u/tahesse)\
**Post date:** [November 29, 2018, 1:53pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/10 "2018-11-29T13:53:43Z")

</div>

unfortunately not, because the last action name oftentimes is `action_listen`

---

<div class="post-metadata">

**Author:** ![Ghostvv](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ghostvv/32/211_2.png) [@Ghostvv](https://forum.rasa.com/u/Ghostvv)\
**Post date:** [November 29, 2018, 2:08pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/11 "2018-11-29T14:08:32Z")

</div>

so basically you need action name before previous action? For this you can use `tracker.events`:

```auto
    action_name = None
    for event in reversed(tracker.events):
        if (event['event'] == 'action' and
                not event['name'] == "action_listen"):
            action_name = event['name']
            break

```

---

<div class="post-metadata">

**Author:** ![tahesse](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@tahesse](https://forum.rasa.com/u/tahesse)\
**Post date:** [November 29, 2018, 3:33pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/12 "2018-11-29T15:33:10Z")

</div>

True, thanks for the snippet code at this point! 🙂  
But this will sadly not yield the exact utterance template IDs which I seek (already spent some time in the debugger trying to figure out if I can do the reverse lookup from utterance text to its template ID).

---

<div class="post-metadata">

**Author:** ![Ghostvv](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ghostvv/32/211_2.png) [@Ghostvv](https://forum.rasa.com/u/Ghostvv)\
**Post date:** [November 29, 2018, 4:59pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/13 "2018-11-29T16:59:17Z")

</div>

We don’t store template ID at this moment. I think it makes sense to add to `BotUttered` event. Would you like to make a PR for this?

---

<div class="post-metadata">

**Author:** ![shaileshsarda](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@shaileshsarda](https://forum.rasa.com/u/shaileshsarda)\
**Post date:** [March 1, 2019, 10:53am UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/14 "2019-03-01T10:53:12Z")

</div>

I have come across same issues with the scenarios.

1. In this scenario, there is information about different cuisines like Maharashtrian, Gujarati thali, south-Indian etc with their respective intent name like Indian\_cuisine where the user might ask about the information about cuisines.And the bot gives the brief information about the cuisines.

User: I want to know more about the Maharashtrian dishes. Bot: [Brief Information about Maharashtrian dishes]

1. In this scenario, I am using FormAction Policy in which user try to order that particular food where bot asks the user for “Specify food type which you want to order?” a user needs to put type\_of\_cuisines there like Maharashtrian. The slot should picks the value for “type\_of\_cuisines”.

The actual problem occurs when the FormAction is active and the user put type\_of\_cuisines is for ex. Maharashtrian and the bot respond the information about that dish[Scenario-1] rather hold the slot for that type\_of\_cuisines[Scenario-2].

Any Suggestions?

---

<div class="post-metadata">

**Author:** ![Ghostvv](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ghostvv/32/211_2.png) [@Ghostvv](https://forum.rasa.com/u/Ghostvv)\
**Post date:** [March 1, 2019, 12:14pm UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/15 "2019-03-01T12:14:35Z")

</div>

it depends where do you call information template, you can call `utter_template` explicitly providing slot values. There is a thread in the forum about it somewhere

---

<div class="post-metadata">

**Author:** ![red-frog](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/red-frog/32/2293_2.png) [@red-frog](https://forum.rasa.com/u/red-frog)\
**Post date:** [March 22, 2019, 3:37am UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/16 "2019-03-22T03:37:11Z")

</div>

So how to solve the problem, is have some examples?

---

<div class="post-metadata">

**Author:** ![Ghostvv](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/ghostvv/32/211_2.png) [@Ghostvv](https://forum.rasa.com/u/Ghostvv)\
**Post date:** [March 22, 2019, 9:12am UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/17 "2019-03-22T09:12:41Z")

</div>

which problem?

---

<div class="post-metadata">

**Author:** ![noman](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/noman/32/10071_2.png) [@noman](https://forum.rasa.com/u/noman)\
**Post date:** [May 8, 2019, 7:51am UTC](https://forum.rasa.com/t/extract-the-same-entity-in-different-contexts/2906/18 "2019-05-08T07:51:20Z")

</div>

> [@tahesse](#):
>
> distinguish between the entities “old\_street” and “new\_street”

To fill the slots “old\_street” and “new\_street” do we need another slot street with same name with the enitty ‘street’ so that rasa can fill the slot street with entity street automatically and then we set our old\_street and new\_street slots with that entity? as i have tried to set old\_street slot to get mapped with the street entity but it didnt work
