Getting the entities from previous user message/questions

Hi,

I want get the entities from the previous user message.

For e.g. : User : Show me the restaurants in Mumbai Bot : Please tell me the type of restaurant User: Chinese

Here i need to get both location(Mumbai) and restaurant type(Chinese).

Could anyone please help me how to achieve this?

rasa_core_sdk=0.12.1 rasa_nlu~=0.14.0

Thanks in advance

Hi @Bhargav,

this should be possible over the events. Every method in your actions that accept

tracker:Tracker

is able to provide you with all recent events. A sample would be:

 for event in tracker.events:
    <your code>

If the event contained extracted entities, you will be able to access them this way.

Tell me, if you need more help.

Regards

HI @JulianGerhard,

Thank you very much for the reply. Could you please explain me how to use these event to retrieve entities. It would help me a lot.

Hi @Bhargav,

no problem. Let’s assume you have a function print_given_entities which should be executed in every validation method of your actions.py.

This would look like:

def print_given_entities(self, latest_message, dispatcher, tracker, domain):
    # the tracker got the information about recent events 
    for event in tracker.events:
        # if there is further data bein parsed
        if 'parse_data' in event:
            # is only doing something if there actually are entities
            for entity in event['parse_data']['entities']:
                logger.debug('Found entity: {}'.format(entity))

if you call this method e.g. in one of your FormActions and kept in mind to provide the expected arguments, then the method will give you what you want to have.

If there are problems with the code, don’t hesitate to ask.

Regards

Hi @JulianGerhard

Thank you very much for the quick response. I will try this and let you know.

Thanks,