def test_can_derive_false_3(self): goal_rule = qcfg_rule.rule_from_string( "what are the major cities in the NT_1 through which the major river in NT_2 runs ### answer ( intersection ( major , intersection ( city , loc_2 ( intersection ( NT_1 , traverse_1 ( intersection ( major , intersection ( river , loc_2 ( NT_2 ) ) ) ) ) ) ) ) )" ) rules = { qcfg_rule.rule_from_string("river ### river"), qcfg_rule.rule_from_string( "river ### intersection ( river , NT_1 )"), qcfg_rule.rule_from_string("cities ### city"), qcfg_rule.rule_from_string( "what are the NT_1 ### answer ( NT_1 )"), qcfg_rule.rule_from_string( "NT_1 in NT_2 ### intersection ( NT_1 , loc_2 ( NT_2 ) )"), qcfg_rule.rule_from_string( "what are the major NT_1 ### answer ( intersection ( major , NT_1 ) )" ), qcfg_rule.rule_from_string( "NT_1 in the NT_2 ### intersection ( NT_1 , loc_2 ( NT_2 ) )"), qcfg_rule.rule_from_string( "river in NT_1 ### intersection ( river , loc_2 ( NT_1 ) )"), qcfg_rule.rule_from_string( "the NT_1 NT_2 ### intersection ( NT_1 , NT_2 )") } derived_rules = set() can_derive = derivation_utils.can_derive(goal_rule, rules, derived_rules) print("derived_rules: %s" % derived_rules) self.assertFalse(can_derive)
def _update_state(affected_rules, search_state, config): """Sparsely update the state for rules that may be affected.""" for idx, affected_rule in enumerate(affected_rules): # Debug logging every Nth rule. if idx % 10 == 0: print("Updating rule %s of %s." % (idx + 1, len(affected_rules))) # Check if rule can now be generated. Ideally, this should have been # determined upstream when determining which rules could be removed, # but some cases are not caught until here, such as when source # sequences contain repeated substrings and are therefore not considered # by `get_candidates`. # Regardless, it is still important to run this for the side-effect of # updating the set of derivable rules. if derivation_utils.can_derive(affected_rule, search_state.current_rules, search_state.derivable_rules): print("Can now generate: %s." % str(affected_rule)) search_state.current_rules.remove(affected_rule) else: candidates = split_utils.find_possible_splits( affected_rule, search_state.derivable_rules, allow_repeated_target_nts=config.allow_repeated_target_nts, ) if config.balance_parens: candidates = _filter_unbalanced_paren_candidates(candidates) for candidate in candidates: search_state.rules_to_candidates[affected_rule].add(candidate) print("Updates complete.")
def test_can_derive_1(self): goal_rule = qcfg_rule.rule_from_string("foo bar ### foo bar") rules = { qcfg_rule.rule_from_string("foo NT_1 ### foo NT_1"), qcfg_rule.rule_from_string("bar ### bar"), } derived_rules = set() can_derive = derivation_utils.can_derive(goal_rule, rules, derived_rules) print("derived_rules: %s" % derived_rules) self.assertTrue(can_derive)
def _get_rules_for_other_examples(induced_rules, other_examples): """Add rules for examples outside of sample that cannot be derived.""" new_rules = set() for source_str, target_str in other_examples: goal_rule = qcfg_rule.QCFGRule(tuple(source_str.split()), tuple(target_str.split()), arity=0) if not derivation_utils.can_derive(goal_rule, induced_rules, None): new_rules.add(goal_rule) print("Added %s rules for examples outside of initial sample." % len(new_rules)) return new_rules
def test_can_derive_after_1(self): goal_rule = qcfg_rule.rule_from_string( "NT_1 after jump thrice ### I_JUMP I_JUMP I_JUMP NT_1") rules = { qcfg_rule.rule_from_string("NT_1 thrice ### NT_1 NT_1 NT_1"), qcfg_rule.rule_from_string("NT_1 after NT_2 ### NT_2 NT_1"), qcfg_rule.rule_from_string("jump ### I_JUMP"), } derived_rules = set() can_derive = derivation_utils.can_derive(goal_rule, rules, derived_rules) print("derived_rules: %s" % derived_rules) self.assertTrue(can_derive)
def test_can_derive_and_1(self): goal_rule = qcfg_rule.rule_from_string( "NT_1 and run twice ### NT_1 I_RUN I_RUN") rules = { qcfg_rule.rule_from_string("NT_1 twice ### NT_1 NT_1"), qcfg_rule.rule_from_string("NT_1 and NT_2 ### NT_1 NT_2"), qcfg_rule.rule_from_string("run ### I_RUN"), } derived_rules = set() can_derive = derivation_utils.can_derive(goal_rule, rules, derived_rules) print("derived_rules: %s" % derived_rules) self.assertTrue(can_derive)
def test_can_derive_and_2(self): goal_rule = qcfg_rule.rule_from_string( "look and jump left ### I_LOOK I_TURN_LEFT I_JUMP") rules = { qcfg_rule.rule_from_string("NT_1 left ### I_TURN_LEFT NT_1"), qcfg_rule.rule_from_string("NT_1 and NT_2 ### NT_1 NT_2"), qcfg_rule.rule_from_string("jump ### I_JUMP"), qcfg_rule.rule_from_string("look ### I_LOOK"), } derived_rules = set() can_derive = derivation_utils.can_derive(goal_rule, rules, derived_rules) print("derived_rules: %s" % derived_rules) self.assertTrue(can_derive)
def test_can_derive_false_2(self): goal_rule = qcfg_rule.rule_from_string( "walk and run around right ### I_WALK I_TURN_RIGHT I_RUN I_TURN_RIGHT I_RUN I_TURN_RIGHT I_RUN I_TURN_RIGHT I_RUN" ) rules = { qcfg_rule.rule_from_string("NT_1 and NT_2 ### NT_1 NT_2"), qcfg_rule.rule_from_string("NT_1 and run ### NT_1 I_RUN"), qcfg_rule.rule_from_string("walk ### I_WALK"), qcfg_rule.rule_from_string("NT_1 right ### I_TURN_RIGHT NT_1"), } derived_rules = set() can_derive = derivation_utils.can_derive(goal_rule, rules, derived_rules) print("derived_rules: %s" % derived_rules) self.assertFalse(can_derive)