def annotate_gold_frames(graph, args): from py2neo import NodeMatcher matcher = NodeMatcher(graph) import csv # First reading in the annotated data into a dictionary. annotated = {} with open("../data/tweets_manual_annotated.csv", encoding='utf-8', mode='r') as f: csvfile = csv.DictReader(f, delimiter=';') for line in csvfile: try: frame = int(line['frames']) annotated[line['text_OLD']] = frame except Exception: continue # next step, write them to their respective tweets tx = graph.begin() print("Writing %d frame numbers to graph.." % len(annotated)) counter = 0 with tqdm(total=len(annotated)) as pbar: for tweet in annotated.keys(): # query = "MATCH (t:Tweet) WHERE t.text_OLD = \"%s\" RETURN t;" % tweet # responses = list(graph.run(query)) responses = list(matcher.match("Tweet", text_OLD=("%s" % tweet))) if len(responses) != 1: print("%d RESPONSES FOR TWEET %s" % (len(responses), tweet)) else: node = responses[0] tx.merge(node) node['frame'] = annotated[tweet] tx.push(node) pbar.update(1) counter += 1 tx.commit() print("DONE. %d/%d written." % (counter, len(annotated)))
def test_gets(self): folder = os.path.dirname(os.path.abspath(__file__)) test_driller = Driller(os.path.join(folder, 'cnfg_simple.yml')) test_driller.drill_batch() st_date = datetime.strptime('14 May, 2020 00:00', '%d %B, %Y %H:%M').timestamp() end_date = datetime.strptime('15 May, 2020 02:00', '%d %B, %Y %H:%M').timestamp() n_matcher = NodeMatcher(test_driller.graph) r_matcher = RelationshipMatcher(test_driller.graph) com_miner = CommitMiner(test_driller.graph, n_matcher, r_matcher) all_com = com_miner.get_all() assert len(all_com) == 8 all_com_dates = com_miner.get_between_dates(st_date, end_date) assert len(all_com_dates) == 8 c_files = com_miner.get_commit_files( 'ad98f8594c15b1ebc4be4f20d849bcc0edf69ec574c33dfd84b7792d') assert len(c_files) == 3 c_file_updates = com_miner.get_commit_file_updates( 'ad98f8594c15b1ebc4be4f20d849bcc0edf69ec574c33dfd84b7792d') assert len(c_file_updates) == 3 c_methods = com_miner.get_commit_methods( 'ad98f8594c15b1ebc4be4f20d849bcc0edf69ec574c33dfd84b7792d') assert len(c_methods) == 3 c_method_updates = com_miner.get_commit_method_updates( 'ad98f8594c15b1ebc4be4f20d849bcc0edf69ec574c33dfd84b7792d') assert len(c_method_updates) == 3 test_driller.clean()
def test_nodes_index(self): folder = os.path.dirname(os.path.abspath(__file__)) test_driller = Driller(os.path.join(folder, 'cnfg_simple.yml')) test_driller.drill_batch() # test that all nodes were indexed node_matcher = NodeMatcher(test_driller.graph) all_commits = list(node_matcher.match("Commit")) assert len(all_commits) == 8 all_devs = list(node_matcher.match("Developer")) assert len(all_devs) == 2 all_files = list(node_matcher.match("File")) assert len(all_files) == 6 all_methods = list(node_matcher.match("Method")) assert len(all_methods) == 5 all_branches = list(node_matcher.match("Branch")) assert len(all_branches) == 1 test_driller.clean()
def addEntity(request): if request.method == 'GET': return render(request, 'addEntity.html', {'entitytypes': entitytypes}) else: label = request.POST.get('fid') entity_name = request.POST.get('entityname') for key in entity_map: if key == label: entity_label = entity_map[key] matcher = NodeMatcher(g) result = matcher.match(entity_label, name=entity_name).first() if result is not None: print(result) messages.error(request, '节点已存在') return render(request, 'addEntity.html', {'entitytypes': entitytypes}) else: node = Node(entity_label, name=entity_name) g.create(node) messages.success(request, '添加成功') return render(request, 'addEntity.html', {'entitytypes': entitytypes})
def process_item(self, item, spider): matcher = NodeMatcher(spider.graph) a = Node("Artist", name=item['Artist name']) spider.graph.merge(a, "Artist", "name") for g in item['Genres']: a[g] = 1 spider.graph.push(a) b = Node("Artist", name=item['Feat']) b1 = matcher.match("Artist", name=item['Feat']).first() if not b1: spider.graph.merge(b, "Artist", "name") spider.graph.push(b) else: b = b1 ab = Relationship(a, 'FEAT', b) rel = spider.graph.match((a, b), r_type="FEAT").first() spider.graph.merge(ab) if rel: ab['Count'] = rel['Count'] + 1 else: ab['Count'] = 1 spider.graph.push(ab) return item
def establish_relation(graph, file_obj): rdf = file_obj.readline() matcher = NodeMatcher(graph) while rdf != "": if get_predicate(rdf) == "国家": node_weapon = matcher.match("weapon", name=get_subject(rdf)).first() if node_weapon == None: print("error! " + get_subject(rdf) + " not exist!") else: if get_object(rdf) == "苏/俄": node_country = matcher.match("country", name="苏联").first() else: node_country = matcher.match("country", name=get_object(rdf)).first() if node_country != None: create_relationship = Relationship(node_weapon, "create_from", node_country) graph.create(create_relationship) else: print(get_object(rdf) + "不存在") rdf = file_obj.readline()
def write_info(disease_name, population_name): graph = Graph("bolt://localhost:7687", username="******", password='******') node_matcher = NodeMatcher(graph) try: # 疾病名称 disease = node_matcher.match("disease").where( f"_.name = '{disease_name}'").first() # 此疾病节点还未创建 if disease is None: disease = Node('disease', name=disease_name) graph.create(disease) # 好发人群 susceptible_population population = node_matcher.match("population").where( f"_.name = '{population_name}'").first() if population is None: population = Node('population', name=population_name) disease_population = Relationship(disease, 'disease_population', population) graph.create(disease_population) except Exception as e: print(f"{disease_name}, {population}, {e}") return False
def prepare_neo4j(): # 连接数据库 GraphT = Graph("http://localhost:7474", username="******", password='******') # 创建节点 for node in seen: syn = getnode(node) syn_name = syn.lemma_names()[0] p = Node("Seen", id=node, name=syn_name) GraphT.create(p) for node in unseen: syn = getnode(node) # print(node) syn_name = syn.lemma_names()[0] p = Node("Unseen", id=node, name=syn_name) GraphT.create(p) matcher = NodeMatcher(GraphT) for edge in edge_list: ed = ['', ''] flag = 0 for i, node in enumerate(edge): if node in seen: ed[i] = matcher.match("Seen", id=node).first() flag += 1 elif node in unseen: ed[i] = matcher.match("Unseen", id=node).first() flag += 1 if flag == 2: r = Relationship(ed[0], " ", ed[1]) GraphT.create(r) else: continue
def search(): # 完善一下的话,html的地方写个循环 if request.method == 'POST': information = request.form.get("information") graph = Graph('http://localhost:7474', username='******', password='******') matcher = NodeMatcher(graph) nodelist = list(matcher.match()) numlist1 = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh'] numlist2 = ['one', 'two', 'three', 'four'] nodedict = dict(zip(numlist1, nodelist)) relmatch = RelationshipMatcher(graph) relist = list(relmatch.match()) reldict = dict(zip(numlist2, relist)) # nodedict1 = json.dumps(nodedict, encoding='UTF-8', ensure_ascii=False) # reldict1 = json.dumps(reldict, encoding='UTF-8', ensure_ascii=False) return render_template('index.html', **nodedict, **reldict) return render_template('search.html')
def _createAuthor(pub, names): graph = connectGraph() print(' Creating Authors') for auth in names: matcher = NodeMatcher(graph) aNode = matcher.match("Author", name=auth['name']).first() try: org = auth['org'] except KeyError: org = '' if aNode is None: if org == '': aNode = Node('Author', name=auth['name']) else: aNode = Node('Author', name=auth['name'], org=org) path_1 = Path(aNode, 'AUTHORED', pub) graph.create(path_1) else: #Check the relationship rmatcher = RelationshipMatcher(graph) auNode = rmatcher.match((aNode, pub), "AUTHORED").first() if auNode is None: path_1 = Path(aNode, 'AUTHORED', pub) graph.create(path_1)
def createFangji_node(count,datalist1,datalist2): namelist=[] for j in datalist1: try: node=Node("方剂",方剂名称=j) graph.create(node) namelist.append(j) except: print("该节点已经存在") count=0 dict2={'方剂制法':'null','注意事项':'null','方剂别名':'null','各家论述':'null','方剂歌诀':'null','加减化裁':'null','方剂鉴别':'null','趣味记忆':'null','方剂方义':'null'} matcher=NodeMatcher(graph) for name in namelist: dict1=eval(datalist2[count]) n=matcher.match("方剂",方剂名称=name) for node in n: #print(dict(node)['方剂名称']) node['方剂别名']=dict2['方剂别名'] node['方剂出处']=dict1['方剂出处:'] node['方剂歌诀']=dict2['方剂歌诀'] node['方剂组成']=dict1['组成:'] node['方剂用法']=dict1['方剂用法:'] node['功效主治']=dict1['功效主治:'] node['加减化裁']=dict2['加减化裁'] node['药材配方']=dict1['药材配方:'] node['药用公用']=dict1['药用公用:'] node['方剂附注']=dict1['方剂附注:'] node['方剂制法']=dict2['方剂制法'] node['方剂鉴别']=dict2['方剂鉴别'] node['趣味记忆']=dict2['趣味记忆'] node['方剂方义']=dict2['方剂方义'] node['各家论述']=dict2['各家论述'] node['注意事项']=dict2['注意事项'] graph.push(node) print(count) count=count+1
def bacth_node_label(self, label, entity_labes): tx = self.graph.begin() newnodelist = [] oldnodelist = [] matcher = NodeMatcher(self.graph) for data in entity_labes: node = matcher.match(name=data).first() if node is None: oneNode = Node() oneNode.add_label(label=label) oneNode["name"] = data newnodelist.append(oneNode) else: node.add_label(label=label) oldnodelist.append(node) if len(newnodelist) > 0: newsub = Subgraph(newnodelist) print("newnodelist----", newnodelist) tx.create(newsub) if len(oldnodelist) > 0: oldsub = Subgraph(oldnodelist) print("oldnodelist----", oldnodelist) tx.push(oldsub) tx.commit()
def insert_tags(): # UserID::MovieID::Tag::Timestamp with open('/Users/Lim/Documents/DAnalyticsWorkspace/ml-10M100K/tags.dat', 'r', encoding='UTF-8') as f: text = f.readlines() f.close() matcher = NodeMatcher(db) tx = db.begin() for data in text: split_data = data.split("::") user_id = int(split_data[0]) movie_id = int(split_data[1]) tag = split_data[2] user = matcher.match("User", id=user_id).first() movie = matcher.match("Movie", id=movie_id).first() if user and movie: relationship = Relationship(user, "TAG", movie, tag=tag) tx.create(relationship) sys.stdout.write('{}\r' + str(user_id)) tx.commit()
def AddWalksWhenRelathionship(person_name='person_name', person_face=[]): # if person_face == [] and person_name != 'person_name': # person_face = GetFaceFromGraph(person_name) t = int(_time.time()) graph = GetGraph() tx = graph.begin() matcher = NodeMatcher(graph) person = matcher.match("Person", name=person_name).first() # last_walked = t if not person: tx.rollback() return 0 # person = Node("Person", name=person_name, face=person_face, last_walked=t) # tx.create(person) else: last_walked = person['last_walked'] t1 = last_walked if t - t1 > 60: time = Node("Time", time=t) person_time = Relationship(person, "WALKS_WHEN", time) tx.create(person_time) person['last_walked'] = t tx.push(person) tx.commit()
def establish_relation(graph, file_obj): rdf = file_obj.readline() matcher = NodeMatcher(graph) while rdf != "": if len(rdf.rstrip("\n").split('||')) == 3: if get_predicate(rdf) == "国籍": node_person = matcher.match("person", name=get_subject(rdf)).first() if node_person == None: print("error! " + get_subject(rdf) + " not exist!") else: if get_object(rdf) == "中华人民共和国": node_country = matcher.match("country", name="中国").first() else: node_country = matcher.match( "country", name=get_object(rdf)).first() if node_country != None: create_relationship = Relationship( node_person, "come_from", node_country) graph.create(create_relationship) else: print(get_object(rdf) + "不存在") rdf = file_obj.readline()
def changeEntity(request): if request.method == 'GET': return render(request, 'changeEntity.html', {'entitytypes': entitytypes}) else: origin_label = request.POST.get('origin_label') origin_name = request.POST.get('origin_name') change_label = request.POST.get('change_label') change_name = request.POST.get('change_name') for key in entity_map: if key == origin_label: origin_entity_label = entity_map[key] if key == change_label: change_entity_label = entity_map[key] matcher = NodeMatcher(g) try: node = matcher.match(origin_entity_label, name=origin_name).first() if node is None: messages.error(request, '节点不存在或输入有误') return render(request, 'changeEntity.html', {'entitytypes': entitytypes}) print('执行语句前') r = 'match (n:' + origin_entity_label + ' {name:"' + origin_name + '"}) remove n:' + origin_entity_label + ' set n:' + change_entity_label + ', n.name="' + change_name + '"' g.run(r) except Exception as e: print(e) messages.error(request, '修改失败') return render(request, 'changeEntity.html', {'entitytypes': entitytypes}) else: print('执行语句后') messages.success(request, '修改成功') return render(request, 'changeEntity.html', {'entitytypes': entitytypes})
def test_custom_attributes_rel(self): folder = os.path.dirname(os.path.abspath(__file__)) test_driller = Driller(os.path.join(folder, 'cnfg_simple.yml')) test_driller.drill_batch() node_matcher = NodeMatcher(test_driller.graph) rel_matcher = RelationshipMatcher(test_driller.graph) commit = node_matcher.match( "Commit", hash="aa6fa504ccb0fa919acc3cb31e510dc2048314eb0656f34babada15c" ).first() assert commit['is_merge'] == 0 update_file_rel = rel_matcher.match([commit], "UpdateFile").first() assert update_file_rel['complexity'] == 2 assert update_file_rel['nloc'] == 8 assert update_file_rel['old_path'] == 'gr_test/default_class.py' assert update_file_rel['path'] == 'gr_test/default_class.py' assert update_file_rel['token_count'] == 42 assert update_file_rel['type'] == 'MODIFY' assert update_file_rel['removed'] == 6 assert update_file_rel['added'] == 0 update_method_rel = rel_matcher.match([commit], 'UpdateMethod').first() # assert update_method_rel['type'] == 'DELETE' assert update_method_rel['nloc'] == 5 assert update_method_rel['complexity'] == 2 assert update_method_rel['token_count'] == 21 assert update_method_rel['length'] == 5 assert update_method_rel['fan_in'] == 0 assert update_method_rel['fan_out'] == 0 assert update_method_rel['start_line'] == 11 assert update_method_rel['end_line'] == 15 test_driller.clean()
def generateInvoice(order_id): seller = 'Team Team' connection = mysql.connector.connect( host='mysql98.unoeuro.com', database='zakeovich_dk_db_cphbusiness') cursor = connection.cursor() #Update order status cursor.execute( f"UPDATE zakeovich_dk_db_cphbusiness.order o SET o.status = 'completed' WHERE o.id = {order_id};" ) #Get order row cursor.execute( f"SELECT o.total, o.order_no, o.status FROM zakeovich_dk_db_cphbusiness.order o WHERE o.id = {order_id};" ) order_total = cursor.fetchall() order_total = order_total[0][0] #Generate invoice for given order and store in sql cursor.execute( f"INSERT INTO zakeovich_dk_db_cphbusiness.invoice (fk_order_id, total, due_date, issue_date, seller) VALUES ({order_id}, {order_total}, date_add(current_date(), INTERVAL 14 DAY), current_date(), '{seller}');" ) #Get the ordernumber of the order cursor.execute( f"SELECT o.order_no, o.status FROM zakeovich_dk_db_cphbusiness.order o WHERE o.id = {order_id};" ) order = cursor.fetchall() order_no = order[0][0] order_status = order[0][1] #Commit SQL update connection.commit() invoice_id = cursor.lastrowid cursor.execute( f"SELECT i.total, i.due_date, i.issue_date, i.seller FROM zakeovich_dk_db_cphbusiness.invoice i WHERE i.id = {invoice_id};" ) invoice = cursor.fetchall() cursor.close() connection.close() graph = Graph("bolt://35-202-37-187.gcp-neo4j-sandbox.com:7687", auth=("neo4j", "cy3yxxzcXDN6UKnw"), secure=True) #graph = Graph(scheme='bolt',host='hobby-ppgaodfmmciegbkemkpmdcel.dbs.graphenedb.com',port=24787, user='******', password='******',secure=True) #Create a nodematcher matcher = NodeMatcher(graph) #Open the connection g_conn = graph.begin() order_node = matcher.match("Order", order_no=order_no).first() #Update order in Neo4j graph.run( f"MATCH (o {{ order_no: {order_no}}}) SET o.status = '{order_status}'") #Serve invoice to Neo4j invoice_node = Node("Invoice", total=invoice[0][0], due_date=str(invoice[0][1]), seller=str(invoice[0][3])) g_conn.create(invoice_node) order_invoice = Relationship(order_node, "GENERATED", invoice_node, issue_date=str(invoice[0][2])) g_conn.create(order_invoice) #Commit the changes g_conn.commit()
def getMovieData(title): matcher = NodeMatcher(graph) movie = matcher.match("Movie", title={title}).first() return jsonify(movie)
def create_kg(): graph = Graph("http://localhost:7474", username="******", password='******') matcher = NodeMatcher(graph) graph.delete_all() zhongchengyaochengfen = ''.join( open('../data/dict/中成药成份.txt', 'r', encoding='utf-8').readlines()) xiyaochengfen = ''.join( open('../data/dict/西药成份.txt', 'r', encoding='utf-8').readlines()) zhongyizhengxing = ''.join( open('../data/dict/中医证型.txt', 'r', encoding='utf-8').readlines()) jibing = ''.join( open('../data/dict/疾病.txt', 'r', encoding='utf-8').readlines()) zhengzhuang = ''.join( open('../data/dict/症状.txt', 'r', encoding='utf-8').readlines()) department = open('../生成文件/寻医问药中的科室部位层级结构.txt', 'r', encoding='utf-8').readlines() xunyiwenyao_entity = pd.read_csv('../生成文件/寻医问药数据中抽取的实体.csv') for i in range(xunyiwenyao_entity.shape[0]): if xunyiwenyao_entity['类型'].loc[i] == '疾病': # 在抽取实体时,采用的是基于规则的方法,即在药品说明书中的某些字段内出现的实体被认为是疾病,在构建图谱时需要得到具体的实体类型 if xunyiwenyao_entity['实体'].loc[i] in zhengzhuang: disease_node = Node('症状', name=xunyiwenyao_entity['实体'].loc[i]) entity_num['症状'] += 1 elif xunyiwenyao_entity['实体'].loc[i] in jibing: disease_node = Node('疾病', name=xunyiwenyao_entity['实体'].loc[i]) entity_num['疾病'] += 1 elif xunyiwenyao_entity['实体'].loc[i] in zhongyizhengxing: disease_node = Node('中医证型', name=xunyiwenyao_entity['实体'].loc[i]) entity_num['中医证型'] += 1 else: disease_node = Node('疾病', name=xunyiwenyao_entity['实体'].loc[i]) entity_num['疾病'] += 1 graph.create(disease_node) for j in department: if xunyiwenyao_entity['实体'].loc[i] in j: List1 = j.strip().split('-->') List = [ ii.strip() for ii in List1 if ii != List1[-1] and ii != '' ] for list_i in range(1, len(List)): if List[list_i - 1] in buwei: front_node = matcher.match('部位').where( name=List[list_i - 1]).first() if front_node == None: front_node = Node('部位', name=List[list_i - 1]) entity_num['部位'] += 1 graph.create(front_node) else: front_node = matcher.match('科室').where( name=List[list_i - 1]).first() if front_node == None: front_node = Node('科室', name=List[list_i - 1]) entity_num['科室'] += 1 graph.create(front_node) if List[list_i] in buwei: this_node = matcher.match('部位').where( name=List[list_i]).first() if this_node == None: this_node = Node('部位', name=List[list_i]) entity_num['部位'] += 1 graph.create(this_node) else: this_node = matcher.match('科室').where( name=List[list_i]).first() if this_node == None: this_node = Node('科室', name=List[list_i]) entity_num['科室'] += 1 graph.create(this_node) graph.create(Relationship(front_node, '包含', this_node)) relation_num['包含'] += 1 if List[-1] in buwei: last_node = matcher.match('部位').where( name=List[-1]).first() if last_node == None: last_node = Node('部位', name=List[-1]) entity_num['部位'] += 1 else: last_node = matcher.match('科室').where( name=List[-1]).first() if last_node == None: last_node = Node('科室', name=List[-1]) entity_num['科室'] += 1 graph.create(Relationship(last_node, '包含', disease_node)) relation_num['包含'] += 1 elif xunyiwenyao_entity['类型'].loc[i] == '药品网页链接': graph.create(Node('药品网页链接', name=xunyiwenyao_entity['实体'].loc[i])) entity_num['药品网页链接'] += 1 elif xunyiwenyao_entity['类型'].loc[i] == '成份': if xunyiwenyao_entity['实体'].loc[i] in zhongchengyaochengfen: component_part_node = Node( '中成药成份', name=xunyiwenyao_entity['实体'].loc[i]) entity_num['中成药成份'] += 1 elif xunyiwenyao_entity['实体'].loc[i] in xiyaochengfen: component_part_node = Node( '西药成份', name=xunyiwenyao_entity['实体'].loc[i]) entity_num['西药成份'] += 1 graph.create(component_part_node) print('实体新建完成') xunyiwenyao_relationship = pd.read_csv('../生成文件/寻医问药中抽取关系.csv') for i in range(xunyiwenyao_relationship.shape[0]): if xunyiwenyao_relationship['关系类型'].loc[i] == '治疗' or \ xunyiwenyao_relationship['关系类型'].loc[i] == '成份是': start_node = matcher.match( xunyiwenyao_relationship['开始节点类型'].loc[i]).where( name=xunyiwenyao_relationship['开始'].loc[i]).first() if start_node == None: start_node = Node(xunyiwenyao_relationship['开始节点类型'].loc[i], name=xunyiwenyao_relationship['开始'].loc[i]) graph.create(start_node) end_node = matcher.match( xunyiwenyao_relationship['结束节点类型'].loc[i]).where( name=xunyiwenyao_relationship['结束'].loc[i]).first() if end_node == None: end_node = Node(xunyiwenyao_relationship['结束节点类型'].loc[i], name=xunyiwenyao_relationship['结束'].loc[i]) graph.create(end_node) graph.create( Relationship(start_node, xunyiwenyao_relationship['关系类型'].loc[i], end_node)) relation_num[xunyiwenyao_relationship['关系类型'].loc[i]] += 1 print('治疗与成份关系添加完成') xunyiwenyao_tabbo = pd.read_csv('../生成文件/寻医问药数据中的禁忌关系.csv') for i in range(xunyiwenyao_tabbo.shape[0]): start_node = matcher.match('药品网页链接').where( name=xunyiwenyao_tabbo['开始'].loc[i]).first() end_node = matcher.match(xunyiwenyao_tabbo['节点类型'].loc[i]).where( name=xunyiwenyao_tabbo['结束'].loc[i]).first() if end_node == None: end_node = Node(xunyiwenyao_tabbo['节点类型'].loc[i], name=xunyiwenyao_tabbo['结束'].loc[i]) entity_num[xunyiwenyao_tabbo['节点类型'].loc[i]] += 1 graph.create(end_node) graph.create(Relationship(start_node, '禁忌', end_node)) relation_num['禁忌'] += 1 print('禁忌关系添加完成') symptom_lead_disease = open( '../data/symptom_lead_disease/求医网中症状导致疾病层级结构.txt', 'r', encoding='utf-8') for i in symptom_lead_disease.readlines(): disease = i.split('-导致-')[1] symptom = i.split('-导致-')[0] disease_node = matcher.match('疾病').where(name=disease).first() symptom_node = matcher.match('症状').where(name=symptom).first() if disease_node == None: disease_node = Node('疾病', name=disease) graph.create(disease_node) entity_num['疾病'] += 1 if symptom_node == None: symptom_node = Node('症状', name=disease) graph.create(symptom_node) entity_num['症状'] += 1 graph.create(Relationship(symptom_node, '导致', disease_node)) relation_num['导致'] += 1 print('添加症状-疾病之间的层级结构关系') zong = 0 for i in relation_num.values(): zong += i print('在寻医问药知识图谱中共有关系{}个,具体数量如下:\n关系类别|数量\n:-:|:-:'.format(zong)) for k, v in relation_num.items(): print(k, '|', v) print('\n') zong = 0 for i in entity_num.values(): zong += i print('在寻医问药知识图谱中共有实体{}个具体数目如下:\n实体类型|数量\n:-:|:-:'.format(zong)) for k, v in entity_num.items(): print(k, '|', v) print('\n')
from py2neo import Graph, NodeMatcher grafo = Graph("bolt://localhost:7687", auth=("neo4j", "1234"), encrypted=False) matcher = NodeMatcher(grafo) def menu(): print(" ----------------------------------------------") print(" ----------------------------------------------") print(" RECOMENDACION DE LUGARES TURÍSTICOS \n") print(" -- ACTIVIDADES: \n") print(" - Aventuras ") print(" - Deportes Extremos ") print(" - Recreacion \n") actividad = input( " - De las opciones anteriores, escoja la que más se adecue a su gusto (escriba la opcion tal y como aparece, sin '-'): " ) print(" -- CLIMAS: \n") print(" - Frio") print(" - Calido ") print(" - Templado \n") clima = input( " - De las opciones anteriores, escoja la que más se adecue a su gusto (escriba la opcion tal y como aparece, sin '-'): " ) print(" -- NÚMERO DE PERSONAS: \n") print(" - Grupos") print(" - Parejas ")
def test_database(filepath, clus): graph = Graph("http://localhost:7474", username="******", password='******') if clus == 1: graph.delete_all() f = open(filepath, 'r') f_csv = csv.reader(f) # labels = next(f_csv) labels = ['none', 'site1', 'name', 'score', 'genres', 'site2', 'year'] print(labels) for row in f_csv: node_poster = Node("Poster", cluster=clus, name=row[labels.index('name')]) genres = row[labels.index('genres')].split('|') # print(genres) # node_poster.update(name=row[labels.index('movie_title')]) attr = {} for data in row: if not labels[row.index(data)] == 'name': attr[labels[row.index(data)]] = data node_poster.update(attr) matcher = NodeMatcher(graph) if node_poster not in matcher.match("Poster %d" % clus): graph.create(node_poster) print(row) # tar = matcher.match("Country", name=row[labels.index('country')]).first() # if tar: # poster_locate_in = Relationship(node_poster, "country", tar) # graph.create(poster_locate_in) # else: # node_country = Node("Country", name=row[labels.index('country')]) # graph.create(node_country) # poster_locates_in = Relationship(node_poster, "country", node_country) # graph.create(poster_locates_in) # tar = matcher.match("Year", name=row[labels.index('year')]).first() # if tar: # poster_locate_in = Relationship(node_poster, "year", tar) # graph.create(poster_locate_in) # else: # node_country = Node("Year", name=row[labels.index('year')]) # graph.create(node_country) # poster_locates_in = Relationship(node_poster, "year", node_country) # graph.create(poster_locates_in) for genre in genres: tar = matcher.match("Genre", name=genre).first() if tar: poster_genre_is = Relationship(node_poster, "genre", tar) graph.create(poster_genre_is) else: node_genre = Node("Genre", name=genre) graph.create(node_genre) poster_genre_is = Relationship(node_poster, "genre", node_genre) graph.create(poster_genre_is) for tar in matcher.match("Poster", cluster=clus): if tar == node_poster: continue sim_poster = Relationship(node_poster, "similar", tar) graph.create(sim_poster) return labels
def searchDB(content, deep): from py2neo import NodeMatcher graph = Graph('http://localhost:7474', username='******', password='******') matcher = NodeMatcher(graph) # a=matcher.match("人物", name="李佳洁").first() # print(a) contentList = list() contentList_temp = list() contentList.append(content) contentList_temp.append(content) relationList = list() entityList = list() categoriesList = list() countContent = 1 deepCount = 0 countList = list() countList.append(0) resultList = graph.run("match (n {name:\"" + content + "\"})-[r]-(m) return n,m,r").data() if len(resultList) == 0: resultMap = { "state": 0, } print(resultMap) resultMap = json.dumps(resultMap, ensure_ascii=False) return resultMap else: while deepCount < deep: countList.append(len(contentList_temp)) for i in range(countList[deepCount], countList[deepCount + 1]): resultList = graph.run("match (n {name:\"" + contentList_temp[i] + "\"})-[r]-(m) return n,m,r").data() entity = resultList[0].get('n').get('name') if resultList[0].get('n').get('newLabel'): entity_type = resultList[0].get('n').get('newLabel') else: entity_type = str( resultList[0].get('n')).split(':')[1].split(' {')[0] categoriesList.append(entity_type) entityList.append({ "entity": entity, "entity_type": entity_type }) for res in resultList: entity1 = res.get('m').get('name') if entity1 in contentList: continue contentList.append(entity1) if res.get('m').get('newLabel'): entity1_type = res.get('m').get('newLabel') else: entity1_type = str( res.get('m')).split(':')[1].split(' {')[0] categoriesList.append(entity1_type) relation = str(res.get('r')).split(':')[1].split(' {')[0] resEntity1 = str(res.get('r')).split(')')[0].split('(')[1] resEntity2 = str(res.get('r')).split('(')[2].split(')')[0] entityList.append({ "entity": entity1, "entity_type": entity1_type }) relationList.append({ "entity1": resEntity1, "entity2": resEntity2, "relation": relation }) contentList_temp = list(set(contentList)) contentList_temp.sort(key=contentList.index) deepCount += 1 print(deepCount) entityList = distinct(entityList, "entity") categoriesList = list(set(categoriesList)) resultMap = { "state": 1, "entityList": entityList, "relationList": relationList, "categories": categoriesList } print(resultMap) resultMap = json.dumps(resultMap, ensure_ascii=False) return resultMap
from py2neo import Graph, Node, Relationship, NodeMatcher import os import sqlite3 uri = 'bolt://localhost:7687' user = '******' password = '******' dirname = os.path.dirname(__file__) db_file = 'recipe.sqlite' graph_db = Graph(uri, auth=(user, password)) matcher = NodeMatcher(graph_db) conn = sqlite3.connect(os.path.join(dirname, db_file)) c = conn.cursor() with conn: c.execute('SELECT * FROM "https://schema.org/Recipe"') rows = c.fetchall() for row in rows: tx = graph_db.begin() recipe = Node("Recipe", name=row[2], index=row[0]) author = matcher.match("Author", name=row[1]).first() if author == None: author = Node("Author", name=row[1]) tx.create(author) tx.create(recipe) relation = Relationship(recipe, "hasAuthor", author) tx.create(relation) tx.commit() print('added recipes and authors')
import json import requests from py2neo import Graph, NodeMatcher from rasa_core_sdk import Action from rasa_core_sdk.events import SlotSet graph = Graph("http://*****:*****@127.0.0.1:7474") selector = NodeMatcher(graph) # MATCH path = (n)-[r]->(m) where n.案件号 =~ '.*浙1125刑初148号.*' RETURN path def retrieveDataFromNeo4j(cyber): url = 'http://*****:*****@127.0.0.1:7474/db/data/transaction/commit' body = { "statements": [{ "statement": cyber, "resultDataContents": ["graph"] }] } headers = {'content-type': "application/json; charset=utf-8"} response = requests.post(url, data=json.dumps(body), headers=headers) return response.text # 查看案例被告 class ViewCaseDefendants(Action): def name(self): return 'action_view_case_defendants' def run(self, dispatcher, tracker, domain):
def main(to_handle): # Don't forget to start the MDG up before using this script! MDG = Graph() deps = [] matcher = NodeMatcher(MDG) errors.write("In " + to_handle + ":\n") exceptions.write("In " + to_handle + ":\n") # print("Starting") with open(data_dir + to_handle, 'r', newline='') as f: reader = csv.reader(f) prev_gid, prev_art, prev_node, prev_version = None, None, None, None for row in reader: if len(row) < 7: continue tx = MDG.begin() # Get metadata repo, gid, aid, version, packaging, sha = (row[0], row[3], row[4], row[5], row[6], get_hash(row[2])) # Missing: release date, packaging # Create & add node repo_node = Node("Artifact", stars=row[1], url=row[2], groupID=gid, artifact=aid, version=version, packaging=packaging, coordinates=gid + ":" + aid + ":" + version, commit_hash=sha, from_github="True") repo_deps = [] for d in row[7:]: if len(d) > 2: dep_list = convert_dep_to_list(d) if dep_list is not None: repo_deps.append(dep_list) # This is to see if the node was in the MDG before we added try: e_node = existing_node(matcher, repo_node) except Exception as err: errors.write("Error while checking if the node " + gid + ":" + aid + ":" + version + ":" + sha + " exists in " + to_handle + ": " + repr(err) + "\n") continue if e_node is not None: print(e_node["coordinates"]) if e_node is not None: repo_node = e_node else: repo_node["coordinates"] += ":" + sha if version != prev_version or (aid != prev_art and gid != prev_gid): try: tx.create(repo_node) except Exception as err: errors.write("Error while creating node " + repo_node["coordinates"] + " in " + to_handle + ": " + repr(err) + "\n") if aid == prev_art and gid == prev_gid: r_next = Relationship(repo_node, "NEXT", prev_node) try: tx.merge(r_next, "Artifact", "coordinates") except Exception as err: errors.write("Error while merging NEXT between " + repo_node["coordinates"] + " and " + prev_node["coordinates"] + " in " + to_handle + ": " + repr(err) + "\n") prev_gid, prev_art, prev_node, prev_version = ( repo_node["groupID"], repo_node["artifact"], repo_node, repo_node["version"]) deps.append((repo_node, repo_deps)) tx.commit() # print("Done adding nodes and NEXT") for (node, dep_list) in deps: tx = MDG.begin() node_deps = purge_deps(dep_list) for dep in node_deps: dep_node, reason = find_dep_node(MDG, matcher, dep) if dep_node is None: exceptions.write(node["coordinates"] + ": could not" + " create dependency with " + dep[0] + ":" + dep[1] + ":" + dep[2][0] + "because " + reason + "\n") continue r_dep = Relationship(node, "DEPENDS_ON", dep_node) try: tx.merge(r_dep, "Artifact", "coordinates") except Exception as err: errors.write("Could not create dependency between " + r_dep.start_node["coordinates"] + " and " + r_dep.end_node["coordinates"] + "because " + repr(err) + "\n") tx.commit()
def returnDoF(): person = request.args.get('person') # Connect to database graph = Graph("http://*****:*****@localhost:7474") nodematcher = NodeMatcher(graph) relamatcher = RelationshipMatcher(graph) nodes = [] edges = [] # Find the node for the person selected personNode = nodematcher.match("COSTAR", name=person).first() # Convert node to GraphJSON nodes.append({ "id": personNode.identity, "name": personNode["name"], "rta": personNode["rta"], "tp": personNode["tp"], "ao": personNode["ao"] }) # Find all relationships for the selected person ships1 = relamatcher.match((personNode, None)) ships2 = relamatcher.match((None, personNode)) # Convert the relationships and nodes to GraphJSON for ship in ships1: nodes.append({ "id": ship.end_node.identity, "name": ship.end_node["name"], "rta": ship.end_node["rta"], "tp": ship.end_node["tp"], "ao": ship.end_node["ao"] }) edges.append({ "source": ship.start_node.identity, "target": ship.end_node.identity, "role": "appeared_with" }) for ship in ships2: nodes.append({ "id": ship.start_node.identity, "name": ship.start_node["name"], "rta": ship.start_node["rta"], "tp": ship.start_node["tp"], "ao": ship.start_node["ao"] }) edges.append({ "source": ship.start_node.identity, "target": ship.end_node.identity, "role": "appeared_with" }) jsonNodes = dumps(nodes) jsonEdges = dumps(edges) # Create a final json file with all GraphJSON finalJson = '{"nodes": ' + jsonNodes + ', "edges": ' + jsonEdges + '}' # Save file to server cache fileLocation = (webserverstatic + person + '.json').encode("utf-8") with open(fileLocation, "w+") as test: test.write(finalJson) # Send file to template to display generated graph return render_template('showdata.html', message=person)
def __init__(self,houseName): self.node_matcher = NodeMatcher(graph) self.rel_matcher = RelationshipMatcher(graph) self.name = houseName
def test_connection(): print(graph) matcher = NodeMatcher(graph) print(matcher.match("House",name="Baratheon").first())
import yaml # logging settings with open('logging.yml', 'rt') as f: config = yaml.safe_load(f.read()) f.close() logging.config.dictConfig(config) logger = logging.getLogger(__name__) # check databases try: _graph_db = Graph('http://localhost:7474', user="******", password="******") _matcher = NodeMatcher(_graph_db) _reader = geoip2.database.Reader('./db/geoip/GeoLite2-City.mmdb', mode=geoip2.database.MODE_MMAP) except: logger.info("neo4j of geoip not available") sys.exit(1) def packet_analysis_live(cap): cap.set_debug() logger.debug(cap.get_parameters()) cap.sniff(timeout=50) signal.signal(signal.SIGINT, signal_handler) for pkt in cap.sniff_continuously(): do_the_job(pkt) stop()