按照Dialogflow tutorial的步骤,我 Build 了天气预报代理 . 在列表中添加训练短语时,会自动检测词性并将其突出显示为 entities .
我的目标是通过Dialogflow v2 Java SDK重现该行为 .
因此,从Dialogflow v2 samples中创建代理的方式中获取愿望,我编写了一个方法来向Intent训练短语列表添加 List
训练短语 .
public static void addTrainingPhrases(List <String> phrases, String intentDisplayName, String projectId) {
try {
IntentsClient ic=IntentsClient.create();
/*selectIntent(displayName,projectId) is only a method I defined to
get the intent more easily */
Intent intent=IntentManagement.selectIntent(intentDisplayName, projectId);
//Mask to fire only the part to modify
FieldMask mask=FieldMask.newBuilder().addPaths("training_phrases").build();
// Build the trainingPhrases from the trainingPhrasesParts
List<TrainingPhrase> trainingPhrases = new ArrayList<TrainingPhrase>(intent.getTrainingPhrasesList());
for (String trainingPhrase : phrases) {
trainingPhrases.add(
TrainingPhrase.newBuilder().addParts(
Part.newBuilder()
.setText(trainingPhrase)
.build())
.build());
}
//Build the patch Intent
intent=Intent.newBuilder(intent)
.addAllTrainingPhrases(trainingPhrases)
.build();
//Build the update request
UpdateIntentRequest request=UpdateIntentRequest.newBuilder()
.setIntent(intent)
.setIntentView(IntentView.INTENT_VIEW_FULL)
.setLanguageCode("en")
.setUpdateMask(mask)
.build();
//API call to patch the previous Intent
Intent response=ic.updateIntent(request);
System.out.format("Intent updated: %s\n", response);
} catch (NullPointerException e) {
System.out.println("No intent "+intentDisplayName+" defined in project "+projectId);
} catch (IOException e) {
System.out.println("An error has occurred");
}
}
这些短语被正确地添加到列表中,但仅仅是字符串短语,即在控制台教程中没有检测到实体 .
我知道一个可能的答案是修改方法,手动传递一个预先构建的Part列表,其中分别设置了 entityType
和 alias
字段,并带有实体名称和相关参数名称,但它是's not what I' m .
相反,我需要客户的这种努力 .