Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
docs(sample): adds new list training phrases sample (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-loved-dreamer committed Oct 11, 2021
1 parent a545bb0 commit 10b4f32
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -116,6 +116,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-dialogflow/tr
| Document Management | [source code](https://github.com/googleapis/java-dialogflow/blob/main/samples/snippets/src/main/java/com/example/dialogflow/DocumentManagement.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/dialogflow/DocumentManagement.java) |
| Intent Management | [source code](https://github.com/googleapis/java-dialogflow/blob/main/samples/snippets/src/main/java/com/example/dialogflow/IntentManagement.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/dialogflow/IntentManagement.java) |
| Knowledge Base Management | [source code](https://github.com/googleapis/java-dialogflow/blob/main/samples/snippets/src/main/java/com/example/dialogflow/KnowledgeBaseManagement.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/dialogflow/KnowledgeBaseManagement.java) |
| List Training Phrases | [source code](https://github.com/googleapis/java-dialogflow/blob/main/samples/snippets/src/main/java/com/example/dialogflow/ListTrainingPhrases.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/dialogflow/ListTrainingPhrases.java) |
| Update Intent | [source code](https://github.com/googleapis/java-dialogflow/blob/main/samples/snippets/src/main/java/com/example/dialogflow/UpdateIntent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/dialogflow/UpdateIntent.java) |


Expand Down
@@ -0,0 +1,65 @@
/*
* 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 com.example.dialogflow;

// [START dialogflow_list_training_phrases]
import com.google.cloud.dialogflow.v2.GetIntentRequest;
import com.google.cloud.dialogflow.v2.Intent;
import com.google.cloud.dialogflow.v2.IntentName;
import com.google.cloud.dialogflow.v2.IntentView;
import com.google.cloud.dialogflow.v2.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 intentId = "my-intent-id";

listTrainingPhrases(projectId, intentId);
}

// DialogFlow API List Training Phrases sample.
public static void listTrainingPhrases(String projectId, String intentId) throws IOException {
try (IntentsClient client = IntentsClient.create()) {
// Set the intent name
IntentName name = IntentName.of(projectId, intentId);

// Compose the get-intent request
GetIntentRequest request =
GetIntentRequest.newBuilder()
.setName(name.toString())
.setIntentView(IntentView.INTENT_VIEW_FULL)
.build();

// Make API request to update intent
Intent response = client.getIntent(request);

// Loop through the results
for (Intent.TrainingPhrase phrase : response.getTrainingPhrasesList()) {
System.out.println("***********************************************");
System.out.println(String.format("Phrase ID: %s", phrase.getName()));
List<Intent.TrainingPhrase.Part> parts = phrase.getPartsList();
for (Intent.TrainingPhrase.Part part : parts) {
System.out.println(String.format("Training Phrase: %s", part.getText()));
}
}
}
}
}
// [END dialogflow_list_training_phrases]
@@ -0,0 +1,51 @@
/*
* 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 com.example.dialogflow;

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 intentID = "875138fa-991f-4c2b-b58c-dcbad1638556";

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, intentID);
assertThat(stdOut.toString()).contains("What date?");
}
}

0 comments on commit 10b4f32

Please sign in to comment.