0

I followed this tutorial to write a simple script using Wit.ai.

So, there is a code snippet which retrieves the entity from the first message:

def first_entity_value(entities, entity):
    if entity not in entities:
        return None
    val = entities[entity][0]['value']
    if not val:
        return None
    return val['value'] if isinstance(val, dict) else val

I have two questions:

  1. How can I get entities from the other messages? So, when user type something (not as a first message)?
  2. I have multiple entities in the message (e.g: I'm gonna visit London this weekend), how can I get, for example, the second entity (weekend)? Now I tried to write something like the following but got an error:

    def first_entity_value(entities, entity):
        if entity not in entities:
            return None
        val = entities[entity][0][1]['value'] # to get the second entity
        if not val:
            return None
        return val['value'] if isinstance(val, dict) else val
    
Faheel
  • 2,435
  • 24
  • 33
Alex Ermolaev
  • 311
  • 2
  • 4
  • 17

1 Answers1

1

"London" is a location and "weekend" is a datetime. They are not the same entity.

To retrieve both entities, just adapt the entity argument:

city = first_entity_value(entities, 'location')
date = first_entity_value(entities, 'datetime')

If you want to retrieve two values of the same entity (e.g: I love Paris and London), then you should use the method you tried:

def get_entity_value(entities, entity, pos):
    if entity not in entities:
        return None
    val = entities[entity][pos]['value'] # to get the entity at "pos"
    if not val:
        return None
    return val['value'] if isinstance(val, dict) else val

I don't really get your first question. The selected action(s) (from Wit converse) is/are run every time you receive a message from the user.

Nitsuja
  • 73
  • 6
  • Ok, I have two entities (datetime and location). I wrote as you mentioned (city = first_entity_value(entities, 'location') date = first_entity_value(entities, 'datetime') and got the following error: val = entities[entity][0]['value'] KeyError: 'value') – Alex Ermolaev Nov 18 '16 at 13:18
  • Did you restablish "val = entities[entity][0]['value']" in the first_entity_value function ? – Nitsuja Nov 18 '16 at 13:32
  • I left it the same val = entities[entity][0]['value'] – Alex Ermolaev Nov 18 '16 at 14:55
  • I put it in gist: https://gist.github.com/ermolushka/a72145b469369b1377861e865b3c5168 – Alex Ermolaev Nov 18 '16 at 15:25
  • Try to print the entities dictionnary, this will probably show what's wrong. – Nitsuja Nov 18 '16 at 15:31
  • I wrote the output in the comment (https://gist.github.com/ermolushka/a72145b469369b1377861e865b3c5168), but I see, that there are location and datetime in the response. – Alex Ermolaev Nov 18 '16 at 15:38
  • Indeed, it seems to met that there is no "value" field for wit/datetime when the specified input is an interval. "This weekend" corresponds to an interval. Instead, the field is called "values" and contains intervals. – Nitsuja Nov 18 '16 at 16:01