def test_find_possible_splits_10(self): rule_a = qcfg_rule.rule_from_string("jump and NT_1 ### jump NT_1") rule_b = qcfg_rule.rule_from_string("NT_1 and NT_2 ### NT_1 NT_2") ruleset = {rule_b} splits = split_utils.find_possible_splits(rule_a, ruleset) expected = qcfg_rule.rule_from_string("jump ### jump") self.assertEqual({expected}, splits)
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_find_possible_splits_11(self): rule_a = qcfg_rule.rule_from_string( "NT_1 right twice ### I_TURN_RIGHT NT_1 I_TURN_RIGHT NT_1") rule_b = qcfg_rule.rule_from_string("NT_1 right ### I_TURN_RIGHT NT_1") ruleset = {rule_b} splits = split_utils.find_possible_splits(rule_a, ruleset) expected = qcfg_rule.rule_from_string("NT_1 twice ### NT_1 NT_1") self.assertEqual({expected}, splits)
def test_find_possible_splits_1(self): rule_a = qcfg_rule.rule_from_string("foo NT_1 ### food NT_1") rule_b = qcfg_rule.rule_from_string("bar ### bar") rule_c = qcfg_rule.rule_from_string("foo bar ### foo bar") ruleset = {rule_a, rule_b} splits = split_utils.find_possible_splits(rule_c, ruleset) expected = qcfg_rule.rule_from_string("foo NT_1 ### foo NT_1") self.assertEqual({expected}, splits)
def test_find_possible_splits_no_split(self): rule_a = qcfg_rule.rule_from_string( "NT_1 named NT_2 ### intersection ( NT_1 , NT_2 )") rule_b = qcfg_rule.rule_from_string( "NT_1 have a major city named NT_2 ### intersection ( NT_1 , loc_1 ( intersection ( major , intersection ( city , NT_2 ) ) ) )" ) ruleset = {rule_a} splits = split_utils.find_possible_splits(rule_b, ruleset) self.assertEmpty(splits)
def test_find_possible_splits_7(self): rule_a = qcfg_rule.rule_from_string( "NT_1 ' s NT_2 director ### NT_2 & film.director.film ( NT_1 )") rule_b = qcfg_rule.rule_from_string("NT_1 ' s NT_2 ### NT_2 ( NT_1 )") ruleset = {rule_b} splits = split_utils.find_possible_splits(rule_a, ruleset) expected = qcfg_rule.rule_from_string( "NT_1 director ### NT_1 & film.director.film") self.assertEqual({expected}, splits)
def test_find_possible_splits_6(self): rule_a = qcfg_rule.rule_from_string( "written and edited ### film.film.edited_by , film.film.written_by" ) rule_b = qcfg_rule.rule_from_string( "NT_1 and edited ### film.film.edited_by , NT_1") ruleset = {rule_b} splits = split_utils.find_possible_splits(rule_a, ruleset) expected = qcfg_rule.rule_from_string( "written ### film.film.written_by") self.assertEqual({expected}, splits)
def test_find_possible_splits_5(self): rule_a = qcfg_rule.rule_from_string("Dutch ### nat ( m_059j2 )") rule_b = qcfg_rule.rule_from_string( "Who influenced M2 ' s Dutch child ### answer ( a(person) & influenced ( nat ( m_059j2 ) & parent ( M2 ) ) )" ) ruleset = {rule_a} splits = split_utils.find_possible_splits(rule_b, ruleset) expected = qcfg_rule.rule_from_string( "Who influenced M2 ' s NT_1 child ### answer ( a(person) & influenced ( NT_1 & parent ( M2 ) ) )" ) self.assertEqual({expected}, splits)
def test_find_possible_splits_4(self): rule_a = qcfg_rule.rule_from_string( "NT_1 ' s star ### film.actor.film/ns:film.performance.film ( NT_1 )" ) rule_b = qcfg_rule.rule_from_string( "Which NT_1 was NT_2 ' s star ### answer ( film.actor.film/ns:film.performance.film ( NT_2 ) & NT_1 )" ) ruleset = {rule_a} splits = split_utils.find_possible_splits(rule_b, ruleset) expected = qcfg_rule.rule_from_string( "Which NT_1 was NT_2 ### answer ( NT_2 & NT_1 )") self.assertEqual({expected}, splits)
def test_find_possible_splits_2(self): rule_a = qcfg_rule.rule_from_string("what is NT_1 ### answer ( NT_1 )") rule_b = qcfg_rule.rule_from_string( "what is NT_1 traversed by NT_2 ### answer ( intersection ( NT_1 , traverse_1 ( NT_2 ) ) )" ) ruleset = {rule_a} splits = split_utils.find_possible_splits(rule_b, ruleset) expected = qcfg_rule.rule_from_string( "NT_1 traversed by NT_2 ### intersection ( NT_1 , traverse_1 ( NT_2 ) )" ) self.assertEqual({expected}, splits)
def test_find_possible_splits_3(self): rule_a = qcfg_rule.rule_from_string( "NT_1 river ### intersection ( NT_1 , river )") rule_b = qcfg_rule.rule_from_string( "NT_1 traversed by the NT_2 river ### intersection ( NT_1 , traverse_1 ( intersection ( NT_2 , river ) ) )" ) ruleset = {rule_a} splits = split_utils.find_possible_splits(rule_b, ruleset) expected = qcfg_rule.rule_from_string( "NT_1 traversed by the NT_2 ### intersection ( NT_1 , traverse_1 ( NT_2 ) )" ) self.assertEqual({expected}, splits)