-
Notifications
You must be signed in to change notification settings - Fork 1
/
violation_detection.py
114 lines (96 loc) · 4.72 KB
/
violation_detection.py
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# 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 itertools
import operator as op
import re
import numpy as np
# Check one atomic condition with an entity
def passing_atomic_value(atomic, value):
# ['=', '>', '<', '>=', '<=', '!=', 'BETWEEN', 'REGEXP', 'IN', 'NOTNULL']
OP_MAPPING = {'=': op.eq, '>': op.gt, '<': op.lt, '>=': op.ge, '<=': op.le, '!=': op.ne}
operator, ref = atomic['operator'], atomic['values']
if operator == 'NOTNULL':
return True
elif operator == 'IN':
return (value is not None) and (value in ref)
elif operator == 'BETWEEN':
# TODO: Check how to compare times
return (value >= ref[0]) and (value <= ref[1])
elif operator == 'REGEXP':
# TODO: Match or Search is more suitable?
return bool(re.match(ref, value))
elif operator in OP_MAPPING:
return OP_MAPPING[operator](value, ref)
else:
raise Exception(f'Invalid operator {operator}')
# Check the entire constraint statement with all the applicable slot entities
def passing_disjunctions_values(disjunctions, slot_entities):
passes = False
for conj in disjunctions:
# TODO: This is not efficient because it does not have short circuit all
if all(passing_atomic_value(atomic, slot_entities[idx]) for idx, atomic in enumerate(conj)):
passes = True
break
return passes
def find_violations(constraint, state, distinct_slot_values):
# Return x, y where x is whether the constraint is applicable and y is a list of violations if x is True else []
# Pre-condition: the order of conjunction terms in the constraint is the same as the order of slot variables in slots
existing_slots = set(state.keys())
applicable_slots = set(constraint['slots'])
# Skip the constraint if it contains any built-in slots which we cannot apply bi-jaccard entity linking
if not all(s in distinct_slot_values for s in applicable_slots):
return False, []
# Compute only if the dialog state contains all the applicable slots
if applicable_slots.issubset(existing_slots):
slot_values = [state[s] for s in constraint['slots']]
violations = []
for slot_entities in itertools.product(*slot_values):
if not passing_disjunctions_values(constraint['disjunctions'], slot_entities):
violations.append({s: slot_entities[idx] for idx, s in enumerate(constraint['slots'])})
return True, violations
else:
return False, []
def get_violation_prob(disjunctions, slot_entities, distinct_slot_values):
satisfying_score = 0
for conj in disjunctions:
atomic_probs = []
for idx, atomic in enumerate(conj):
assert atomic['operator'] == 'IN'
try:
atomic_probs.append(sum(
[slot_entities[idx][distinct_slot_values[atomic['slotVariable']].index(v)] for v in
atomic['values']]))
except:
print(atomic)
print(distinct_slot_values[atomic['slotVariable']])
raise Exception()
satisfying_score += np.prod(atomic_probs)
return 1 - satisfying_score
def find_violations_probs(constraint, state, distinct_slot_values):
# Return x, y where x is whether the constraint is applicable and y is a list of violations if x is True else []
# Pre-condition: the order of conjunction terms in the constraint is the same as the order of slot variables in slots
existing_slots = set(state.keys())
applicable_slots = set(constraint['slots'])
# Skip the constraint if it contains any built-in slots which we cannot apply bi-jaccard entity linking
if not all(s in distinct_slot_values for s in applicable_slots):
return False, []
# Compute only if the dialog state contains all the applicable slots
if applicable_slots.issubset(existing_slots):
slot_values = [state[s] for s in constraint['slots']]
violations = []
for slot_entities in itertools.product(*slot_values):
violations.append(get_violation_prob(constraint['disjunctions'], slot_entities, distinct_slot_values))
return True, violations
else:
return False, []