-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: create custom highlights (#15)
- Loading branch information
Showing
7 changed files
with
259 additions
and
21 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
contact-center-insights/snippets/create_phrase_matcher_all_of.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# 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. | ||
# | ||
# [START contactcenterinsights_create_phrase_matcher_all_of] | ||
from google.cloud import contact_center_insights_v1 | ||
|
||
|
||
def create_phrase_matcher_all_of( | ||
project_id: str, | ||
) -> contact_center_insights_v1.PhraseMatcher: | ||
# Construct a parent resource. | ||
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path( | ||
project_id, "us-central1" | ||
) | ||
|
||
# Construct a phrase matcher that matches all of its rule groups. | ||
phrase_matcher = contact_center_insights_v1.PhraseMatcher() | ||
phrase_matcher.display_name = "NON_SHIPPING_PHONE_SERVICE" | ||
phrase_matcher.type_ = ( | ||
contact_center_insights_v1.PhraseMatcher.PhraseMatcherType.ALL_OF | ||
) | ||
phrase_matcher.active = True | ||
|
||
# Construct a rule group to match the word "PHONE" or "CELLPHONE", ignoring case sensitivity. | ||
rule_group_phone_or_cellphone = contact_center_insights_v1.PhraseMatchRuleGroup() | ||
rule_group_phone_or_cellphone.type_ = ( | ||
contact_center_insights_v1.PhraseMatchRuleGroup.PhraseMatchRuleGroupType.ANY_OF | ||
) | ||
|
||
for word in ["PHONE", "CELLPHONE"]: | ||
rule = contact_center_insights_v1.PhraseMatchRule() | ||
rule.query = word | ||
rule.config.exact_match_config = contact_center_insights_v1.ExactMatchConfig() | ||
rule_group_phone_or_cellphone.phrase_match_rules.append(rule) | ||
phrase_matcher.phrase_match_rule_groups.append(rule_group_phone_or_cellphone) | ||
|
||
# Construct another rule group to not match the word "SHIPPING" or "DELIVERY", ignoring case sensitivity. | ||
rule_group_not_shipping_or_delivery = ( | ||
contact_center_insights_v1.PhraseMatchRuleGroup() | ||
) | ||
rule_group_not_shipping_or_delivery.type_ = ( | ||
contact_center_insights_v1.PhraseMatchRuleGroup.PhraseMatchRuleGroupType.ALL_OF | ||
) | ||
|
||
for word in ["SHIPPING", "DELIVERY"]: | ||
rule = contact_center_insights_v1.PhraseMatchRule() | ||
rule.query = word | ||
rule.negated = True | ||
rule.config.exact_match_config = contact_center_insights_v1.ExactMatchConfig() | ||
rule_group_not_shipping_or_delivery.phrase_match_rules.append(rule) | ||
phrase_matcher.phrase_match_rule_groups.append(rule_group_not_shipping_or_delivery) | ||
|
||
# Call the Insights client to create a phrase matcher. | ||
insights_client = contact_center_insights_v1.ContactCenterInsightsClient() | ||
phrase_matcher = insights_client.create_phrase_matcher( | ||
parent=parent, phrase_matcher=phrase_matcher | ||
) | ||
|
||
print(f"Created {phrase_matcher.name}") | ||
return phrase_matcher | ||
|
||
|
||
# [END contactcenterinsights_create_phrase_matcher_all_of] |
58 changes: 58 additions & 0 deletions
58
contact-center-insights/snippets/create_phrase_matcher_any_of.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# 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. | ||
# | ||
# [START contactcenterinsights_create_phrase_matcher_any_of] | ||
from google.cloud import contact_center_insights_v1 | ||
|
||
|
||
def create_phrase_matcher_any_of( | ||
project_id: str, | ||
) -> contact_center_insights_v1.PhraseMatcher: | ||
# Construct a parent resource. | ||
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path( | ||
project_id, "us-central1" | ||
) | ||
|
||
# Construct a phrase matcher that matches any of its rule groups. | ||
phrase_matcher = contact_center_insights_v1.PhraseMatcher() | ||
phrase_matcher.display_name = "PHONE_SERVICE" | ||
phrase_matcher.type_ = ( | ||
contact_center_insights_v1.PhraseMatcher.PhraseMatcherType.ANY_OF | ||
) | ||
phrase_matcher.active = True | ||
|
||
# Construct a rule group to match the word "PHONE" or "CELLPHONE", ignoring case sensitivity. | ||
rule_group = contact_center_insights_v1.PhraseMatchRuleGroup() | ||
rule_group.type_ = ( | ||
contact_center_insights_v1.PhraseMatchRuleGroup.PhraseMatchRuleGroupType.ANY_OF | ||
) | ||
|
||
for word in ["PHONE", "CELLPHONE"]: | ||
rule = contact_center_insights_v1.PhraseMatchRule() | ||
rule.query = word | ||
rule.config.exact_match_config = contact_center_insights_v1.ExactMatchConfig() | ||
rule_group.phrase_match_rules.append(rule) | ||
phrase_matcher.phrase_match_rule_groups.append(rule_group) | ||
|
||
# Call the Insights client to create a phrase matcher. | ||
insights_client = contact_center_insights_v1.ContactCenterInsightsClient() | ||
phrase_matcher = insights_client.create_phrase_matcher( | ||
parent=parent, phrase_matcher=phrase_matcher | ||
) | ||
|
||
print(f"Created {phrase_matcher.name}") | ||
return phrase_matcher | ||
|
||
|
||
# [END contactcenterinsights_create_phrase_matcher_any_of] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
google-auth==2.0.2 | ||
google-cloud-pubsub==2.8.0 | ||
protobuf==3.17.3 | ||
pytest==6.2.5 | ||
pytest==6.2.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
google-api-core==2.0.1 | ||
google-cloud-bigquery==2.26.0 | ||
google-cloud-contact-center-insights==0.2.0 | ||
google-cloud-contact-center-insights==0.2.0 |
50 changes: 50 additions & 0 deletions
50
contact-center-insights/snippets/test_create_phrase_matcher_all_of.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 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. | ||
# | ||
import google.auth | ||
|
||
from google.cloud import contact_center_insights_v1 | ||
|
||
import pytest | ||
|
||
import create_phrase_matcher_all_of | ||
|
||
|
||
@pytest.fixture | ||
def project_id(): | ||
_, project_id = google.auth.default() | ||
return project_id | ||
|
||
|
||
@pytest.fixture | ||
def insights_client(): | ||
return contact_center_insights_v1.ContactCenterInsightsClient() | ||
|
||
|
||
@pytest.fixture | ||
def phrase_matcher_all_of(project_id, insights_client): | ||
# Create a phrase matcher. | ||
phrase_matcher = create_phrase_matcher_all_of.create_phrase_matcher_all_of( | ||
project_id | ||
) | ||
yield phrase_matcher | ||
|
||
# Delete the phrase matcher. | ||
insights_client.delete_phrase_matcher(name=phrase_matcher.name) | ||
|
||
|
||
def test_create_phrase_matcher_all_of(capsys, phrase_matcher_all_of): | ||
phrase_matcher = phrase_matcher_all_of | ||
out, err = capsys.readouterr() | ||
assert f"Created {phrase_matcher.name}" in out |
50 changes: 50 additions & 0 deletions
50
contact-center-insights/snippets/test_create_phrase_matcher_any_of.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 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. | ||
# | ||
import google.auth | ||
|
||
from google.cloud import contact_center_insights_v1 | ||
|
||
import pytest | ||
|
||
import create_phrase_matcher_any_of | ||
|
||
|
||
@pytest.fixture | ||
def project_id(): | ||
_, project_id = google.auth.default() | ||
return project_id | ||
|
||
|
||
@pytest.fixture | ||
def insights_client(): | ||
return contact_center_insights_v1.ContactCenterInsightsClient() | ||
|
||
|
||
@pytest.fixture | ||
def phrase_matcher_any_of(project_id, insights_client): | ||
# Create a phrase matcher. | ||
phrase_matcher = create_phrase_matcher_any_of.create_phrase_matcher_any_of( | ||
project_id | ||
) | ||
yield phrase_matcher | ||
|
||
# Delete the phrase matcher. | ||
insights_client.delete_phrase_matcher(name=phrase_matcher.name) | ||
|
||
|
||
def test_create_phrase_matcher_any_of(capsys, phrase_matcher_any_of): | ||
phrase_matcher = phrase_matcher_any_of | ||
out, err = capsys.readouterr() | ||
assert f"Created {phrase_matcher.name}" in out |