def get_semantic_relations(grounded_syntax, from_grounded_token,
                           to_grounded_token, arg_idx):
    basic_ontology = grounded_syntax.basic_ontology
    semantic_relations = {}
    grounded_syntax_paths = get_grounded_syntax_paths(grounded_syntax,
                                                      from_grounded_token,
                                                      to_grounded_token)
    if len(grounded_syntax_paths) == 0:
        return semantic_relations

    grounded_syntax_path = min(grounded_syntax_paths.values(),
                               key=lambda p: get_grounded_syntax_path_cost(p))
    ontology_paths = get_ontology_paths(
        basic_ontology, from_grounded_token.ground.arg_types[arg_idx],
        to_grounded_token.ground)
    for ontology_path in ontology_paths.values():
        augmented_ontology_path = OntologyPath(basic_ontology,
                                               [from_grounded_token.ground] +
                                               ontology_path.path_nodes,
                                               ontology_path.key)
        semantic_relation = SemanticRelation(from_grounded_token,
                                             to_grounded_token, arg_idx,
                                             grounded_syntax_path,
                                             augmented_ontology_path)
        semantic_relations[semantic_relation.key] = semantic_relation

    return semantic_relations
def get_type_relations(from_type, to_grounded_token):
    ontology_paths = get_ontology_paths(to_grounded_token.basic_ontology,
                                        from_type, to_grounded_token.ground)

    type_relations = {}
    for key, ontology_path in ontology_paths.iteritems():
        type_relation = TypeRelation(from_type, to_grounded_token, ontology_path)
        type_relations[key] = type_relation

    return type_relations
Example #3
0
def get_type_relations(from_type, to_grounded_token):
    ontology_paths = get_ontology_paths(to_grounded_token.basic_ontology,
                                        from_type, to_grounded_token.ground)

    type_relations = {}
    for key, ontology_path in ontology_paths.iteritems():
        type_relation = TypeRelation(from_type, to_grounded_token,
                                     ontology_path)
        type_relations[key] = type_relation

    return type_relations
Example #4
0
def test_get_grounded_syntax_paths():
    string = "Circle O has a radius of 5."
    tokens = string_to_tokens(string)
    syntax = create_syntax(tokens, 1)
    grounded_syntax = get_grounded_syntax(syntax, ontology_semantics, geowordnet, 0.99)
    circle = grounded_syntax.grounded_tokens[(0, 'circle')]
    radius = grounded_syntax.grounded_tokens[(4, 'radiusOf')]
    grounded_syntax_paths = get_grounded_syntax_paths(grounded_syntax, circle, radius)
    ontology_paths = get_ontology_paths(grounded_syntax.basic_ontology, radius.ground.arg_types[0], circle.ground)
    print(ontology_paths)
    print(get_grounded_syntax_path_cost(grounded_syntax_paths[0]))
    print(get_ontology_path_cost(ontology_paths[0]))
    pprint(grounded_syntax_paths)
    pprint(ontology_paths)

    grounded_syntax.display_graphs()
Example #5
0
def test_get_grounded_syntax_paths():
    string = "Circle O has a radius of 5."
    tokens = string_to_tokens(string)
    syntax = create_syntax(tokens, 1)
    grounded_syntax = get_grounded_syntax(syntax, ontology_semantics,
                                          geowordnet, 0.99)
    circle = grounded_syntax.grounded_tokens[(0, 'circle')]
    radius = grounded_syntax.grounded_tokens[(4, 'radiusOf')]
    grounded_syntax_paths = get_grounded_syntax_paths(grounded_syntax, circle,
                                                      radius)
    ontology_paths = get_ontology_paths(grounded_syntax.basic_ontology,
                                        radius.ground.arg_types[0],
                                        circle.ground)
    print(ontology_paths)
    print(get_grounded_syntax_path_cost(grounded_syntax_paths[0]))
    print(get_ontology_path_cost(ontology_paths[0]))
    pprint(grounded_syntax_paths)
    pprint(ontology_paths)

    grounded_syntax.display_graphs()
Example #6
0
def test_get_ontology_path_cost():
    """
    Needs to be moved to semantics package.
    """
    o = basic_ontology
    s0 = Function('5', [], o.types['number'])
    s1 = Function('O', [], o.types['reference'])
    oo = augment_ontology(o, {s0.name: s0, s1.name: s1})
    s2 = o.functions['equal']
    s3 = o.functions['radiusOf']
    s4 = o.functions['isRadiusOf']
    s5 = o.functions['circle']
    truth = o.types['truth']
    number = o.types['number']
    perp = oo.functions['isPerpendicularTo']
    line = o.types['line']
    ref = o.types['reference']
    paths = get_ontology_paths(oo, ref, s1)
    for path in paths.values():
        print(path)
        print(get_ontology_path_cost(path))
Example #7
0
def test_get_ontology_path_cost():
    """
    Needs to be moved to semantics package.
    """
    o = basic_ontology
    s0 = Function('5', [], o.types['number'])
    s1 = Function('O', [], o.types['reference'])
    oo = augment_ontology(o, {s0.name: s0, s1.name: s1})
    s2 = o.functions['equal']
    s3 = o.functions['radiusOf']
    s4 = o.functions['isRadiusOf']
    s5 = o.functions['circle']
    truth = o.types['truth']
    number = o.types['number']
    perp = oo.functions['isPerpendicularTo']
    line = o.types['line']
    ref = o.types['reference']
    paths = get_ontology_paths(oo, ref, s1)
    for path in paths.values():
        print(path)
        print(get_ontology_path_cost(path))