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
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
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 == []
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 == []
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'}
def test_nlm_instance_call_graphnode(): start = GraphNode("Person", "AliceThree") res = mem(start) query = res[0] assert isinstance(query, GraphNode) == True assert query.label == "Person" assert query.name == "AliceThree" assert query.props == {'age': 22, 'sex': 'male'}
def test_query_with_graph_node(): qin1 = GraphNode("Person", "Alice") res1 = nlmg.query(qin1) assert res1 != None qin2 = GraphNode("Person", "AliceOne") res2 = nlmg.query(qin2) assert dict(res2[0]) == { "name": "AliceOne", "age": 20, "sex": "female", "occupation": "teacher" } qin3 = GraphNode("Person", "Alice", {"age": 21}) res3 = nlmg.query(qin3) assert res3 == []
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'}
def test_no_props_update_node(make_updated_node): node = nlmg.check_update_node(GraphNode("Person", "Alice")) new = nlmg.check_update_node(make_updated_node, update_props=True) assert new.identity == node.identity assert dict(new) == { "name": "Alice", "age": 20, "sex": "female", "occupation": "teacher" } nlmg.graph.delete_all()
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'}
def extract_relation_or_node(self, ext_in: ExtractorInput): try: from_dict(data_class=ExtractorInput, data={ "text": ext_in.text, "intent": ext_in.intent, "entities": ext_in.entities }) except Exception as e: raise ParameterError ext_out = NLMExtractor.extract(ext_in) return GraphNode("Demo", "demo_node")
def make_nodes_for_query(): node1 = GraphNode("Person", "AliceOne", { "age": 20, "sex": "female", "occupation": "teacher" }) node2 = GraphNode("Person", "AliceTwo", { "age": 21, "occupation": "teacher" }) node3 = GraphNode("Person", "AliceThree", {"age": 22, "sex": "male"}) node4 = GraphNode("Person", "AliceFour", { "age": 23, "sex": "male", "occupation": "doctor" }) node5 = GraphNode("Person", "AliceFive", { "age": 24, "sex": "female", "occupation": "scientist" }) node6 = GraphNode("Person", "AliceSix", {"age": 25, "sex": "female"}) for node in [node1, node2, node3, node4, node5, node6]: nlmg.check_update_node(node) return (node1, node2, node3, node4, node5, node6)
def test_update_graphrelationship_without_kind(make_relation): make_relation.kind = None relation = nlmg.add(make_relation) make_relation.start = GraphNode("Person", "Bob", { "age": 23, "occupation": "scientist" }) new = nlmg.update(make_relation) assert len(new) == 2 assert dict(new[0]) == { "name": "Bob", "age": 23, "occupation": "scientist" } nlmg.graph.delete_all()
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
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}
ExtractorInput(text=inputs.text)) elif isinstance(inputs, ExtractorInput): 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)
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"}
def make_updated_node(): props = {"age": 20, "sex": "female", "occupation": "teacher"} node = GraphNode("Person", "Alice", props) return node
def test_query_with_graph_node_without_fuzzy(): qin = GraphNode("Person", "Alice", {"age": 21}) res = nlmg.query(qin, fuzzy=False) assert res == []
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"}
def convert_node_to_graphnode(node): label = str(node.labels)[1:] dct = dict(node) name = dct.pop("name") gn = GraphNode(label, name, dct) return gn
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")
def make_node(): props = {"age": 20, "sex": "female"} node = GraphNode("Person", "Alice", props) return node
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)
def make_node_no_props(): return GraphNode("Person", "Alice")
@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)