Skip to content

Commit 17cf4f4

Browse files
authored
samples: create custom highlights (#82)
1 parent f7bf8eb commit 17cf4f4

File tree

4 files changed

+320
-0
lines changed

4 files changed

+320
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2021 Google Inc.
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.contactcenterinsights;
18+
19+
// [START contactcenterinsights_create_phrase_matcher_all_of]
20+
21+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
22+
import com.google.cloud.contactcenterinsights.v1.ExactMatchConfig;
23+
import com.google.cloud.contactcenterinsights.v1.LocationName;
24+
import com.google.cloud.contactcenterinsights.v1.PhraseMatchRule;
25+
import com.google.cloud.contactcenterinsights.v1.PhraseMatchRuleConfig;
26+
import com.google.cloud.contactcenterinsights.v1.PhraseMatchRuleGroup;
27+
import com.google.cloud.contactcenterinsights.v1.PhraseMatcher;
28+
import java.io.IOException;
29+
30+
public class CreatePhraseMatcherAllOf {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace this variable before running the sample.
34+
String projectId = "my_project_id";
35+
36+
createPhraseMatcherAllOf(projectId);
37+
}
38+
39+
public static PhraseMatcher createPhraseMatcherAllOf(String projectId) throws IOException {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the "close" method on the client to safely clean up any remaining background resources.
43+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
44+
// Construct a phrase matcher that matches all of its rule groups.
45+
PhraseMatcher.Builder phraseMatcher =
46+
PhraseMatcher.newBuilder()
47+
.setDisplayName("NON_SHIPPING_PHONE_SERVICE")
48+
.setTypeValue(1)
49+
.setActive(true);
50+
51+
// Construct a rule group to match the word "PHONE" or "CELLPHONE", ignoring case sensitivity.
52+
PhraseMatchRuleGroup.Builder ruleGroup1 = PhraseMatchRuleGroup.newBuilder().setTypeValue(2);
53+
54+
String[] words1 = {"PHONE", "CELLPHONE"};
55+
for (String w : words1) {
56+
PhraseMatchRule.Builder rule =
57+
PhraseMatchRule.newBuilder()
58+
.setQuery(w)
59+
.setConfig(
60+
PhraseMatchRuleConfig.newBuilder()
61+
.setExactMatchConfig(ExactMatchConfig.newBuilder().build())
62+
.build());
63+
ruleGroup1.addPhraseMatchRules(rule.build());
64+
}
65+
phraseMatcher.addPhraseMatchRuleGroups(ruleGroup1.build());
66+
67+
// Construct another rule group to not match the word "SHIPPING" or "DELIVERY",
68+
// ignoring case sensitivity.
69+
PhraseMatchRuleGroup.Builder ruleGroup2 = PhraseMatchRuleGroup.newBuilder().setTypeValue(1);
70+
71+
String[] words2 = {"SHIPPING", "DELIVERY"};
72+
for (String w : words2) {
73+
PhraseMatchRule.Builder rule =
74+
PhraseMatchRule.newBuilder()
75+
.setQuery(w)
76+
.setNegated(true)
77+
.setConfig(
78+
PhraseMatchRuleConfig.newBuilder()
79+
.setExactMatchConfig(ExactMatchConfig.newBuilder().build())
80+
.build());
81+
ruleGroup2.addPhraseMatchRules(rule.build());
82+
}
83+
phraseMatcher.addPhraseMatchRuleGroups(ruleGroup2.build());
84+
85+
// Construct a parent resource.
86+
LocationName parent = LocationName.of(projectId, "us-central1");
87+
88+
// Call the Insights client to create a phrase matcher.
89+
PhraseMatcher response = client.createPhraseMatcher(parent, phraseMatcher.build());
90+
System.out.printf("Created %s%n", response.getName());
91+
return response;
92+
}
93+
}
94+
}
95+
96+
// [END contactcenterinsights_create_phrase_matcher_all_of]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2021 Google Inc.
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.contactcenterinsights;
18+
19+
// [START contactcenterinsights_create_phrase_matcher_any_of]
20+
21+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
22+
import com.google.cloud.contactcenterinsights.v1.ExactMatchConfig;
23+
import com.google.cloud.contactcenterinsights.v1.LocationName;
24+
import com.google.cloud.contactcenterinsights.v1.PhraseMatchRule;
25+
import com.google.cloud.contactcenterinsights.v1.PhraseMatchRuleConfig;
26+
import com.google.cloud.contactcenterinsights.v1.PhraseMatchRuleGroup;
27+
import com.google.cloud.contactcenterinsights.v1.PhraseMatcher;
28+
import java.io.IOException;
29+
30+
public class CreatePhraseMatcherAnyOf {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace this variable before running the sample.
34+
String projectId = "my_project_id";
35+
36+
createPhraseMatcherAnyOf(projectId);
37+
}
38+
39+
public static PhraseMatcher createPhraseMatcherAnyOf(String projectId) throws IOException {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the "close" method on the client to safely clean up any remaining background resources.
43+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
44+
// Construct a phrase matcher that matches any of its rule groups.
45+
PhraseMatcher.Builder phraseMatcher =
46+
PhraseMatcher.newBuilder()
47+
.setDisplayName("PHONE_SERVICE")
48+
.setTypeValue(2)
49+
.setActive(true);
50+
51+
// Construct a rule group to match the word "PHONE" or "CELLPHONE", ignoring case sensitivity.
52+
PhraseMatchRuleGroup.Builder ruleGroup = PhraseMatchRuleGroup.newBuilder().setTypeValue(2);
53+
54+
String[] words = {"PHONE", "CELLPHONE"};
55+
for (String w : words) {
56+
PhraseMatchRule.Builder rule =
57+
PhraseMatchRule.newBuilder()
58+
.setQuery(w)
59+
.setConfig(
60+
PhraseMatchRuleConfig.newBuilder()
61+
.setExactMatchConfig(ExactMatchConfig.newBuilder().build())
62+
.build());
63+
ruleGroup.addPhraseMatchRules(rule.build());
64+
}
65+
phraseMatcher.addPhraseMatchRuleGroups(ruleGroup.build());
66+
67+
// Construct a parent resource.
68+
LocationName parent = LocationName.of(projectId, "us-central1");
69+
70+
// Call the Insights client to create a phrase matcher.
71+
PhraseMatcher response = client.createPhraseMatcher(parent, phraseMatcher.build());
72+
System.out.printf("Created %s%n", response.getName());
73+
return response;
74+
}
75+
}
76+
}
77+
78+
// [END contactcenterinsights_create_phrase_matcher_any_of]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2021 Google Inc.
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.contactcenterinsights;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
23+
import com.google.cloud.contactcenterinsights.v1.PhraseMatcher;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
33+
34+
@RunWith(JUnit4.class)
35+
public class CreatePhraseMatcherAllOfIT {
36+
37+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
private String phraseMatcherName;
41+
42+
private static void requireEnvVar(String varName) {
43+
assertNotNull(String.format(varName), String.format(varName));
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
49+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
}
51+
52+
@Before
53+
public void setUp() {
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@After
60+
public void tearDown() throws IOException {
61+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
62+
client.deletePhraseMatcher(phraseMatcherName);
63+
}
64+
System.setOut(null);
65+
}
66+
67+
@Test
68+
public void testCreatePhraseMatcherAllOf() throws IOException {
69+
PhraseMatcher phraseMatcher = CreatePhraseMatcherAllOf.createPhraseMatcherAllOf(PROJECT_ID);
70+
phraseMatcherName = phraseMatcher.getName();
71+
assertThat(bout.toString()).contains(phraseMatcherName);
72+
}
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2021 Google Inc.
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.contactcenterinsights;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
23+
import com.google.cloud.contactcenterinsights.v1.PhraseMatcher;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
33+
34+
@RunWith(JUnit4.class)
35+
public class CreatePhraseMatcherAnyOfIT {
36+
37+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
private String phraseMatcherName;
41+
42+
private static void requireEnvVar(String varName) {
43+
assertNotNull(String.format(varName), String.format(varName));
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
49+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
}
51+
52+
@Before
53+
public void setUp() {
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@After
60+
public void tearDown() throws IOException {
61+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
62+
client.deletePhraseMatcher(phraseMatcherName);
63+
}
64+
System.setOut(null);
65+
}
66+
67+
@Test
68+
public void testCreatePhraseMatcherAnyOf() throws IOException {
69+
PhraseMatcher phraseMatcher = CreatePhraseMatcherAnyOf.createPhraseMatcherAnyOf(PROJECT_ID);
70+
phraseMatcherName = phraseMatcher.getName();
71+
assertThat(bout.toString()).contains(phraseMatcherName);
72+
}
73+
}

0 commit comments

Comments
 (0)