Skip to content

Commit 23e645b

Browse files
docs(sample): adds new list training phrases sample (#742)
1 parent b38cc42 commit 23e645b

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 com.example.dialogflow;
18+
19+
// [START dialogflow_list_training_phrases]
20+
import com.google.cloud.dialogflow.v2.GetIntentRequest;
21+
import com.google.cloud.dialogflow.v2.Intent;
22+
import com.google.cloud.dialogflow.v2.IntentName;
23+
import com.google.cloud.dialogflow.v2.IntentView;
24+
import com.google.cloud.dialogflow.v2.IntentsClient;
25+
import java.io.IOException;
26+
import java.util.List;
27+
28+
public class ListTrainingPhrases {
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my-project-id";
32+
String intentId = "my-intent-id";
33+
34+
listTrainingPhrases(projectId, intentId);
35+
}
36+
37+
// DialogFlow API List Training Phrases sample.
38+
public static void listTrainingPhrases(String projectId, String intentId) throws IOException {
39+
try (IntentsClient client = IntentsClient.create()) {
40+
// Set the intent name
41+
IntentName name = IntentName.of(projectId, intentId);
42+
43+
// Compose the get-intent request
44+
GetIntentRequest request =
45+
GetIntentRequest.newBuilder()
46+
.setName(name.toString())
47+
.setIntentView(IntentView.INTENT_VIEW_FULL)
48+
.build();
49+
50+
// Make API request to update intent
51+
Intent response = client.getIntent(request);
52+
53+
// Loop through the results
54+
for (Intent.TrainingPhrase phrase : response.getTrainingPhrasesList()) {
55+
System.out.println("***********************************************");
56+
System.out.println(String.format("Phrase ID: %s", phrase.getName()));
57+
List<Intent.TrainingPhrase.Part> parts = phrase.getPartsList();
58+
for (Intent.TrainingPhrase.Part part : parts) {
59+
System.out.println(String.format("Training Phrase: %s", part.getText()));
60+
}
61+
}
62+
}
63+
}
64+
}
65+
// [END dialogflow_list_training_phrases]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 com.example.dialogflow;
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 intentID = "875138fa-991f-4c2b-b58c-dcbad1638556";
31+
32+
private ByteArrayOutputStream stdOut;
33+
34+
@Before
35+
public void setUp() throws IOException {
36+
stdOut = new ByteArrayOutputStream();
37+
System.setOut(new PrintStream(stdOut));
38+
}
39+
40+
@After
41+
public void tearDown() throws IOException {
42+
stdOut = null;
43+
System.setOut(null);
44+
}
45+
46+
@Test
47+
public void testListTrainingPhrases() throws IOException {
48+
ListTrainingPhrases.listTrainingPhrases(PROJECT_ID, intentID);
49+
assertThat(stdOut.toString()).contains("What date?");
50+
}
51+
}

0 commit comments

Comments
 (0)