Search in sources :

Example 1 with DocumentName

use of com.google.cloud.dialogflow.v2beta1.DocumentName in project java-dialogflow by googleapis.

the class DetectIntentKnowledgeTest method testDetectIntentKnowledge.

@Test
public void testDetectIntentKnowledge() throws Exception {
    KnowledgeBaseName knowledgeBaseName = KnowledgeBaseName.newBuilder().setProject(PROJECT_ID).setKnowledgeBase(TEST_KNOWLEDGE_BASE_ID).build();
    DocumentName documentName = DocumentName.newBuilder().setProject(PROJECT_ID).setKnowledgeBase(TEST_KNOWLEDGE_BASE_ID).setDocument(TEST_DOCUMENT_ID).build();
    Map<String, KnowledgeAnswers> allAnswers = DetectIntentKnowledge.detectIntentKnowledge(PROJECT_ID, knowledgeBaseName.toString(), SESSION_ID, LANGUAGE_CODE, TEXTS);
    assertEquals(TEXTS.size(), allAnswers.size());
    int answersFound = 0;
    for (String text : TEXTS) {
        KnowledgeAnswers knowledgeAnswers = allAnswers.get(text);
        if (knowledgeAnswers.getAnswersCount() > 0) {
            Answer answer = knowledgeAnswers.getAnswers(0);
            if (text.equals(answer.getFaqQuestion()) && documentName.toString().equals(answer.getSource())) {
                answersFound++;
            }
        }
    }
    // To make the test less flaky, check that half of the texts got a result.
    assertThat(answersFound).isGreaterThan(TEXTS.size() / 2);
}
Also used : Answer(com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers.Answer) KnowledgeBaseName(com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName) DocumentName(com.google.cloud.dialogflow.v2beta1.DocumentName) KnowledgeAnswers(com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers) Test(org.junit.Test)

Aggregations

DocumentName (com.google.cloud.dialogflow.v2beta1.DocumentName)1 KnowledgeAnswers (com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers)1 Answer (com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers.Answer)1 KnowledgeBaseName (com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName)1 Test (org.junit.Test)1