Search in sources :

Example 16 with Intent

use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow by googleapis.

the class ITSystemTest method listIntentsTest.

@Test
public void listIntentsTest() {
    ListIntentsRequest request = ListIntentsRequest.newBuilder().setParent(PROJECT_AGENT_NAME.toString()).build();
    for (Intent actualIntent : intentsClient.listIntents(request).iterateAll()) {
        if (intent.getName().equals(actualIntent.getName())) {
            assertEquals(intent.getDisplayName(), actualIntent.getDisplayName());
            assertEquals(intent.getAction(), actualIntent.getAction());
            assertEquals(intent.getEvents(0), actualIntent.getEvents(0));
            assertEquals(intent.getEventsCount(), actualIntent.getEventsCount());
        }
    }
}
Also used : ListIntentsRequest(com.google.cloud.dialogflow.v2.ListIntentsRequest) Intent(com.google.cloud.dialogflow.v2.Intent) Test(org.junit.Test)

Example 17 with Intent

use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow by googleapis.

the class ITSystemTest method getIntentTest.

@Test
public void getIntentTest() {
    GetIntentRequest request = GetIntentRequest.newBuilder().setName(intentName.toString()).build();
    Intent actualIntent = intentsClient.getIntent(request);
    assertEquals(intent.getName(), actualIntent.getName());
    assertEquals(intent.getDisplayName(), actualIntent.getDisplayName());
    assertEquals(intent.getAction(), actualIntent.getAction());
    assertEquals(intent.getEvents(0), actualIntent.getEvents(0));
    assertEquals(intent.getEventsCount(), actualIntent.getEventsCount());
}
Also used : GetIntentRequest(com.google.cloud.dialogflow.v2.GetIntentRequest) Intent(com.google.cloud.dialogflow.v2.Intent) Test(org.junit.Test)

Example 18 with Intent

use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow by googleapis.

the class DetectIntentWithSentimentAnalysis method detectIntentSentimentAnalysis.

public static Map<String, QueryResult> detectIntentSentimentAnalysis(String projectId, List<String> texts, String sessionId, String languageCode) throws IOException, ApiException {
    Map<String, QueryResult> queryResults = Maps.newHashMap();
    // Instantiates a client
    try (SessionsClient sessionsClient = SessionsClient.create()) {
        // Set the session name using the sessionId (UUID) and projectID (my-project-id)
        SessionName session = SessionName.of(projectId, sessionId);
        System.out.println("Session Path: " + session.toString());
        // Detect intents for each text input
        for (String text : texts) {
            // Set the text (hello) and language code (en-US) for the query
            TextInput.Builder textInput = TextInput.newBuilder().setText(text).setLanguageCode(languageCode);
            // Build the query with the TextInput
            QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();
            // 
            SentimentAnalysisRequestConfig sentimentAnalysisRequestConfig = SentimentAnalysisRequestConfig.newBuilder().setAnalyzeQueryTextSentiment(true).build();
            QueryParameters queryParameters = QueryParameters.newBuilder().setSentimentAnalysisRequestConfig(sentimentAnalysisRequestConfig).build();
            DetectIntentRequest detectIntentRequest = DetectIntentRequest.newBuilder().setSession(session.toString()).setQueryInput(queryInput).setQueryParams(queryParameters).build();
            // Performs the detect intent request
            DetectIntentResponse response = sessionsClient.detectIntent(detectIntentRequest);
            // Display the query result
            QueryResult queryResult = response.getQueryResult();
            System.out.println("====================");
            System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
            System.out.format("Detected Intent: %s (confidence: %f)\n", queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
            System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentMessagesCount() > 0 ? queryResult.getFulfillmentMessages(0).getText() : "Triggered Default Fallback Intent");
            System.out.format("Sentiment Score: '%s'\n", queryResult.getSentimentAnalysisResult().getQueryTextSentiment().getScore());
            queryResults.put(text, queryResult);
        }
    }
    return queryResults;
}
Also used : DetectIntentResponse(com.google.cloud.dialogflow.v2.DetectIntentResponse) QueryInput(com.google.cloud.dialogflow.v2.QueryInput) QueryResult(com.google.cloud.dialogflow.v2.QueryResult) DetectIntentRequest(com.google.cloud.dialogflow.v2.DetectIntentRequest) SentimentAnalysisRequestConfig(com.google.cloud.dialogflow.v2.SentimentAnalysisRequestConfig) QueryParameters(com.google.cloud.dialogflow.v2.QueryParameters) SessionsClient(com.google.cloud.dialogflow.v2.SessionsClient) TextInput(com.google.cloud.dialogflow.v2.TextInput) SessionName(com.google.cloud.dialogflow.v2.SessionName)

Example 19 with Intent

use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow by googleapis.

the class DetectIntentWithTextToSpeechResponse method detectIntentWithTexttoSpeech.

public static Map<String, QueryResult> detectIntentWithTexttoSpeech(String projectId, List<String> texts, String sessionId, String languageCode) throws IOException, ApiException {
    Map<String, QueryResult> queryResults = Maps.newHashMap();
    // Instantiates a client
    try (SessionsClient sessionsClient = SessionsClient.create()) {
        // Set the session name using the sessionId (UUID) and projectID (my-project-id)
        SessionName session = SessionName.of(projectId, sessionId);
        System.out.println("Session Path: " + session.toString());
        // Detect intents for each text input
        for (String text : texts) {
            // Set the text (hello) and language code (en-US) for the query
            TextInput.Builder textInput = TextInput.newBuilder().setText(text).setLanguageCode(languageCode);
            // Build the query with the TextInput
            QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();
            // 
            OutputAudioEncoding audioEncoding = OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16;
            int sampleRateHertz = 16000;
            OutputAudioConfig outputAudioConfig = OutputAudioConfig.newBuilder().setAudioEncoding(audioEncoding).setSampleRateHertz(sampleRateHertz).build();
            DetectIntentRequest dr = DetectIntentRequest.newBuilder().setQueryInput(queryInput).setOutputAudioConfig(outputAudioConfig).setSession(session.toString()).build();
            // Performs the detect intent request
            DetectIntentResponse response = sessionsClient.detectIntent(dr);
            // Display the query result
            QueryResult queryResult = response.getQueryResult();
            System.out.println("====================");
            System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
            System.out.format("Detected Intent: %s (confidence: %f)\n", queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
            System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentMessagesCount() > 0 ? queryResult.getFulfillmentMessages(0).getText() : "Triggered Default Fallback Intent");
            queryResults.put(text, queryResult);
        }
    }
    return queryResults;
}
Also used : DetectIntentRequest(com.google.cloud.dialogflow.v2.DetectIntentRequest) OutputAudioEncoding(com.google.cloud.dialogflow.v2.OutputAudioEncoding) OutputAudioConfig(com.google.cloud.dialogflow.v2.OutputAudioConfig) SessionName(com.google.cloud.dialogflow.v2.SessionName) DetectIntentResponse(com.google.cloud.dialogflow.v2.DetectIntentResponse) QueryInput(com.google.cloud.dialogflow.v2.QueryInput) QueryResult(com.google.cloud.dialogflow.v2.QueryResult) SessionsClient(com.google.cloud.dialogflow.v2.SessionsClient) TextInput(com.google.cloud.dialogflow.v2.TextInput)

Example 20 with Intent

use of com.google.cloud.dialogflow.v2.Intent in project java-dialogflow by googleapis.

the class IntentManagement method listIntents.

// [START dialogflow_list_intents]
/**
 * List intents
 *
 * @param projectId Project/Agent Id.
 * @return Intents found.
 */
public static List<Intent> listIntents(String projectId) throws ApiException, IOException {
    List<Intent> intents = Lists.newArrayList();
    // Instantiates a client
    try (IntentsClient intentsClient = IntentsClient.create()) {
        // Set the project agent name using the projectID (my-project-id)
        AgentName parent = AgentName.of(projectId);
        // Performs the list intents request
        for (Intent intent : intentsClient.listIntents(parent).iterateAll()) {
            System.out.println("====================");
            System.out.format("Intent name: '%s'\n", intent.getName());
            System.out.format("Intent display name: '%s'\n", intent.getDisplayName());
            System.out.format("Action: '%s'\n", intent.getAction());
            System.out.format("Root followup intent: '%s'\n", intent.getRootFollowupIntentName());
            System.out.format("Parent followup intent: '%s'\n", intent.getParentFollowupIntentName());
            System.out.format("Input contexts:\n");
            for (String inputContextName : intent.getInputContextNamesList()) {
                System.out.format("\tName: %s\n", inputContextName);
            }
            System.out.format("Output contexts:\n");
            for (Context outputContext : intent.getOutputContextsList()) {
                System.out.format("\tName: %s\n", outputContext.getName());
            }
            intents.add(intent);
        }
    }
    return intents;
}
Also used : Context(com.google.cloud.dialogflow.v2.Context) IntentsClient(com.google.cloud.dialogflow.v2.IntentsClient) Intent(com.google.cloud.dialogflow.v2.Intent) AgentName(com.google.cloud.dialogflow.v2.AgentName)

Aggregations

Test (org.junit.Test)12 Intent (com.google.cloud.dialogflow.v2.Intent)10 IntentsClient (com.google.cloud.dialogflow.v2.IntentsClient)8 Intent (com.google.cloud.dialogflow.cx.v3beta1.Intent)7 QueryInput (com.google.cloud.dialogflow.v2.QueryInput)5 QueryResult (com.google.cloud.dialogflow.v2.QueryResult)5 SessionName (com.google.cloud.dialogflow.v2.SessionName)5 SessionsClient (com.google.cloud.dialogflow.v2.SessionsClient)5 AgentName (com.google.cloud.dialogflow.v2.AgentName)4 DetectIntentResponse (com.google.cloud.dialogflow.v2.DetectIntentResponse)4 ListIntentsPagedResponse (com.google.cloud.dialogflow.v2.IntentsClient.ListIntentsPagedResponse)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 Intent (com.google.cloud.dialogflow.cx.v3.Intent)3 IntentsClient (com.google.cloud.dialogflow.cx.v3.IntentsClient)3 TrainingPhrase (com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase)3 DetectIntentRequest (com.google.cloud.dialogflow.v2.DetectIntentRequest)3 TextInput (com.google.cloud.dialogflow.v2.TextInput)3 FieldMask (com.google.protobuf.FieldMask)3 ArrayList (java.util.ArrayList)3 IntentsClient (com.google.cloud.dialogflow.cx.v3beta1.IntentsClient)2