def Is(a, b): if is_number(a) or is_number(b): return Equals(a, b) a_name = a.__class__.__name__ b_name = b.__class__.__name__ if issubtype(a_name, 'polygon') or issubtype(a_name, 'line'): truth = set(a) == set(b) else: truth = a == b if truth: return TruthValue(0) else: return TruthValue(np.inf)
def prefix_to_formula(prefix): """ :param list prefix: :return FormulaNode: """ if isinstance(prefix, str): if prefix in abbreviations: return FormulaNode(signatures[abbreviations[prefix]], []) elif is_number(prefix): return FormulaNode(FunctionSignature(prefix, 'number', []), []) else: return FormulaNode(VariableSignature(prefix, 'number'), []) else: sig = signatures[abbreviations[prefix[0]]] children = [prefix_to_formula(child) for child in prefix[1:]] for idx, child in enumerate(children): child.signature.return_type = sig.arg_types[idx] child.return_type = sig.arg_types[idx] out = FormulaNode(sig, children) return out
def evaluate(formula, assignment): if not isinstance(formula, Node): return formula if not formula.is_grounded(assignment.keys()): return None if isinstance(formula, SetNode): if issubtype(formula.head.return_type, 'boolean'): out = reduce(operator.__and__, (evaluate(child, assignment) for child in formula.children), True) return out return formula if isinstance(formula.signature, VariableSignature): return assignment[formula.signature.id] elif is_number(formula.signature.id): return float(formula.signature.id) else: evaluated_args = [] for arg in formula.children: if isinstance(arg, FormulaNode): evaluated_args.append(evaluate(arg, assignment)) elif isinstance(arg, SetNode): evaluated_args.append( SetNode([ evaluate(arg_arg, assignment) for arg_arg in arg.children ])) else: evaluated_args.append(arg) # FIXME : rather than try/catch, check type matching try: out = getattr(this, formula.signature.id)(*evaluated_args) return out except: return TruthValue(np.inf)
def _ground_variable(match_parse, variable, references={}): assert isinstance(variable, FormulaNode) assert isinstance(match_parse, MatchParse) return_type = variable.return_type graph_parse = match_parse.graph_parse core_parse = graph_parse.core_parse variable_signature = variable.signature if variable_signature.id in signatures: # pass What, Which, etc. return variable elif variable_signature.id in match_parse.graph_parse.core_parse.variable_assignment.keys( ): # pass point_0, point_1, etc. return variable elif isinstance(variable_signature, VariableSignature) and variable_signature.is_ref(): # @v_1, etc. return references[variable_signature.name] elif return_type == 'number': if is_number(variable_signature.name): return variable elif len(variable_signature.name) == 1: # x, y, z, etc. Need to redefine id (id shouldn't be tuple). return FormulaNode( VariableSignature(variable_signature.name, return_type), []) elif len(variable_signature.name ) == 2 and variable_signature.name.isupper(): new_leaf = FormulaNode( VariableSignature(variable.signature.id, "line", name=variable.signature.name), []) return FormulaNode(signatures['LengthOf'], [_ground_variable(match_parse, new_leaf)]) elif return_type == 'point': if len(variable_signature.name) == 1: return match_parse.match_dict[variable_signature.name][0] else: points = get_all_instances(graph_parse, 'point', True) return SetNode(points.values()) elif return_type == 'line': if len(variable_signature.name ) == 1 and variable_signature.name in match_parse.match_dict: line = match_parse.match_dict[variable_signature.name][0] return line elif len(variable_signature.name ) == 2 and variable_signature.name.isupper(): label_a, label_b = variable_signature.name point_a = match_parse.match_dict[label_a][0] point_b = match_parse.match_dict[label_b][0] return FormulaNode(signatures['Line'], [point_a, point_b]) """ elif variable_signature.name == 'hypotenuse': def func(x): l, t = x formula = FormulaNode(signatures['IsHypotenuseOf'], (l,t)) tv = core_parse.evaluate(formula) return tv.norm lines = get_all_instances(graph_parse, 'line', True).values() triangles = get_all_instances(graph_parse, 'triangle', True).values() line, triangle = min(itertools.product(lines, triangles), key=func) return line """ else: lines = get_all_instances(graph_parse, 'line', True) return SetNode(lines.values()) elif return_type == 'circle': if len(variable_signature.name) == 1: center_label = variable_signature.name center = match_parse.match_dict[center_label][0] center_idx = int(center.signature.name.split("_")[1]) return graph_parse.circle_dict[center_idx][0]['variable'] # radius = match_parse.graph_parse.core_parse.radius_variables[center_idx][0] elif variable_signature.name == 'circle': circles = get_all_instances(graph_parse, 'circle', True) return SetNode(circles.values()) else: raise Exception() elif return_type == 'angle': # TODO : if len(variable_signature.name ) == 3 and variable_signature.name.isupper(): label_a, label_b, label_c = variable_signature.name point_a = match_parse.match_dict[label_a][0] point_b = match_parse.match_dict[label_b][0] point_c = match_parse.match_dict[label_c][0] out = FormulaNode(signatures['Angle'], [point_a, point_b, point_c]) measure = evaluate(FormulaNode(signatures['MeasureOf'], [out]), core_parse.variable_assignment) if measure > np.pi: out = FormulaNode(signatures['Angle'], [point_c, point_b, point_a]) return out elif len(variable_signature.name ) == 1 and variable_signature.name.isupper(): angles = get_all_instances(graph_parse, 'angle', True) p = match_parse.match_dict[variable_signature.name][0] for formula in angles.values(): if formula.children[1].signature == p.signature: measure = evaluate( FormulaNode(signatures['MeasureOf'], [formula]), core_parse.variable_assignment) if measure > np.pi: continue return formula elif len(variable_signature.name ) == 1 and variable_signature.name.islower(): return match_parse.match_dict[variable_signature.name][0] elif return_type == 'arc': if len(variable_signature.name ) == 2 and variable_signature.name.isupper(): point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] test_arc = get_instances(graph_parse, 'arc', False, *point_keys).values()[0] if MeasureOf(test_arc) > np.pi: point_keys = [point_keys[1], point_keys[0]] arc = get_instances(graph_parse, 'arc', True, *point_keys).values()[0] return arc else: arcs = get_all_instances(graph_parse, 'arc', True) return SetNode(arcs.values()) elif return_type == 'triangle': if variable_signature.name.isupper() and len( variable_signature.name) == 3: point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] triangles = get_instances(graph_parse, 'triangle', True, *point_keys) return triangles.values()[0] else: triangles = get_all_instances(graph_parse, 'triangle', True) return SetNode(triangles.values()) elif return_type == 'quad': if variable_signature.name.isupper() and len( variable_signature.name) == 4: point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] quads = get_instances(graph_parse, 'quad', True, *point_keys) return quads.values()[0] else: quads = get_all_instances(graph_parse, 'quad', True) return SetNode(quads.values()) elif return_type == 'hexagon': if variable_signature.name.isupper() and len( variable_signature.name) == 6: point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] hexagons = get_instances(graph_parse, 'hexagon', True, *point_keys) return hexagons.values()[0] else: quads = get_all_instances(graph_parse, 'hexagon', True) return SetNode(quads.values()) elif return_type == 'polygon': if variable_signature.name.isupper(): point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] polygons = get_instances(graph_parse, 'polygon', True, *point_keys) return polygons.values()[0] else: polygons = get_all_instances(graph_parse, 'polygon', True) return SetNode(polygons.values()) elif return_type == 'twod': circles = get_all_instances(graph_parse, 'circle', True) polygons = get_all_instances(graph_parse, 'polygon', True) return SetNode(polygons.values() + circles.values()) elif return_type == 'oned': lines = get_all_instances(graph_parse, 'line', True) arcs = get_all_instances(graph_parse, 'arc', True) return SetNode(lines.values() + arcs.values()) #logging.warning("failed to ground variable: %r" % variable) raise Exception()
def _ground_variable(match_parse, variable, references={}): assert isinstance(variable, FormulaNode) assert isinstance(match_parse, MatchParse) return_type = variable.return_type graph_parse = match_parse.graph_parse core_parse = graph_parse.core_parse variable_signature = variable.signature if variable_signature.id in signatures: # pass What, Which, etc. return variable elif variable_signature.id in match_parse.graph_parse.core_parse.variable_assignment.keys(): # pass point_0, point_1, etc. return variable elif isinstance(variable_signature, VariableSignature) and variable_signature.is_ref(): # @v_1, etc. return references[variable_signature.name] elif return_type == "number": if is_number(variable_signature.name): return variable elif len(variable_signature.name) == 1: # x, y, z, etc. Need to redefine id (id shouldn't be tuple). return FormulaNode(VariableSignature(variable_signature.name, return_type), []) elif len(variable_signature.name) == 2 and variable_signature.name.isupper(): new_leaf = FormulaNode(VariableSignature(variable.signature.id, "line", name=variable.signature.name), []) return FormulaNode(signatures["LengthOf"], [_ground_variable(match_parse, new_leaf)]) else: # ABC: number -> just variable return variable elif return_type == "point": if len(variable_signature.name) == 1: return match_parse.match_dict[variable_signature.name][0] else: points = get_all_instances(graph_parse, "point", True) return SetNode(points.values()) elif return_type == "line": if len(variable_signature.name) == 1 and variable_signature.name in match_parse.match_dict: line = match_parse.match_dict[variable_signature.name][0] return line elif len(variable_signature.name) == 2 and variable_signature.name.isupper(): label_a, label_b = variable_signature.name point_a = match_parse.match_dict[label_a][0] point_b = match_parse.match_dict[label_b][0] return FormulaNode(signatures["Line"], [point_a, point_b]) else: lines = get_all_instances(graph_parse, "line", True) return SetNode(lines.values()) elif return_type == "circle": if len(variable_signature.name) == 1: center_label = variable_signature.name center = match_parse.match_dict[center_label][0] center_idx = int(center.signature.name.split("_")[1]) return graph_parse.circle_dict[center_idx][0]["variable"] # radius = match_parse.graph_parse.core_parse.radius_variables[center_idx][0] elif variable_signature.name == "circle": circles = get_all_instances(graph_parse, "circle", True) return SetNode(circles.values()) else: raise Exception() elif return_type == "angle": # TODO : if len(variable_signature.name) == 3 and variable_signature.name.isupper(): label_a, label_b, label_c = variable_signature.name point_a = match_parse.match_dict[label_a][0] point_b = match_parse.match_dict[label_b][0] point_c = match_parse.match_dict[label_c][0] out = FormulaNode(signatures["Angle"], [point_a, point_b, point_c]) measure = evaluate(FormulaNode(signatures["MeasureOf"], [out]), core_parse.variable_assignment) if measure > np.pi: out = FormulaNode(signatures["Angle"], [point_c, point_b, point_a]) return out elif len(variable_signature.name) == 1 and variable_signature.name.isupper(): angles = get_all_instances(graph_parse, "angle", True) p = match_parse.match_dict[variable_signature.name][0] for formula in angles.values(): if formula.children[1].signature == p.signature: measure = evaluate(FormulaNode(signatures["MeasureOf"], [formula]), core_parse.variable_assignment) if measure > np.pi: continue return formula elif ( len(variable_signature.name) == 1 and variable_signature.name.islower() and variable_signature.name in match_parse.match_dict ): return match_parse.match_dict[variable_signature.name][0] else: angles = get_all_instances(graph_parse, "angle", True) return SetNode(angles.values()) elif return_type == "arc": if len(variable_signature.name) == 2 and variable_signature.name.isupper(): point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] test_arc = get_instances(graph_parse, "arc", False, *point_keys).values()[0] if MeasureOf(test_arc) > np.pi: point_keys = [point_keys[1], point_keys[0]] arc = get_instances(graph_parse, "arc", True, *point_keys).values()[0] return arc else: arcs = get_all_instances(graph_parse, "arc", True) return SetNode(arcs.values()) elif return_type == "triangle": if variable_signature.name.isupper() and len(variable_signature.name) == 3: point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] triangles = get_instances(graph_parse, "triangle", True, *point_keys) return triangles.values()[0] else: triangles = get_all_instances(graph_parse, "triangle", True) return SetNode(triangles.values()) elif return_type == "quad": if variable_signature.name.isupper() and len(variable_signature.name) == 4: point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] quads = get_instances(graph_parse, "quad", True, *point_keys) return quads.values()[0] else: quads = get_all_instances(graph_parse, "quad", True) return SetNode(quads.values()) elif return_type == "hexagon": if variable_signature.name.isupper() and len(variable_signature.name) == 6: point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] hexagons = get_instances(graph_parse, "hexagon", True, *point_keys) return hexagons.values()[0] else: quads = get_all_instances(graph_parse, "hexagon", True) return SetNode(quads.values()) elif return_type == "polygon": if variable_signature.name.isupper(): point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] polygons = get_instances(graph_parse, "polygon", True, *point_keys) return polygons.values()[0] else: polygons = get_all_instances(graph_parse, "polygon", True) return SetNode(polygons.values()) elif return_type == "twod": circles = get_all_instances(graph_parse, "circle", True) polygons = get_all_instances(graph_parse, "polygon", True) return SetNode(polygons.values() + circles.values()) elif return_type == "oned": lines = get_all_instances(graph_parse, "line", True) arcs = get_all_instances(graph_parse, "arc", True) return SetNode(lines.values() + arcs.values()) logging.error("failed to ground variable: %r" % variable) return variable
def tag_f(a, b, c): assert len(c) == 1 if c[0][0].isupper() or is_number(c[0]): return 'function', c[0] else: return 'variable', c[0]
def has_signature(self, id_): if self.signature.id == id_: return True return any(not is_number(child) and child.has_signature(id_) for child in self.children)
def has_signature(self, id_): return any(not is_number(child) and child.has_signature(id_) for child in self.children)
def generate_tag_rules(self, syntax_parse): tag_rules = set() for span in syntax_parse.iterate_spans(): prev_word = syntax_parse.get_word(span[0]-1) if prev_word is not None: prev_word = prev_word.lower() words = syntax_parse.get_words(span) if len(words) == 1: return_types = [] word = words[0] if is_number(word): tag_rule = TagRule(syntax_parse, span, FunctionSignature(word, 'number', [])) tag_rules.add(tag_rule) continue elif word.startswith("@v"): return_types.append('number') elif word.islower() and len(word) == 1: pos = syntax_parse.get_pos_by_index(span[0]) if pos == "DT": continue return_types.append('line') return_types.append('angle') return_types.append('number') elif word.isupper(): if len(word) == 1: pos = syntax_parse.get_pos_by_index(span[0]) if pos == "DT": continue if prev_word in ('circle', 'circles'): return_types.append('circle') elif prev_word in ('angle', 'angles'): return_types.append('angle') elif prev_word in ('line', 'lines'): return_types.append('line') else: return_types.append('point') elif len(word) == 2: if prev_word in ('arc', 'arcs'): return_types.append('arc') else: return_types.extend(['number', 'line']) elif len(word) == 3: return_types.extend(['angle', 'triangle', 'number']) elif len(word) == 4: return_types.extend(['quad']) elif len(word) == 6: return_types.extend(['hexagon']) else: return_types.extend(['polygon']) curr_tag_rules = [TagRule(syntax_parse, span, VariableSignature((span,rt), rt, name=word)) for rt in return_types] for tag_rule in curr_tag_rules: tag_rules.add(tag_rule) if len(curr_tag_rules) > 0: continue if words in self.lexicon: for return_type, name in self.lexicon[words]: if name[0].isupper(): signature = signatures[name] else: signature = VariableSignature((span,return_type), return_type, name=name) tag_rule = TagRule(syntax_parse, span, signature) tag_rules.add(tag_rule) return tag_rules