2
votes

I was wondering how I could get the automatic mapping between training phrases and parameters. When you simply type in "school" into training phrase and you have an entity with the same value you get an automatic mapping (see here after I added school as a training phrase I got an automatic mapping to the entity @school https://i.imgur.com/uY8Mq0S.png).

I want this but I am using the python API to insert new intents. Is there any way of doing this, or do I need to manually check if any of the words matches an entity and then manually creating that parameter for that intent? Here's a snippit of the code im using.

import dialogflow_v2beta1

client = dialogflow_v2beta1.IntentsClient()
parent = client.project_agent_path('[project]')

intent = {
    "display_name": "test",
    "webhook_state": True,
    "training_phrases": [{"parts": [{"text": "school", "entity_type": "@school"}], "type": "EXAMPLE"}],
    "parameters": [{"display_name": "school", "entity_type_display_name": "@school", "value": "$school"}]
}

response = client.create_intent(parent, intent)

Thank you for reading :)

2
i had similar problem, could not found appropriate solution - sid8491

2 Answers

1
votes

Training phrase entity annotation is a feature of the Dialogflow UI and is not available in the API.

You need to manually annotate entities in your training phrases as you have already detailed in your questions.

-1
votes

Here is a code that can do what you want

def create_annotated_intent(project_id, display_name, training_phrases_parts,
                          action, mapped_entities, message_texts):
    """Create an intent of the given intent type and parameters.
    :type entity_display_name: list

    """

    intents_client = dialogflow.IntentsClient()
    parent = intents_client.project_agent_path(project_id)
    training_phrases = []
    entity_display_name = mapped_entities.keys()

    for training_phrases_part in training_phrases_parts:
        parts = []
        mots = training_phrases_part.split(" ")
        for mot in mots:
            is_entity = False
            for entity_name in entity_display_name:
                if mot in mapped_entities[entity_name]:
                    parts.append(dialogflow.types.Intent.TrainingPhrase.Part(
                        text=mot, entity_type="@" + entity_name, alias=entity_name))
                    if mots.index(mot) != len(mots) - 1:
                        parts.append(dialogflow.types.Intent.TrainingPhrase.Part(
                            text=" "))
                    is_entity = True
                    break
            if not is_entity:
                if mots.index(mot) != len(mots) - 1:
                    parts.append(dialogflow.types.Intent.TrainingPhrase.Part(
                        text=mot + " "))
                else:
                    parts.append(dialogflow.types.Intent.TrainingPhrase.Part(
                        text=mot))

        # Here we create a new training phrase for each provided part.
        training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=parts)
        training_phrases.append(training_phrase)

    text = dialogflow.types.Intent.Message.Text(text=message_texts)
    message = dialogflow.types.Intent.Message(text=text)
    parameters = []

    for entity_name in entity_display_name:
        params = dialogflow.types.Intent.Parameter(display_name=entity_name,
                                                   value='$' + entity_name)
        params.entity_type_display_name = '@' + entity_name
        parameters.append(params)

    intent = dialogflow.types.Intent(
        display_name=display_name,
        action=action,
        parameters=parameters,
        training_phrases=training_phrases,
        messages=[message])

    response = intents_client.create_intent(parent, intent)

    print('Intent created: {}'.format(response))