forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_management.py
More file actions
140 lines (112 loc) · 4.58 KB
/
Copy pathentity_management.py
File metadata and controls
140 lines (112 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# Copyright 2017 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.
"""DialogFlow API Entity Python sample showing how to manage entities.
Examples:
python entity_management.py -h
python entity_management.py --project-id PROJECT_ID \
list --entity-type-id e57238e2-e692-44ea-9216-6be1b2332e2a
python entity_management.py --project-id PROJECT_ID \
create new_room --synonyms basement cellar \
--entity-type-id e57238e2-e692-44ea-9216-6be1b2332e2a
python entity_management.py --project-id PROJECT_ID \
delete new_room \
--entity-type-id e57238e2-e692-44ea-9216-6be1b2332e2a
"""
import argparse
# [START dialogflow_list_entities]
def list_entities(project_id, entity_type_id):
import dialogflow_v2 as dialogflow
entity_types_client = dialogflow.EntityTypesClient()
parent = entity_types_client.entity_type_path(
project_id, entity_type_id)
entities = entity_types_client.get_entity_type(parent).entities
for entity in entities:
print('Entity value: {}'.format(entity.value))
print('Entity synonyms: {}\n'.format(entity.synonyms))
# [END dialogflow_list_entities]
# [START dialogflow_create_entity]
def create_entity(project_id, entity_type_id, entity_value, synonyms):
"""Create an entity of the given entity type."""
import dialogflow_v2 as dialogflow
entity_types_client = dialogflow.EntityTypesClient()
# Note: synonyms must be exactly [entity_value] if the
# entity_type's kind is KIND_LIST
synonyms = synonyms or [entity_value]
entity_type_path = entity_types_client.entity_type_path(
project_id, entity_type_id)
entity = dialogflow.types.EntityType.Entity()
entity.value = entity_value
entity.synonyms.extend(synonyms)
response = entity_types_client.batch_create_entities(
entity_type_path, [entity])
print('Entity created: {}'.format(response))
# [END dialogflow_create_entity]
# [START dialogflow_delete_entity]
def delete_entity(project_id, entity_type_id, entity_value):
"""Delete entity with the given entity type and entity value."""
import dialogflow_v2 as dialogflow
entity_types_client = dialogflow.EntityTypesClient()
entity_type_path = entity_types_client.entity_type_path(
project_id, entity_type_id)
entity_types_client.batch_delete_entities(
entity_type_path, [entity_value])
# [END dialogflow_delete_entity]
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'--project-id',
help='Project/agent id. Required.',
required=True)
subparsers = parser.add_subparsers(dest='command')
list_parser = subparsers.add_parser(
'list', help=list_entities.__doc__)
list_parser.add_argument(
'--entity-type-id',
help='The id of the entity_type.')
create_parser = subparsers.add_parser(
'create', help=create_entity.__doc__)
create_parser.add_argument(
'entity_value',
help='The entity value to be added.')
create_parser.add_argument(
'--entity-type-id',
help='The id of the entity_type to which to add an entity.',
required=True)
create_parser.add_argument(
'--synonyms',
nargs='*',
help='The synonyms that will map to the provided entity value.',
default=[])
delete_parser = subparsers.add_parser(
'delete', help=delete_entity.__doc__)
delete_parser.add_argument(
'--entity-type-id',
help='The id of the entity_type.',
required=True)
delete_parser.add_argument(
'entity_value',
help='The value of the entity to delete.')
args = parser.parse_args()
if args.command == 'list':
list_entities(args.project_id, args.entity_type_id)
elif args.command == 'create':
create_entity(
args.project_id, args.entity_type_id, args.entity_value,
args.synonyms)
elif args.command == 'delete':
delete_entity(
args.project_id, args.entity_type_id, args.entity_value)