From 2810eb29af292fd91cd622757510e44d0560e1eb Mon Sep 17 00:00:00 2001 From: Franklin Nunez <69214580+b-loved-dreamer@users.noreply.github.com> Date: Fri, 15 Oct 2021 12:19:11 -0700 Subject: [PATCH] docs(samples): adds list training phrases sample (#336) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs(samples): adds list training phrases sample * docs(samples): removed testing resources * docs(samples): removed testing resources * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- README.md | 1 + .../dialogflow/cx/ListTrainingPhrases.java | 62 +++++++++++++++++++ .../cx/ListTrainingPhrasesTest.java | 53 ++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java create mode 100644 samples/snippets/src/test/java/dialogflow/cx/ListTrainingPhrasesTest.java diff --git a/README.md b/README.md index 7d4e8bb0b..4dce28ccd 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-dialogflow-cx | Detect Intent Stream | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java) | | List Pages | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/ListPages.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/ListPages.java) | | List Test Case Results | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java) | +| List Training Phrases | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java) | | Update Intent | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java) | diff --git a/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java b/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java new file mode 100644 index 000000000..2bd0e73ae --- /dev/null +++ b/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java @@ -0,0 +1,62 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dialogflow.cx; + +// [START dialogflow_list_training_phrases] +import com.google.cloud.dialogflow.cx.v3.GetIntentRequest; +import com.google.cloud.dialogflow.cx.v3.Intent; +import com.google.cloud.dialogflow.cx.v3.IntentName; +import com.google.cloud.dialogflow.cx.v3.IntentsClient; +import java.io.IOException; +import java.util.List; + +public class ListTrainingPhrases { + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "location"; + String agentId = "my-agent-id"; + String intentId = "my-intent-id"; + + listTrainingPhrases(projectId, location, agentId, intentId); + } + + // DialogFlow API List Training Phrases sample. + public static void listTrainingPhrases( + String projectId, String location, String agentId, String intentId) throws IOException { + try (IntentsClient client = IntentsClient.create()) { + // Set the intent name + IntentName name = IntentName.of(projectId, location, agentId, intentId); + + // Compose the get-intent request + GetIntentRequest request = GetIntentRequest.newBuilder().setName(name.toString()).build(); + + // Make API request to get intent + Intent response = client.getIntent(request); + + // Loop through the results + for (Intent.TrainingPhrase phrase : response.getTrainingPhrasesList()) { + System.out.println("***********************************************"); + List parts = phrase.getPartsList(); + for (Intent.TrainingPhrase.Part part : parts) { + System.out.println(String.format("Training Phrase: %s", part.getText())); + } + } + } + } +} +// [END dialogflow_list_training_phrases] diff --git a/samples/snippets/src/test/java/dialogflow/cx/ListTrainingPhrasesTest.java b/samples/snippets/src/test/java/dialogflow/cx/ListTrainingPhrasesTest.java new file mode 100644 index 000000000..e3389ed16 --- /dev/null +++ b/samples/snippets/src/test/java/dialogflow/cx/ListTrainingPhrasesTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dialogflow.cx; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ListTrainingPhrasesTest { + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String LOCATION = "global"; + private static String AGENT_ID = "b8d0e85d-0741-4e6d-a66a-3671184b7b93"; + private static String INTENT_ID = "45974f75-9412-445a-9863-47bfdfa3d96d"; + + private ByteArrayOutputStream stdOut; + + @Before + public void setUp() throws IOException { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + } + + @After + public void tearDown() throws IOException { + stdOut = null; + System.setOut(null); + } + + @Test + public void testListTrainingPhrases() throws IOException { + ListTrainingPhrases.listTrainingPhrases(PROJECT_ID, LOCATION, AGENT_ID, INTENT_ID); + assertThat(stdOut.toString()).contains("green"); + } +}