Esempio n. 1
0
def test_query_with_graph_relation_without_fuzzy_node():
    qin1 = GraphRelation(GraphNode("Person", "Alice"),
                         GraphNode("Animal", "Monkey"), "LOVES")
    res1 = nlmg.query(qin1, topn=5, fuzzy=False)
    assert res1 == []

    qin2 = GraphRelation(GraphNode("Person", "Alice"),
                         GraphNode("Animal", "Monkey"), "WRONG")
    res2 = nlmg.query(qin2, fuzzy=False)
    assert res2 == []
Esempio n. 2
0
def test_query_with_graph_relation_without_kind_with_fuzzy_node():
    qin1 = GraphRelation(GraphNode("Person", "Alice"),
                         GraphNode("Animal", "Monkey"))
    res1 = nlmg.query(qin1, topn=5, fuzzy=True)
    assert len(res1) >= 1

    qin1 = GraphRelation(GraphNode("Person", "AliceTw"),
                         GraphNode("Person", "AliceTh"))
    res1 = nlmg.query(qin1, topn=5, fuzzy=True)
    assert len(res1) == 1
Esempio n. 3
0
def test_query_with_graph_relation_not_exist_nodes_with_relation():
    qin2 = GraphRelation(GraphNode("Person", "AliceOne1"),
                         GraphNode("Person", "AliceFive1"), "LOVES")
    res2 = nlmg.query(qin2, topn=5)
    assert res2 == []

    # start, end are None
    qin6 = GraphRelation(GraphNode("Fruit", "Apple"),
                         GraphNode("Animal", "Monkey"), "KNOWS")
    res6 = nlmg.query(qin6)
    assert res6 == []
Esempio n. 4
0
def test_query_with_graph_relation_with_fuzzy_node():
    qin1 = GraphRelation(GraphNode("Person", "AliceTw"),
                         GraphNode("Person", "AliceTh"), "LOVES")
    res1 = nlmg.query(qin1, topn=5, fuzzy=True)
    assert len(res1) == 1

    qin2 = GraphRelation(GraphNode("Person", "Alice"),
                         GraphNode("Animal", "Monkey"), "WRONG")
    res2 = nlmg.query(qin2, fuzzy=True)
    assert len(res2) == 0

    qin3 = GraphRelation(GraphNode("Person", "Alice"),
                         GraphNode("Animal", "Monkey"), "WRONG")
    res3 = nlmg.query(qin3, fuzzy=True)
    assert len(res3) == 0
Esempio n. 5
0
def convert_relation_to_graph_relation(relation):
    start = convert_node_to_graphnode(relation.start_node)
    end = convert_node_to_graphnode(relation.end_node)
    kind = list(relation.types())[0]
    props = dict(relation)
    gr = GraphRelation(start, end, kind, props)
    return gr
Esempio n. 6
0
def test_query_with_graph_relation_not_exist_start_with_relation():
    # start is None
    qin4 = GraphRelation(GraphNode("Animal", "Monkey"),
                         GraphNode("Person", "AliceTwo"), "LOVES")
    res4 = nlmg.query(qin4)
    assert dict(res4[0]) == {}
    assert len(res4) == 1
Esempio n. 7
0
def test_nlm_instance_call_graphrelation():
    start = GraphNode("Person", "AliceThree")
    end = GraphNode("Person", "AliceOne")
    relation = GraphRelation(start, end, "LOVES")
    res = mem(relation)
    query = res[0]
    assert isinstance(query, GraphRelation)
    assert query.kind == "LOVES"
    assert query.props == {'from': 2011, 'roles': 'husband'}
    assert query.start.label == "Person"
    assert query.start.name == "AliceThree"
    assert query.start.props == {'age': 22, 'sex': 'male'}
Esempio n. 8
0
def test_query_add_update_without_addinexist_with_fuzzynode():
    start = GraphNode("Person", "AliceTh")
    end = GraphNode("Person", "AliceO")
    relation = GraphRelation(start, end, "LOVES")
    res1 = mem(relation, add_inexistence=False, fuzzy_node=True)
    query1 = res1[0]
    assert len(res1) == 1
    assert query1.kind == "LOVES"
    assert query1.props == {'from': 2011, 'roles': 'husband'}
    assert query1.start.label == "Person"
    assert query1.start.name == "AliceThree"
    assert query1.start.props == {'age': 22, 'sex': 'male'}

    res2 = mem(start, add_inexistence=False, fuzzy_node=True)
    query2 = res2[0]
    assert len(res2) == 1
    assert query2.label == "Person"
    assert query2.name == "AliceThree"
    assert query2.props == {'age': 22, 'sex': 'male'}
Esempio n. 9
0
def test_query_add_update_with_addinexist_without_fuzzynode():
    start = GraphNode("Person", "AliceSeven", {'age': 23, 'sex': 'male'}) # node does not exist
    end = GraphNode("Person", "AliceOne", {'age': 22}) # exist age = 20
    relation = GraphRelation(start, end, "LIKES", {'from': 2011, 'roles': 'friend'})

    res2 = mem(start, add_inexistence=True, fuzzy_node=False)
    assert res2 == []

    res0 = mem(end, update_props=True, fuzzy_node=False)
    query1 = res0[0]
    assert query1.props == {"age": 22, "sex": "female", "occupation": "teacher"}
    assert query1.label == "Person"
    assert query1.name == "AliceOne"

    res1 = mem(relation, add_inexistence=True, fuzzy_node=False)
    assert res1 == []

    res3 = mem(relation)
    query = res3[0]
    assert query.kind == "LIKES"
    assert query.props == {'from': 2011, 'roles': 'friend'}
Esempio n. 10
0
def make_relations_for_query(make_nodes_for_query):
    (node1, node2, node3, node4, node5, node6) = make_nodes_for_query
    rela1 = GraphRelation(node3, node1, "LOVES", {
        "roles": "husband",
        "from": 2011
    })
    rela2 = GraphRelation(node3, node1, "WORK_WITH", {
        "roles": "boss",
        "from": 2009
    })
    rela3 = GraphRelation(node2, node3, "LOVES", {
        "roles": "student",
        "at": 2005
    })
    rela4 = GraphRelation(node4, node2, "LOVES")
    rela5 = GraphRelation(node5, node6, "WORK_WITH", {"roles": "workmates"})
    rela6 = GraphRelation(node6, node4, "LOVES", {"from": 2012})
    rela7 = GraphRelation(node1, node5, "KNOWS", {"roles": "friend"})
    for rela in [rela1, rela2, rela3, rela4, rela5, rela6, rela7]:
        nlmg.check_update_relationship(rela)
Esempio n. 11
0
            ext_out = self.extract_relation_or_node(inputs)
        else:
            return []
        return self.query_add_update(ext_out, **kwargs)


if __name__ == '__main__':
    from configs.config import neo_sche, neo_host, neo_port, neo_user, neo_pass
    from py2neo.database import Graph

    graph = Graph(port=7688)
    nlm = NLMLayer(graph=graph)

    start = GraphNode("Person", "AliceThreeNotExist")
    end = GraphNode("Animal", "Monkey")
    end = GraphNode("Person", "AliceOne")
    relation = GraphRelation(start, end, "LIKES")

    nlu_out = GraphRelation(start, end, "LOVES")

    etin = ExtractorInput(text="hhh")

    # nlu_out = "1"
    res = nlm(relation, add_inexistence=True)
    # res = nlm.query_add_update("MATCH (a:Person) RETURN a.age, a.name LIMIT 4")

    nlm2 = NLMLayer(graph=graph)
    assert (nlm2 == nlm)

    print("RES: ", res)
Esempio n. 12
0
    @property
    def relationships(self) -> types.GeneratorType:
        """all relations (a generator)"""
        return iter(self.graph.relationships.match())

    def excute(self, cypher) -> dict:
        """
        Be careful to use this function.
        Especially when you're updating the database.
        This function will not check the duplicated nodes or relationships.
        """
        try:
            run = self.graph.run(cypher)
            return dict(run.stats())
        except Exception as e:
            raise InputError


if __name__ == '__main__':
    import os
    import sys
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(root)
    nlmg = NLMGraph(graph=Graph(port=7688))

    start = GraphNode("Person", "AliceThree")
    end = GraphNode("Person", "AliceOne")
    qin = GraphRelation(start, end)
    res = nlmg.query(qin, topn=2, fuzzy=True)
    print(res)
Esempio n. 13
0
def make_relaition_no_props():
    start = GraphNode("Person", "Bob", {"age": 22, "occupation": "engineer"})
    end = GraphNode("Person", "Alice", {"age": 20, "sex": "female"})
    return GraphRelation(start, end, "LOVES")
Esempio n. 14
0
def test_query_with_graph_relation_not_exist_end_with_relation():
    # end is None
    qin5 = GraphRelation(GraphNode("Person", "AliceTwo"),
                         GraphNode("Animal", "Monkey"), "LOVES")
    res5 = nlmg.query(qin5)
    assert dict(res5[0]) == {"roles": "student", "at": 2005}
Esempio n. 15
0
def make_updated_relation():
    start = GraphNode("Person", "Bob", {"age": 22, "occupation": "engineer"})
    end = GraphNode("Person", "Alice", {"age": 20, "sex": "female"})
    props = {"roles": "husband"}
    return GraphRelation(start, end, "LOVES", props)
Esempio n. 16
0
def test_query_with_graph_relation_normal(make_relations_for_query):
    qin = GraphRelation(GraphNode("Person", "AliceOne"),
                        GraphNode("Person", "AliceFive"), "KNOWS")
    res = nlmg.query(qin)
    assert dict(res[0]) == {"roles": "friend"}
Esempio n. 17
0
def test_query_with_graph_relation_exist_nodes_without_relation():
    qin = GraphRelation(GraphNode("Person", "AliceOne"),
                        GraphNode("Person", "AliceFive"), "WRONG")
    res = nlmg.query(qin)
    assert dict(res[0]) == {"roles": "friend"}