我想使用Dialogflow java-sdk创建一个意图。问题是我能够创建训练短语和槽位,但无法像在dialogflow控制台中那样将特定单词分配到训练短语。

我已经检查过,并且Dialogflow本身也不做。
下面,我写了我当前正在使用的代码和所使用的功能。这可能无济于事,但可能有助于在我正在使用的当前方法中提供信息。

dialogflow java api中是否有任何功能可以帮助我实现这一目标。

public  OutputResponseDTO createIntent(CreateIntentInputDTO createIntentInputDTO)
{
    // Instantiates a client
    try (IntentsClient intentsClient = IntentsClient.create()) {

        // Set the project agent name using the projectID (my-project-id)
        ProjectAgentName parent = ProjectAgentName.of(createIntentInputDTO.getProjectId());

        //first check if intent already exists
        for (Intent intent : intentsClient.listIntents(parent).iterateAll()) {
            //logger.error(intent.getDisplayName()+"Inside delete!!!!!!"+createIntentInputDTO.getIntentName());
            if (intent.getDisplayName().equals(createIntentInputDTO.getDisplayName())) {
                logger.error("Inside delete!!!!!!");
                deleteIntent(createIntentInputDTO.getIntentName(),createIntentInputDTO.getProjectId());
            }
        }

        List<String> trainingPhrasesIP=new ArrayList<String>();
        for(int l=0;l<createIntentInputDTO.getTrainingPhrasesParts().size();l++)
        {
            trainingPhrasesIP.add(createIntentInputDTO.getTrainingPhrasesParts().get(l).getPhraseName());
        }
        // Build the trainingPhrases from the trainingPhrasesParts
        List<TrainingPhrase> trainingPhrases = new ArrayList<>();
        for (String trainingPhrase : trainingPhrasesIP) {
            trainingPhrases.add(
                    TrainingPhrase.newBuilder().addParts(
                            Part.newBuilder().setText(trainingPhrase).build())
                    .build());
        }

        // Build the message texts for the agent's response
        Message messages = Message.newBuilder()
                .setText(
                        Text.newBuilder()
                        .addAllText(createIntentInputDTO.getMessageTexts()).build()
                        ).build();



        List<Parameter> parameters=new ArrayList<Intent.Parameter>();

        for(int j=0;j<createIntentInputDTO.getSlotsInputDTOs().size();j++)
        {

            //String firstFourChars = createIntentInputDTO.getSlotsInputDTOs().get(j).getEntityDisplayName().substring(0, 5);
            //System.out.println(firstFourChars);

            String paraNameWithoutAtTwo;
            String paraNameWithoutAtOne;
            String paraNameWithoutAtAndWithDollar;
            String paraNameWithAt;

            try
            {
                paraNameWithoutAtOne=createIntentInputDTO.getSlotsInputDTOs().get(j).getEntityDisplayName().replace("@sys.", "");
             paraNameWithoutAtTwo=paraNameWithoutAtOne.replace("@", "");
            }
            catch(Exception e)
            {
                paraNameWithoutAtTwo=createIntentInputDTO.getSlotsInputDTOs().get(j).getEntityDisplayName();
                logger.info("Something happended when removing stuff");
            }

            paraNameWithoutAtAndWithDollar="$"+paraNameWithoutAtTwo;
            paraNameWithAt="@"+paraNameWithoutAtTwo;
            paraNameWithoutAt=createIntentInputDTO.getSlotsInputDTOs().get(j).getEntityDisplayName();



            Parameter parameter=Parameter.newBuilder()
                .setValue(createIntentInputDTO.getSlotsInputDTOs().get(j).getValue())
                    .setDisplayName(paraNameWithoutAtTwo)
                    .setValue(paraNameWithoutAtAndWithDollar)


                    .setEntityTypeDisplayName(createIntentInputDTO.getSlotsInputDTOs().get(j).getEntityDisplayName())
                    .build();
            parameters.add(parameter);
        }




        // Build the intent
        Intent intent = Intent.newBuilder()
                .setDisplayName(createIntentInputDTO.getDisplayName())
                .addMessages(messages)
                //                  .addParameters(p)
                                    .addAllParameters(parameters)
                .addAllTrainingPhrases(trainingPhrases)
                .build();




        // Performs the create intent request
        Intent response = intentsClient.createIntent(parent, intent);

    //  System.out.format("Intent created: %s\n", response);


        try
        {
            IntentMasterCollection intentMasterCollection=new IntentMasterCollection();
            intentMasterCollection.setName(createIntentInputDTO.getDisplayName());
            intentActionMaster.save(intentMasterCollection);
        }
        catch(Exception e)
        {
            logger.error("Error adding in DB intent master");
        }

        try
        {
            //we gonna put the data again!(Training phrases only)
            String[] splitName = response.getName().split("/");
            //              intentIds.add(splitName[splitName.length - 1]);



            //finally setting all the values

            updateIntent(splitName[splitName.length - 1]);

        }
        catch(Exception e)
        {
            logger.error("Error in writing to intent");
        }
        return new OutputResponseDTO(true, message.getStatusCode("success.message.createIntent.id"),
                message.get("success.message.createIntent"), null,  null,requestBean.getTraceId());
    }
    catch(Exception e)
    {
        logger.error("Error Creating Intent:"+e.getMessage());
        return new OutputResponseDTO(false, message.getStatusCode("error.message.generalError.id"),
                message.get("error.message.generalError"), null,  e.getMessage(),requestBean.getTraceId());
    }
}

最佳答案

我认为您遇到的问题与零件定义有关。
每个训练短语都分为几个部分,您可以为每个部分定义它仅仅是文本还是包含一个实体:

        Part part1 = Part.newBuilder().setText("I want to cancel a card ended in  ").build();
        Part part2 = Part.newBuilder().setText("5123").setEntityType("@cardNumber").setAlias("cardNumber")
                .setUserDefined(true).build();
        List<Part> parts = new ArrayList<Part>();
        parts.add(part1);
        parts.add(part2);


然后,您构建训练短语:

    TrainingPhrase tp = TrainingPhrase.newBuilder().setType(Type.EXAMPLE).addAllParts(parts).build();

07-26 03:24