Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit 2810eb2

Browse files
docs(samples): adds list training phrases sample (#336)
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 27e724d commit 2810eb2

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-dialogflow-cx
8989
| 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) |
9090
| 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) |
9191
| 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) |
92+
| 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) |
9293
| 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) |
9394

9495

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
// [START dialogflow_list_training_phrases]
20+
import com.google.cloud.dialogflow.cx.v3.GetIntentRequest;
21+
import com.google.cloud.dialogflow.cx.v3.Intent;
22+
import com.google.cloud.dialogflow.cx.v3.IntentName;
23+
import com.google.cloud.dialogflow.cx.v3.IntentsClient;
24+
import java.io.IOException;
25+
import java.util.List;
26+
27+
public class ListTrainingPhrases {
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "my-project-id";
31+
String location = "location";
32+
String agentId = "my-agent-id";
33+
String intentId = "my-intent-id";
34+
35+
listTrainingPhrases(projectId, location, agentId, intentId);
36+
}
37+
38+
// DialogFlow API List Training Phrases sample.
39+
public static void listTrainingPhrases(
40+
String projectId, String location, String agentId, String intentId) throws IOException {
41+
try (IntentsClient client = IntentsClient.create()) {
42+
// Set the intent name
43+
IntentName name = IntentName.of(projectId, location, agentId, intentId);
44+
45+
// Compose the get-intent request
46+
GetIntentRequest request = GetIntentRequest.newBuilder().setName(name.toString()).build();
47+
48+
// Make API request to get intent
49+
Intent response = client.getIntent(request);
50+
51+
// Loop through the results
52+
for (Intent.TrainingPhrase phrase : response.getTrainingPhrasesList()) {
53+
System.out.println("***********************************************");
54+
List<Intent.TrainingPhrase.Part> parts = phrase.getPartsList();
55+
for (Intent.TrainingPhrase.Part part : parts) {
56+
System.out.println(String.format("Training Phrase: %s", part.getText()));
57+
}
58+
}
59+
}
60+
}
61+
}
62+
// [END dialogflow_list_training_phrases]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class ListTrainingPhrasesTest {
29+
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
30+
private static String LOCATION = "global";
31+
private static String AGENT_ID = "b8d0e85d-0741-4e6d-a66a-3671184b7b93";
32+
private static String INTENT_ID = "45974f75-9412-445a-9863-47bfdfa3d96d";
33+
34+
private ByteArrayOutputStream stdOut;
35+
36+
@Before
37+
public void setUp() throws IOException {
38+
stdOut = new ByteArrayOutputStream();
39+
System.setOut(new PrintStream(stdOut));
40+
}
41+
42+
@After
43+
public void tearDown() throws IOException {
44+
stdOut = null;
45+
System.setOut(null);
46+
}
47+
48+
@Test
49+
public void testListTrainingPhrases() throws IOException {
50+
ListTrainingPhrases.listTrainingPhrases(PROJECT_ID, LOCATION, AGENT_ID, INTENT_ID);
51+
assertThat(stdOut.toString()).contains("green");
52+
}
53+
}

0 commit comments

Comments
 (0)