use of com.google.cloud.dialogflow.v2.OutputAudioEncoding 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;
}
Aggregations