Skip to content

Commit fbe592f

Browse files
authored
samples: create custom highlights (#15)
1 parent 61ad847 commit fbe592f

File tree

7 files changed

+259
-21
lines changed

7 files changed

+259
-21
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# [START contactcenterinsights_create_phrase_matcher_all_of]
16+
from google.cloud import contact_center_insights_v1
17+
18+
19+
def create_phrase_matcher_all_of(
20+
project_id: str,
21+
) -> contact_center_insights_v1.PhraseMatcher:
22+
# Construct a parent resource.
23+
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
24+
project_id, "us-central1"
25+
)
26+
27+
# Construct a phrase matcher that matches all of its rule groups.
28+
phrase_matcher = contact_center_insights_v1.PhraseMatcher()
29+
phrase_matcher.display_name = "NON_SHIPPING_PHONE_SERVICE"
30+
phrase_matcher.type_ = (
31+
contact_center_insights_v1.PhraseMatcher.PhraseMatcherType.ALL_OF
32+
)
33+
phrase_matcher.active = True
34+
35+
# Construct a rule group to match the word "PHONE" or "CELLPHONE", ignoring case sensitivity.
36+
rule_group_phone_or_cellphone = contact_center_insights_v1.PhraseMatchRuleGroup()
37+
rule_group_phone_or_cellphone.type_ = (
38+
contact_center_insights_v1.PhraseMatchRuleGroup.PhraseMatchRuleGroupType.ANY_OF
39+
)
40+
41+
for word in ["PHONE", "CELLPHONE"]:
42+
rule = contact_center_insights_v1.PhraseMatchRule()
43+
rule.query = word
44+
rule.config.exact_match_config = contact_center_insights_v1.ExactMatchConfig()
45+
rule_group_phone_or_cellphone.phrase_match_rules.append(rule)
46+
phrase_matcher.phrase_match_rule_groups.append(rule_group_phone_or_cellphone)
47+
48+
# Construct another rule group to not match the word "SHIPPING" or "DELIVERY", ignoring case sensitivity.
49+
rule_group_not_shipping_or_delivery = (
50+
contact_center_insights_v1.PhraseMatchRuleGroup()
51+
)
52+
rule_group_not_shipping_or_delivery.type_ = (
53+
contact_center_insights_v1.PhraseMatchRuleGroup.PhraseMatchRuleGroupType.ALL_OF
54+
)
55+
56+
for word in ["SHIPPING", "DELIVERY"]:
57+
rule = contact_center_insights_v1.PhraseMatchRule()
58+
rule.query = word
59+
rule.negated = True
60+
rule.config.exact_match_config = contact_center_insights_v1.ExactMatchConfig()
61+
rule_group_not_shipping_or_delivery.phrase_match_rules.append(rule)
62+
phrase_matcher.phrase_match_rule_groups.append(rule_group_not_shipping_or_delivery)
63+
64+
# Call the Insights client to create a phrase matcher.
65+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
66+
phrase_matcher = insights_client.create_phrase_matcher(
67+
parent=parent, phrase_matcher=phrase_matcher
68+
)
69+
70+
print(f"Created {phrase_matcher.name}")
71+
return phrase_matcher
72+
73+
74+
# [END contactcenterinsights_create_phrase_matcher_all_of]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# [START contactcenterinsights_create_phrase_matcher_any_of]
16+
from google.cloud import contact_center_insights_v1
17+
18+
19+
def create_phrase_matcher_any_of(
20+
project_id: str,
21+
) -> contact_center_insights_v1.PhraseMatcher:
22+
# Construct a parent resource.
23+
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
24+
project_id, "us-central1"
25+
)
26+
27+
# Construct a phrase matcher that matches any of its rule groups.
28+
phrase_matcher = contact_center_insights_v1.PhraseMatcher()
29+
phrase_matcher.display_name = "PHONE_SERVICE"
30+
phrase_matcher.type_ = (
31+
contact_center_insights_v1.PhraseMatcher.PhraseMatcherType.ANY_OF
32+
)
33+
phrase_matcher.active = True
34+
35+
# Construct a rule group to match the word "PHONE" or "CELLPHONE", ignoring case sensitivity.
36+
rule_group = contact_center_insights_v1.PhraseMatchRuleGroup()
37+
rule_group.type_ = (
38+
contact_center_insights_v1.PhraseMatchRuleGroup.PhraseMatchRuleGroupType.ANY_OF
39+
)
40+
41+
for word in ["PHONE", "CELLPHONE"]:
42+
rule = contact_center_insights_v1.PhraseMatchRule()
43+
rule.query = word
44+
rule.config.exact_match_config = contact_center_insights_v1.ExactMatchConfig()
45+
rule_group.phrase_match_rules.append(rule)
46+
phrase_matcher.phrase_match_rule_groups.append(rule_group)
47+
48+
# Call the Insights client to create a phrase matcher.
49+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
50+
phrase_matcher = insights_client.create_phrase_matcher(
51+
parent=parent, phrase_matcher=phrase_matcher
52+
)
53+
54+
print(f"Created {phrase_matcher.name}")
55+
return phrase_matcher
56+
57+
58+
# [END contactcenterinsights_create_phrase_matcher_any_of]

contact-center-insights/snippets/noxfile.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,29 @@
3939

4040
TEST_CONFIG = {
4141
# You can opt out from the test for specific Python versions.
42-
'ignored_versions': [],
43-
42+
"ignored_versions": [],
4443
# Old samples are opted out of enforcing Python type hints
4544
# All new samples should feature them
46-
'enforce_type_hints': False,
47-
45+
"enforce_type_hints": False,
4846
# An envvar key for determining the project id to use. Change it
4947
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
5048
# build specific Cloud project. You can also use your own string
5149
# to use your own Cloud project.
52-
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
50+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
5351
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
5452
# If you need to use a specific version of pip,
5553
# change pip_version_override to the string representation
5654
# of the version number, for example, "20.2.4"
5755
"pip_version_override": None,
5856
# A dictionary you want to inject into your test. Don't put any
5957
# secrets here. These values will override predefined values.
60-
'envs': {},
58+
"envs": {},
6159
}
6260

6361

6462
try:
6563
# Ensure we can import noxfile_config in the project's directory.
66-
sys.path.append('.')
64+
sys.path.append(".")
6765
from noxfile_config import TEST_CONFIG_OVERRIDE
6866
except ImportError as e:
6967
print("No user noxfile_config found: detail: {}".format(e))
@@ -78,12 +76,12 @@ def get_pytest_env_vars() -> Dict[str, str]:
7876
ret = {}
7977

8078
# Override the GCLOUD_PROJECT and the alias.
81-
env_key = TEST_CONFIG['gcloud_project_env']
79+
env_key = TEST_CONFIG["gcloud_project_env"]
8280
# This should error out if not set.
83-
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
81+
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
8482

8583
# Apply user supplied envs.
86-
ret.update(TEST_CONFIG['envs'])
84+
ret.update(TEST_CONFIG["envs"])
8785
return ret
8886

8987

@@ -92,11 +90,14 @@ def get_pytest_env_vars() -> Dict[str, str]:
9290
ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9"]
9391

9492
# Any default versions that should be ignored.
95-
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
93+
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
9694

9795
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
9896

99-
INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ("True", "true")
97+
INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in (
98+
"True",
99+
"true",
100+
)
100101
#
101102
# Style Checks
102103
#
@@ -141,7 +142,7 @@ def _determine_local_import_names(start_dir: str) -> List[str]:
141142

142143
@nox.session
143144
def lint(session: nox.sessions.Session) -> None:
144-
if not TEST_CONFIG['enforce_type_hints']:
145+
if not TEST_CONFIG["enforce_type_hints"]:
145146
session.install("flake8", "flake8-import-order")
146147
else:
147148
session.install("flake8", "flake8-import-order", "flake8-annotations")
@@ -150,9 +151,11 @@ def lint(session: nox.sessions.Session) -> None:
150151
args = FLAKE8_COMMON_ARGS + [
151152
"--application-import-names",
152153
",".join(local_names),
153-
"."
154+
".",
154155
]
155156
session.run("flake8", *args)
157+
158+
156159
#
157160
# Black
158161
#
@@ -165,6 +168,7 @@ def blacken(session: nox.sessions.Session) -> None:
165168

166169
session.run("black", *python_files)
167170

171+
168172
#
169173
# Sample Tests
170174
#
@@ -173,7 +177,9 @@ def blacken(session: nox.sessions.Session) -> None:
173177
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
174178

175179

176-
def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
180+
def _session_tests(
181+
session: nox.sessions.Session, post_install: Callable = None
182+
) -> None:
177183
if TEST_CONFIG["pip_version_override"]:
178184
pip_version = TEST_CONFIG["pip_version_override"]
179185
session.install(f"pip=={pip_version}")
@@ -203,7 +209,7 @@ def _session_tests(session: nox.sessions.Session, post_install: Callable = None)
203209
# on travis where slow and flaky tests are excluded.
204210
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
205211
success_codes=[0, 5],
206-
env=get_pytest_env_vars()
212+
env=get_pytest_env_vars(),
207213
)
208214

209215

@@ -213,9 +219,9 @@ def py(session: nox.sessions.Session) -> None:
213219
if session.python in TESTED_VERSIONS:
214220
_session_tests(session)
215221
else:
216-
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
217-
session.python
218-
))
222+
session.skip(
223+
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
224+
)
219225

220226

221227
#
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
google-auth==2.0.2
22
google-cloud-pubsub==2.8.0
33
protobuf==3.17.3
4-
pytest==6.2.5
4+
pytest==6.2.5
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
google-api-core==2.0.1
22
google-cloud-bigquery==2.26.0
3-
google-cloud-contact-center-insights==0.2.0
3+
google-cloud-contact-center-insights==0.2.0
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
import google.auth
16+
17+
from google.cloud import contact_center_insights_v1
18+
19+
import pytest
20+
21+
import create_phrase_matcher_all_of
22+
23+
24+
@pytest.fixture
25+
def project_id():
26+
_, project_id = google.auth.default()
27+
return project_id
28+
29+
30+
@pytest.fixture
31+
def insights_client():
32+
return contact_center_insights_v1.ContactCenterInsightsClient()
33+
34+
35+
@pytest.fixture
36+
def phrase_matcher_all_of(project_id, insights_client):
37+
# Create a phrase matcher.
38+
phrase_matcher = create_phrase_matcher_all_of.create_phrase_matcher_all_of(
39+
project_id
40+
)
41+
yield phrase_matcher
42+
43+
# Delete the phrase matcher.
44+
insights_client.delete_phrase_matcher(name=phrase_matcher.name)
45+
46+
47+
def test_create_phrase_matcher_all_of(capsys, phrase_matcher_all_of):
48+
phrase_matcher = phrase_matcher_all_of
49+
out, err = capsys.readouterr()
50+
assert f"Created {phrase_matcher.name}" in out
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
import google.auth
16+
17+
from google.cloud import contact_center_insights_v1
18+
19+
import pytest
20+
21+
import create_phrase_matcher_any_of
22+
23+
24+
@pytest.fixture
25+
def project_id():
26+
_, project_id = google.auth.default()
27+
return project_id
28+
29+
30+
@pytest.fixture
31+
def insights_client():
32+
return contact_center_insights_v1.ContactCenterInsightsClient()
33+
34+
35+
@pytest.fixture
36+
def phrase_matcher_any_of(project_id, insights_client):
37+
# Create a phrase matcher.
38+
phrase_matcher = create_phrase_matcher_any_of.create_phrase_matcher_any_of(
39+
project_id
40+
)
41+
yield phrase_matcher
42+
43+
# Delete the phrase matcher.
44+
insights_client.delete_phrase_matcher(name=phrase_matcher.name)
45+
46+
47+
def test_create_phrase_matcher_any_of(capsys, phrase_matcher_any_of):
48+
phrase_matcher = phrase_matcher_any_of
49+
out, err = capsys.readouterr()
50+
assert f"Created {phrase_matcher.name}" in out

0 commit comments

Comments
 (0)