in pytext/models/semantic_parsers/rnng/rnng_parser.py [0:0]
def valid_actions(self, state: ParserState) -> List[int]:
"""Used for restricting the set of possible action predictions
Args:
state (ParserState): The state of the stack, buffer and action
Returns:
List[int] : indices of the valid actions
"""
valid_actions: List[int] = []
is_open_NT = state.is_open_NT
num_open_NT = state.num_open_NT
stack = state.stack_stackrnn
buffer = state.buffer_stackrnn
# Can REDUCE if
# 1. Top of multi-element stack is not an NT, and
# 2. Two open NTs on stack, or buffer is empty
if (is_open_NT and not is_open_NT[-1] and not len(is_open_NT) == 1) and (
num_open_NT >= 2 or len(buffer) == 0
):
assert len(stack) > 0
valid_actions.append(self.reduce_idx)
if len(buffer) > 0 and num_open_NT < self.max_open_NT:
last_open_NT = None
try:
last_open_NT = stack.element_from_top(is_open_NT[::-1].index(True))
except ValueError:
pass
if (not self.training) or self.constraints_intent_slot_nesting:
# if stack is empty or the last open NT is slot
if (not last_open_NT) or last_open_NT.node in self.valid_SL_idxs:
valid_actions += self.valid_IN_idxs
elif last_open_NT.node in self.valid_IN_idxs:
if (
self.constraints_no_slots_inside_unsupported
and state.found_unsupported
):
pass
else:
valid_actions += self.valid_SL_idxs
else:
valid_actions += self.valid_IN_idxs
valid_actions += self.valid_SL_idxs
elif (not self.training) and num_open_NT >= self.max_open_NT:
print(
"not predicting NT because buffer len is "
+ str(len(buffer))
+ " and num open NTs is "
+ str(num_open_NT)
)
# Can SHIFT if
# 1. Buffer is non-empty, and
# 2. At least one open NT on stack
if len(buffer) > 0 and num_open_NT >= 1:
valid_actions.append(self.shift_idx)
return valid_actions