def generate_binary_rules(self, tag_rules): binary_rules = set() for parent_tag_rule, a_tag_rule, b_tag_rule in itertools.permutations(tag_rules, 3): if self.__class__.val_func(parent_tag_rule, a_tag_rule, b_tag_rule): binary_rule = BinaryRule(parent_tag_rule, a_tag_rule, b_tag_rule) binary_rules.add(binary_rule) return binary_rules
def tuples_to_semantic_rules(words, syntax_tree, tuples): # TODO : index inheritance tag_rules_list = [ tuple_to_tag_rules(words, syntax_tree, tuple_) for tuple_ in tuples ] tags = { tag_rule.index: tag_rule.signature for tag_rule in itertools.chain(*tag_rules_list) } for index in words: if index not in tags: tags[index] = None unary_rules = [] binary_rules = [] for tag_rules in tag_rules_list: if len(tag_rules) == 2: unary_rule = UnaryRule(words, syntax_tree, tags, tag_rules[0].index, tag_rules[0].signature, tag_rules[1].index, tag_rules[1].signature) unary_rules.append(unary_rule) elif len(tag_rules) == 3: binary_rule = BinaryRule( words, syntax_tree, tags, tag_rules[0].index, tag_rules[0].signature, tag_rules[1].index, tag_rules[1].signature, tag_rules[2].index, tag_rules[2].signature) binary_rules.append(binary_rule) return unary_rules, binary_rules
def get_binary_rules(self): binary_rules = [] for node in self: if node.is_binary(): binary_rule = BinaryRule(node.content, node.children[0].content, node.children[1].content) binary_rules.append(binary_rule) return binary_rules
def val_func(p, a, b): if p.signature.id != "Is": return False if not issubtype(a.signature.return_type, b.signature.return_type) and \ not issubtype(b.signature.return_type, a.signature.return_type): return False if not (issubtype(a.signature.return_type, 'number') or issubtype(a.signature.return_type, 'entity')): return False return BinaryRule.val_func(p, a, b)
def val_func(p, a, b): if p.signature.id != "CC": return False if a.signature.valence > 0: return False if b.signature.valence > 0: return False if a.signature.return_type != b.signature.return_type: return False return BinaryRule.val_func(p, a, b)
def get_self_rule(self): if self.is_leaf(): return None elif self.is_unary(): unary_rule = UnaryRule(self.content, self.children[0].content) return unary_rule elif self.is_binary(): binary_rule = BinaryRule(self.content, self.children[0].content, self.children[1].content) return binary_rule raise Exception()
def get_next_semantic_rules(self, words, syntax_tree, tag_model, parent_index, parent_signature, excluding_indices=set(), lifted_tag_rules=set()): assert isinstance(parent_signature, FunctionSignature) assert parent_signature.is_binary() next_tag_rules = self.get_next_tag_rules(words, syntax_tree, tag_model, parent_index, parent_signature, excluding_indices, lifted_tag_rules) binary_rules = [] for a_tag, b_tag in itertools.product(next_tag_rules, repeat=2): if issubtype(a_tag.signature.return_type, parent_signature.arg_types[0]) and \ issubtype(b_tag.signature.return_type, parent_signature.arg_types[1]): binary_rule = BinaryRule(words, syntax_tree, tag_model, parent_index, parent_signature, a_tag.index, a_tag.signature, b_tag.index, b_tag.signature) binary_rules.append(binary_rule) return binary_rules
def node_to_semantic_rules(words, syntax_tree, tags, node, lift_index=False): assert isinstance(node, Node) unary_rules = [] binary_rules = [] """ if issubtype(node.function_signature.return_type, 'truth'): start_rule = UnaryRule(words, syntax_tree, tags, None, function_signatures['StartTruth'], node.index, node.function_signature) else: print node.function_signature start_rule = None raise Exception() unary_rules.append(start_rule) """ stack = deque() stack.appendleft(node) while len(stack) > 0: curr_node = stack.pop() assert isinstance(curr_node, Node) if curr_node.function_signature.is_leaf(): continue elif curr_node.function_signature.is_unary(): child_node = curr_node.children[0] unary_rule = UnaryRule(words, syntax_tree, tags, curr_node.index, curr_node.function_signature, child_node.get_index(lift_index), child_node.function_signature) unary_rules.append(unary_rule) stack.appendleft(child_node) elif curr_node.function_signature.is_binary(): a_node, b_node = curr_node.children binary_rule = BinaryRule(words, syntax_tree, tags, curr_node.index, curr_node.function_signature, a_node.get_index(lift_index), a_node.function_signature, b_node.get_index(lift_index), b_node.function_signature) binary_rules.append(binary_rule) stack.appendleft(a_node) stack.appendleft(b_node) return unary_rules, binary_rules
def val_func(p, a, b): if p.signature.id in ('Is', 'CC') or a.signature.id in ('Is', 'CC') or b.signature.id in ('Is', 'CC'): return False return BinaryRule.val_func(p, a, b)