|
| 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 | + """Creates a phrase matcher that matches all specified queries. |
| 23 | +
|
| 24 | + Args: |
| 25 | + project_id: |
| 26 | + The project identifier. For example, 'my-project'. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + A phrase matcher. |
| 30 | + """ |
| 31 | + # Construct a parent resource. |
| 32 | + parent = ( |
| 33 | + contact_center_insights_v1.ContactCenterInsightsClient.common_location_path( |
| 34 | + project_id, "us-central1" |
| 35 | + ) |
| 36 | + ) |
| 37 | + |
| 38 | + # Construct a phrase matcher that matches all of its rule groups. |
| 39 | + phrase_matcher = contact_center_insights_v1.PhraseMatcher() |
| 40 | + phrase_matcher.display_name = "NON_SHIPPING_PHONE_SERVICE" |
| 41 | + phrase_matcher.type_ = ( |
| 42 | + contact_center_insights_v1.PhraseMatcher.PhraseMatcherType.ALL_OF |
| 43 | + ) |
| 44 | + phrase_matcher.active = True |
| 45 | + |
| 46 | + # Construct a rule group to match the word "PHONE" or "CELLPHONE", ignoring case sensitivity. |
| 47 | + rule_group_phone_or_cellphone = contact_center_insights_v1.PhraseMatchRuleGroup() |
| 48 | + rule_group_phone_or_cellphone.type_ = ( |
| 49 | + contact_center_insights_v1.PhraseMatchRuleGroup.PhraseMatchRuleGroupType.ANY_OF |
| 50 | + ) |
| 51 | + |
| 52 | + for word in ["PHONE", "CELLPHONE"]: |
| 53 | + rule = contact_center_insights_v1.PhraseMatchRule() |
| 54 | + rule.query = word |
| 55 | + rule.config.exact_match_config = contact_center_insights_v1.ExactMatchConfig() |
| 56 | + rule_group_phone_or_cellphone.phrase_match_rules.append(rule) |
| 57 | + phrase_matcher.phrase_match_rule_groups.append(rule_group_phone_or_cellphone) |
| 58 | + |
| 59 | + # Construct another rule group to not match the word "SHIPPING" or "DELIVERY", ignoring case sensitivity. |
| 60 | + rule_group_not_shipping_or_delivery = ( |
| 61 | + contact_center_insights_v1.PhraseMatchRuleGroup() |
| 62 | + ) |
| 63 | + rule_group_not_shipping_or_delivery.type_ = ( |
| 64 | + contact_center_insights_v1.PhraseMatchRuleGroup.PhraseMatchRuleGroupType.ALL_OF |
| 65 | + ) |
| 66 | + |
| 67 | + for word in ["SHIPPING", "DELIVERY"]: |
| 68 | + rule = contact_center_insights_v1.PhraseMatchRule() |
| 69 | + rule.query = word |
| 70 | + rule.negated = True |
| 71 | + rule.config.exact_match_config = contact_center_insights_v1.ExactMatchConfig() |
| 72 | + rule_group_not_shipping_or_delivery.phrase_match_rules.append(rule) |
| 73 | + phrase_matcher.phrase_match_rule_groups.append(rule_group_not_shipping_or_delivery) |
| 74 | + |
| 75 | + # Call the Insights client to create a phrase matcher. |
| 76 | + insights_client = contact_center_insights_v1.ContactCenterInsightsClient() |
| 77 | + phrase_matcher = insights_client.create_phrase_matcher( |
| 78 | + parent=parent, phrase_matcher=phrase_matcher |
| 79 | + ) |
| 80 | + |
| 81 | + print(f"Created {phrase_matcher.name}") |
| 82 | + return phrase_matcher |
| 83 | + |
| 84 | + |
| 85 | +# [END contactcenterinsights_create_phrase_matcher_all_of] |
0 commit comments