Exemplo n.º 1
0
def query_special_event():
    # step 1: query all special event
    c_string = "MATCH (n:SpecialEvent) RETURN n"
    result = graph.run(c_string)
    special_event_list = []
    for item in result:
        special_event_list.append(dict(item[0]))
    tmp_list = []
    for item in special_event_list:
        tmp_list.extend(item.values())

    results = dict()
    for v in tmp_list:
        c_string = "START end_node=node:%s(event='%s') MATCH (m)-[r:%s]->(end_node) RETURN count(m)" % (
            special_event_index_name, v, event_special)
        node_result = graph.run(c_string)
        event_list = []
        for item in node_result:
            tmp = dict(item)
            node_number = tmp.values()[0]
        results[v] = node_number

    return_results = sorted(results.iteritems(),
                            key=lambda x: x[1],
                            reverse=True)
    return return_results
Exemplo n.º 2
0
def get_graph_single(uid_list, node_type, relation_type, layer):
    # print uid_list
    relation_list = []
    for i in uid_list:
        if layer == '0':
            c_string = 'START s0=node:node_index(uid="' + str(i) + '") '
            c_string += 'MATCH (s0)-[r]-(s1:User) WHERE (s1.uid in '+ json.dumps(uid_list)\
            + 'and type(r) in '+ json.dumps(relation_type)  +') return r LIMIT 1000'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                relation_list.append(r1)
        if layer == '1':
            c_string = 'START s0=node:node_index(uid="' + str(i) + '") '
            c_string += 'MATCH (s0:User)-[r]-(s1' + node_type + ') WHERE type(r) in ' + json.dumps(
                relation_type) + ' return r LIMIT 5'
            # print c_string,'!!!!!!!!!!!!!!'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                relation_list.append(r1)
        if layer == '2':
            c_string = 'START s0=node:node_index(uid="' + str(i) + '") '
            c_string += 'MATCH (s0)-[r]-(s1)-[r2]-(s2) WHERE (type(r) in '+ json.dumps(relation_type)\
                     + 'and type(r2) in '+ json.dumps(relation_type)  +') return r,r2 LIMIT 10'
            result = graph.run(c_string)
            for r in result:
                # print r['r']
                r1 = dict(r)['r']
                relation_list.append(r1)
                r2 = dict(r)['r2']
                relation_list.append(r2)
    # result_list = list(result)
    print len(relation_list), '^^^^^^^^^^^'
    return relation_list
Exemplo n.º 3
0
def get_graph(uid_list, layer):
    print uid_list
    relation_list = []
    for i in uid_list:
        if layer == '0':
            c_string = 'START s0=node:node_index(uid="' + str(i) + '") '
            c_string += 'MATCH (s0)-[r]-(s1:User) WHERE s1.uid in ' + json.dumps(
                uid_list) + ' return r LIMIT 1000'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                relation_list.append(r1)
        if layer == '1':
            c_string = 'START s0=node:node_index(uid="' + str(i) + '") '
            c_string += 'MATCH (s0:User)-[r]-(s1)  return r LIMIT 1'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                relation_list.append(r1)
        if layer == '2':
            c_string = 'START s0=node:node_index(uid="' + str(i) + '") '
            c_string += 'MATCH (s0:User)-[r]-(s1)-[r2]-(s2) return r,r2 LIMIT 10'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                # print r1,'!!!!!!!!!!!!!!!!********'
                relation_list.append(r1)
                r2 = dict(r)['r2']
                relation_list.append(r2)
    # result_list = list(result)
    # print relation_list
    return relation_list
Exemplo n.º 4
0
def del_e_theme_rel(theme_name, event_id):
    en_name = p.get_pinyin(theme_name)
    s_string = 'START s0 = node:special_event_index(event="%s"),s3 = node:event_index(event_id="%s")\
                MATCH (s0)-[r:special_event]-(s3) DELETE r' % (en_name,
                                                               event_id)
    print s_string
    graph.run(s_string)

    event_list_string = es_event.get(index=special_event_name, doc_type=special_event_type, id=en_name,\
                            fields=['event'])
    eid_list = event_list_string['fields']['event'][0].split('&')
    new_eid_list = set(eid_list) - set([event_id])
    new_eid_list = [i for i in new_eid_list]
    eid_string = '&'.join(new_eid_list)
    if len(new_eid_list) == 0:
        s_string = 'START s0 = node:special_event_index(event="%s") DELETE s0' % (
            en_name)
        graph.run(s_string)
        es_event.delete(index=special_event_name,
                        doc_type=special_event_type,
                        id=en_name)
    else:
        es_event.update(index=special_event_name,doc_type=special_event_type,id=en_name,\
            body={'doc':{'event':eid_string, 'event_count':len(new_eid_list)}})
    return 'true'
Exemplo n.º 5
0
def del_u_group_rel(g_name, uid):
    en_name = p.get_pinyin(g_name)
    en_name = en_name.lower()
    s_string = 'START s0 = node:'+group_index_name+'('+group_primary+'="'+en_name+'"),'\
               +'s3 = node:'+node_index_name+'('+people_primary+'="'+uid+'") MATCH (s0)-[r:'+group_rel+']-(s3) DELETE r'

    print s_string
    graph.run(s_string)

    user_list_string = es_group.get(index=group_name,
                                    doc_type=group_type,
                                    id=en_name,
                                    fields=['people'])
    uid_list = user_list_string['fields']['people'][0].split('&')
    new_uid_list = set(uid_list) - set([uid])
    new_uid_list = [i for i in new_uid_list]
    uid_string = '&'.join(new_uid_list)
    if len(new_uid_list) == 0:
        s_string = 'START s0 = node:' + group_index_name + '(' + group_primary + '="' + en_name + '") DELETE s0'
        graph.run(s_string)
        es_group.delete(index=group_name, doc_type=group_type, id=en_name)
    else:
        es_group.update(index=group_name,doc_type=group_type,id=en_name,\
            body={'doc':{'people':uid_string, 'people_count':len(new_uid_list)}})
    return '1'
Exemplo n.º 6
0
def add_user_group_rel(group_name, uid):
    s_string = 'START s0 = node:group_index(group="%s"),s = node:node_index(uid="%s")\
                MATCH (s0)-[r]-(s) RETURN r' % (group_name, uid)

    # print s_string
    graph.run(s_string)
    return 'true'
Exemplo n.º 7
0
def query_group():  #群体概览
    # step 1: query all group
    c_string = "MATCH (n:Group) RETURN n"
    result = graph.run(c_string)
    group_list = []
    for item in result:
        group_list.append(dict(item[0]))
    tmp_list = []
    for item in group_list:
        tmp_list.extend(item.values())

    results = dict()
    for v in tmp_list:
        c_string = "START end_node=node:%s(group='%s') MATCH (m)-[r:%s]->(end_node) RETURN count(m)" % (
            group_index_name, v, group_rel)
        node_result = graph.run(c_string)
        event_list = []
        for item in node_result:
            tmp = dict(item)
            node_number = tmp.values()[0]
        results[v] = node_number

    return_results = sorted(results.iteritems(),
                            key=lambda x: x[1],
                            reverse=True)
    return return_results
Exemplo n.º 8
0
def search_related_u_auto(g_name, submit_user):
    group_id = p.get_pinyin(g_name)
    group_id = group_id.lower()
    uid_string = es_group.get(index=group_name,
                              doc_type=group_type,
                              id=group_id,
                              fields=['people'])
    uid_list = uid_string['fields']['people'][0].split('&')
    related_list = []
    for en_name in uid_list:
        s_string = 'START s0 = node:node_index(uid="%s") \
                MATCH (s0)-[r]-(s3:User) return s3' % (en_name)
        print s_string
        result = graph.run(s_string)
        for item in result:
            item_dict = dict(item)
            related_list.append(item_dict['s3']['uid'])
    for en_name in uid_list:
        s_string = 'START s0 = node:node_index(uid="%s") \
                MATCH (s0)-[r]-(s3:Org) return s3' % (en_name)
        print s_string
        result = graph.run(s_string)
        for item in result:
            item_dict = dict(item)
            related_list.append(
                item_dict['s3']['org_id'])  # print uid_list, '---------'
    related_list = set(related_list) - set(uid_list)
    related_list = [i for i in related_list]
    print related_list, '---------'
    result = user_detail_search(related_list, submit_user)
    return result
Exemplo n.º 9
0
def get_graph(eid_list, layer):
    # print eid_list
    relation_list = []
    for i in eid_list:
        if layer == '0':
            c_string = 'START s0=node:event_index(event="' + str(i) + '") '
            c_string += 'MATCH (s0)-[r]-(s1:Event) WHERE s1.event_id in ' + json.dumps(
                eid_list) + ' return r LIMIT 1000'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                relation_list.append(r1)
        if layer == '1':
            c_string = 'START s0=node:event_index(event="' + str(i) + '") '
            c_string += 'MATCH (s0)-[r]-(s1)  return r LIMIT 1'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                relation_list.append(r1)
        if layer == '2':
            c_string = 'START s0=node:event_index(event="' + str(i) + '") '
            c_string += 'MATCH (s0)-[r]-(s1)-[r2]-(s2) return r,r2 LIMIT 10'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                relation_list.append(r1)
                r2 = dict(r)['r2']
                relation_list.append(r2)
    # result_list = list(result)
    # print relation_list
    return relation_list
Exemplo n.º 10
0
def get_graph_single(eid_list, node_type, relation_type, layer):
    print eid_list
    relation_list = []
    for i in eid_list:
        if layer == '0':
            c_string = 'START s0=node:event_index(event="' + str(i) + '") '
            c_string += 'MATCH (s0)-[r]-(s1:Event) WHERE (s1.event_id in '+ json.dumps(eid_list)\
                     + 'and type(r) in '+ json.dumps(relation_type)  +') return r LIMIT 500'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                relation_list.append(r1)
        if layer == '1':
            c_string = 'START s0=node:event_index(event="' + str(i) + '") '
            c_string += 'MATCH (s0)-[r]-(s1' + node_type + ') WHERE type(r) in ' + json.dumps(
                relation_type) + ' return r LIMIT 10'
            # c_string += 'MATCH (s0)-[r]-(s1)  return r LIMIT 1'
            result = graph.run(c_string)
            for r in result:
                r1 = dict(r)['r']
                relation_list.append(r1)
        if layer == '2':
            c_string = 'START s0=node:event_index(event="' + str(i) + '") '
            c_string += 'MATCH (s0)-[r]-(s1)-[r2]-(s2) WHERE (type(r) in '+ json.dumps(relation_type)\
                     + 'and type(r2) in '+ json.dumps(relation_type)  +') return r,r2 LIMIT 10'
            result = graph.run(c_string)
            for r in result:
                print r['r']
                r1 = dict(r)['r']
                relation_list.append(r1)
                r2 = dict(r)['r2']
                relation_list.append(r2)

    return relation_list
Exemplo n.º 11
0
def delete_rel(node_key1, node1_id, node1_index_name, rel, node_key2, node2_id,
               node2_index_name):
    list = []
    Index = ManualIndexManager(graph)
    node1_index = Index.get_index(Node, node1_index_name)
    node2_index = Index.get_index(Node, node2_index_name)
    if not (node1_index or node2_index):
        print "node_index does not exist"
        return None
    node1 = node1_index.get(node_key1, node1_id)[0]
    node2 = node2_index.get(node_key2, node2_id)[0]
    if not (node1 or node2):
        print("node does not exist")
    c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') MATCH (start_node)-[r:%s]-(end_node) RETURN r" % (
        node1_index_name, node_key1, node1_id, node2_index_name, node_key2,
        node2_id, rel)
    result = graph.run(c_string)
    for item in result:
        list.append(item)
    if not list:
        print "Deleted rel does not exist"
        return None
    c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') MATCH (start_node)-[r:%s]-(end_node) DELETE r" % (
        node1_index_name, node_key1, node1_id, node2_index_name, node_key2,
        node2_id, rel)
    print c_string
    graph.run(c_string)
    print "delete success"
    return True
Exemplo n.º 12
0
def compare_group_event(g_name1, g_name2, submit_user, flag):
    group_id1 = p.get_pinyin(g_name1)
    group_id1 = group_id1.lower()
    uid_string1 = es_group.get(index=group_name, doc_type=group_type, id=group_id1,  fields=['people'])
    uid_list1 = uid_string1['fields']['people'][0].split('&')

    group_id2 = p.get_pinyin(g_name2)
    group_id2 = group_id2.lower()
    uid_string2 = es_group.get(index=group_name, doc_type=group_type, id=group_id2,  fields=['people'])
    uid_list2 = uid_string2['fields']['people'][0].split('&')
    
    uid_list_all = [uid_list1, uid_list2]
    event_list = []
    for user_result in uid_list_all:
        event_list1 = []

        user_list, org_list = search_user_type(user_result)
        for uid in user_list:
            c_string = 'start n=node:'+node_index_name+'("'+people_primary+':'+str(uid)+'") match (n)-[r]-(e:Event) return e'
            result = graph.run(c_string)
            for event in result:
                # print event,'---------'
                # if event:
                event_dict = dict(event)
                event_id = event_dict['e']['event_id']
                event_list1.append(event_id)
        for uid in org_list:
            c_string = 'start n=node:'+org_index_name+'("'+org_primary+':'+str(uid)+'") match (n)-[r]-(e:Event) return e'
            result = graph.run(c_string)
            for event in result:
                # print event,'---------'
                # if event:
                event_dict = dict(event)
                event_id = event_dict['e']['event_id']
                event_list1.append(event_id)
        event_list.append(event_list1)
    if flag == 'all':
        event_list1 = [i for i in set(event_list[0])]
        event_list2 = [i for i in set(event_list[1])]
        detail_result1 = event_detail_search(event_list1,submit_user)
        detail_result2 = event_detail_search(event_list2,submit_user)

    if flag == 'same':
        same_u = set(event_list[0])&set(event_list[1])
        same_u = [i for i in same_u]
        detail_result1 = event_detail_search(same_u,submit_user)
        detail_result2 = event_detail_search(same_u,submit_user)

    if flag == 'diff':
        diff_u1 = set(event_list[0]) - (set(event_list[0])&set(event_list[1]))
        diff_u1 = [i for i in diff_u1]
        diff_u2 = set(event_list[1]) - (set(event_list[0])&set(event_list[1]))
        diff_u2 = [i for i in diff_u2]
        detail_result1 = event_detail_search(diff_u1,submit_user)
        detail_result2 = event_detail_search(diff_u2,submit_user)
    return {'detail_result1':detail_result1,'detail_result2':detail_result2}
Exemplo n.º 13
0
def get_theme_net(theme_name, submit_user):
    topic_id = p.get_pinyin(theme_name)
    topic_id = topic_id.lower()
    eid_string = es_event.get(index=special_event_name,
                              doc_type=special_event_type,
                              id=topic_id,
                              fields=['event'])
    event_list = eid_string['fields']['event'][0].split('&')
    event_result = es_event.mget(index=event_analysis_name, doc_type=event_text_type, \
                body={'ids':event_list}, fields=['en_name', 'name'])['docs']
    event_name_dict = {}
    for i in event_result:
        event_en_name = i['fields']['en_name'][0]
        event_name = i['fields']['name'][0]
        event_name_dict[event_en_name] = event_name
    event_graph_id = []
    for i in event_list:
        a = graph.run('start n=node:' + event_index_name + '("' +
                      event_primary + ':' + str(i) + '") return id(n)')
        for j in a:
            event_graph_id.append(str(dict(j)['id(n)']))
    # print event_graph_id
    event_id_string = ','.join(event_graph_id)
    query = 'start d=node(' + event_id_string + '),e=node(' + event_id_string + ') match (d)-[r]->(e) return d,type(r),e'
    result = graph.run(query)
    exist_relation = []
    exist_relation_string = []
    for i in result:
        # print i
        dict_i = dict(i)
        start_id = dict_i['d']['event_id']
        end_id = dict_i['e']['event_id']
        exist_relation.append([event_name_dict[start_id], relation_dict[dict_i['type(r)']], \
                    event_name_dict[end_id]])
        # print exist_relation
        relation_string = start_id + '-' + end_id
        exist_relation_string.append(relation_string)
    set_exist_relation = set(exist_relation_string)
    relation_set_count = len(list(set_exist_relation))
    node_count = len(event_list)
    total_count = node_count * (node_count - 1) / 2
    try:
        relation_degree = float(relation_set_count) / total_count
    except:
        relation_degree = 0
    if relation_degree == 0:
        conclusion = u'无关联'
    elif relation_degree < 0.33 and relation_degree > 0:
        conclusion = u'关联度较低'
    elif relation_degree >= 0.33 and relation_degree < 0.66:
        conclusion = u'关联度适中'
    elif relation_degree >= 0.66:
        conclusion = u'联系紧密'  ##未定义!!
    return {'relation_table':exist_relation, 'relation_count':relation_set_count,\
        'conclusion':conclusion, 'relation_degree':relation_degree}
Exemplo n.º 14
0
def delete_group(g_name, submit_user):
    en_name = p.get_pinyin(g_name)
    en_name = en_name.lower()
    try:
        es_group.delete(index=group_name, doc_type=group_type, id=en_name)
        s_string = 'start s0=node:'+group_index_name+'('+group_primary+'="'+en_name+'") '\
            + 'MATCH (s0) delete r,s0'
        graph.run(s_string)
    except:
        return '0'
    return '1'
Exemplo n.º 15
0
def compare_user_theme(theme_name1, theme_name2, sort_flag, diff):
    s_string1 = 'START s0 = node:special_event_index(event="%s")\
                MATCH (s0)-[r]-(s) RETURN s.event_id as event' % theme_name1
    event_result1 = graph.run(s_string1)
    uid_list1 = []
    for event in event_result1:
        # print
        event_value = event['event']
        # event_list.append(event_value)
        c_string = 'START s0 = node:event_index(event="' + str(
            event_value) + '") '
        c_string += 'MATCH (s0)-[r]-(s1:User) return s1 LIMIT 50'
        print c_string
        result = graph.run(c_string)
        for i in list(result):
            end_id = dict(i['s1'])
            uid_list1.append(end_id['uid'])

    s_string2 = 'START s0 = node:special_event_index(event="%s")\
                MATCH (s0)-[r]-(s) RETURN s.event_id as event' % theme_name2
    event_result2 = graph.run(s_string2)
    uid_list2 = []
    for event in event_result2:
        # print
        event_value = event['event']
        # event_list.append(event_value)
        c_string = 'START s0 = node:event_index(event="' + str(
            event_value) + '") '
        c_string += 'MATCH (s0)-[r]-(s1:User) return s1 LIMIT 100'
        result = graph.run(c_string)
        for i in list(result):
            end_id = dict(i['s1'])
            uid_list2.append(end_id['uid'])

    if diff == '0':
        uid_list1 = [i for i in set(uid_list1)]
        uid_list2 = [i for i in set(uid_list2)]
        detail_result1 = related_user_search(uid_list1, sort_flag)
        detail_result2 = related_user_search(uid_list2, sort_flag)

    if diff == '1':
        same_u = set(uid_list1) & set(uid_list2)
        same_u = [i for i in same_u]
        detail_result1 = related_user_search(same_u, sort_flag)
        detail_result2 = related_user_search(same_u, sort_flag)

    if diff == '2':
        diff_u1 = set(uid_list1) - (set(uid_list1) & set(uid_list2))
        diff_u1 = [i for i in diff_u1]
        diff_u2 = set(uid_list2) - (set(uid_list1) & set(uid_list2))
        diff_u2 = [i for i in diff_u2]
        detail_result1 = related_user_search(diff_u1, sort_flag)
        detail_result2 = related_user_search(diff_u2, sort_flag)
    return {'detail_result1': detail_result1, 'detail_result2': detail_result2}
Exemplo n.º 16
0
def create_node_or_node_rel(node_key1, node1_id, node1_index_name, rel_union,
                            node_key2, node2_id, node2_index_name):
    Index = ManualIndexManager(graph)
    node_index = Index.get_index(Node, node1_index_name)
    group_index = Index.get_index(Node, node2_index_name)
    print node_index
    print group_index
    tx = graph.begin()
    node1 = node_index.get(node_key1, node1_id)[0]
    node2 = group_index.get(node_key2, node2_id)[0]
    if not (node1 and node2):
        print "node does not exist"
        return 'does not exist'
    # c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') MATCH (start_node)-[r:%s]->(end_node) RETURN r" % (
    # node1_index_name,node_key1, node1_id, node2_index_name, node_key2, node2_id, rel)
    # # return c_string

    # result = graph.run(c_string)
    # # print result
    # rel_list = []
    # for item in result:
    #     rel_list.append(item)
    # # print rel_list
    # if rel in rel_list:
    #     return 'has relation already'
    rel = rel_union.split(',')[0]
    if rel in [other_rel, event_other, organization_tag, user_tag]:
        rel_name = rel_union.split(',')[1]
        c_string = "START start_node=node:%s(%s='%s'), end_node=node:%s(%s='%s') \
                    MATCH (start_node)-[r:%s]->(end_node) RETURN type(r), r.name"                                                                                  % (node1_index_name,\
                    node_key1, node1_id, node2_index_name, node_key2, node2_id, rel)
        result = graph.run(c_string)
        exist_relation = []
        for i in result:
            dict_i = dict(i)
            exist_relation = dict_i['r.name'].split(',')
        if exist_relation:
            del_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') \
                   MATCH (start_node)-[r:%s]->(end_node) delete r"                                                                   % (node1_index_name,\
                    node_key1, node1_id, node2_index_name, node_key2, node2_id, rel)
            result = graph.run(del_string)
        exist_relation.append(rel_name)
        exist_relation = [i for i in set(exist_relation)]
        add_relation_string = ','.join(exist_relation)
        c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s')\
                create (start_node)-[r:%s {name:'%s'} ]->(end_node)  "                                                                       %(node1_index_name,\
                node_key1, node1_id, node2_index_name, node_key2, node2_id, rel, add_relation_string)
        result = graph.run(c_string)
    else:
        rel = Relationship(node1, rel, node2)
        graph.create(rel)
        print "create success"
    return True
Exemplo n.º 17
0
def delete_theme(theme_name, submit_user):
    en_name = p.get_pinyin(theme_name)
    en_name = en_name.lower()
    print en_name,'000000000'
    try:
        es_event.delete(index=special_event_name, doc_type=special_event_type, id=en_name)
        s_string = 'START s0 = node:special_event_index(event="%s") \
                MATCH (s0)-[r]-(s) delete r,s0' %(en_name)
        graph.run(s_string)
    except:
        return '0'
    return '1'
Exemplo n.º 18
0
def compare_event_group(group_name1, group_name2, sort_flag, diff):
    s_string1 = 'START s0 = node:group_index(group="%s")\
                MATCH (s0)-[r]-(s) RETURN s.uid as user_id' % group_name1
    group_result1 = graph.run(s_string1)
    event_list1 = []
    for event in group_result1:
        user_dict = dict(event)
        usd = user_dict['user_id']
        c_string = 'START s0 = node:node_index(uid="' + str(usd) + '") '
        c_string += 'MATCH (s0)-[r]-(s1:Event) return s1 LIMIT 50'
        # print c_string
        result = graph.run(c_string)
        for i in list(result):
            # print i
            end_id = dict(i['s1'])
            event_list1.append(end_id['event_id'])
    print len(event_list1)

    s_string2 = 'START s0 = node:group_index(group="%s")\
                MATCH (s0)-[r]-(s) RETURN s.uid as user_id' % group_name2
    group_result2 = graph.run(s_string2)
    event_list2 = []
    for event in group_result2:
        user_dict = dict(event)
        usd = user_dict['user_id']
        c_string = 'START s0 = node:node_index(uid="' + str(usd) + '") '
        c_string += 'MATCH (s0)-[r]-(s2:Event) return s2 LIMIT 50'
        # print c_string
        result = graph.run(c_string)
        for i in list(result):
            print i
            end_id = dict(i['s2'])
            event_list2.append(end_id['event_id'])
    print len(event_list2)

    if diff == '0':
        detail_result1 = event_detail_search(event_list1, sort_flag)
        detail_result2 = event_detail_search(event_list2, sort_flag)

    if diff == '1':
        same_e = set(event_list1) & set(event_list2)
        same_e = [i for i in same_e]
        detail_result1 = event_detail_search(same_e, sort_flag)
        detail_result2 = event_detail_search(same_e, sort_flag)

    if diff == '2':
        diff_e1 = set(event_list1) - (set(event_list1) & set(event_list2))
        diff_e1 = [i for i in diff_e1]
        diff_e2 = set(event_list2) - (set(event_list1) & set(event_list2))
        diff_e2 = [i for i in diff_e2]
        detail_result1 = event_detail_search(diff_e1, sort_flag)
        detail_result2 = event_detail_search(diff_e2, sort_flag)
    return {'detail_result1': detail_result1, 'detail_result2': detail_result2}
Exemplo n.º 19
0
def group_user_rank(g_name, submit_user):
    group_id = p.get_pinyin(g_name)
    group_id = group_id.lower()
    print group_id
    uid_string = es_group.get(index=group_name,
                              doc_type=group_type,
                              id=group_id,
                              fields=['people'])
    uid_list = uid_string['fields']['people'][0].split('&')

    indx_id_list = []
    for i in uid_list:
        a = graph.run('start n=node:' + node_index_name + '("' +
                      people_primary + ':' + str(i) + '") return id(n)')
        for j in a:
            indx_id_list.append(str(dict(j)['id(n)']))
    event_id_string = ','.join(indx_id_list)
    query = 'start d=node(' + event_id_string + '),e=node(' + event_id_string + ') match (d)-[r]->(e) return d,type(r),e'
    result = graph.run(query)
    exist_relation = []
    exist_relation_string = []
    for i in result:
        # print i
        dict_i = dict(i)
        start_id = dict_i['d']['uid']
        start_name = user_name_search(start_id)
        end_id = dict_i['e']['uid']
        end_name = user_name_search(end_id)
        exist_relation.append([start_id, start_name, relation_dict[dict_i['type(r)']], \
                    end_id, end_name])
        # print exist_relation
        relation_string = start_id + '-' + end_id
        exist_relation_string.append(relation_string)
    set_exist_relation = set(exist_relation_string)
    relation_set_count = len(list(set_exist_relation))
    node_count = len(uid_list)
    total_count = node_count * (node_count - 1) / 2
    try:
        relation_degree = float(relation_set_count) / total_count
    except:
        relation_degree = 0
    if relation_degree == 0:
        conclusion = u'无关联'
    elif relation_degree < 0.33 and relation_degree > 0:
        conclusion = u'关联度较低'
    elif relation_degree >= 0.33 and relation_degree < 0.66:
        conclusion = u'关联度适中'
    elif relation_degree >= 0.66:
        conclusion = u'联系紧密'  ##未定义!!
    return {'relation_table':exist_relation, 'relation_count':relation_set_count,\
        'conclusion':conclusion, 'relation_degree':relation_degree}
Exemplo n.º 20
0
def group_tab_graph(group_name, node_type, relation_type, layer):
    s_string = 'START s0 = node:group_index(group="' + group_name + '")  \
                MATCH (s0)-[r]-(s) RETURN s.uid as uid'

    all_uid_list = []  #for map
    user_list = graph.run(s_string)
    origin_relation = []
    s_string2 = 'START s0 = node:group_index(group="' + group_name + '")  \
                MATCH (s0)-[r]-(s) RETURN r'

    user_list_o = graph.run(s_string2)
    for r in user_list_o:
        r1 = dict(r)['r']
        origin_relation.append(r1)

    # b = Node("Group", group=group_name)
    # print g.degree(b),'-=-=-=-=-=----------------'
    if node_type != '':
        node_type = ':' + node_type
    # if relation_type!='':
    #     relation_type = ':' + relation_type
    user_relation = []
    # total_user = len(list(uid_list))
    uid_list = []
    u_nodes_list = {}  #all user nodes
    e_nodes_list = {}  #all event nodes
    only_uid_no = []
    for uid in user_list:
        uid_value = str(uid['uid'])
        only_uid_no.append(uid_value)
        user_name = user_name_search(uid_value)
        # print uid_value,'000000000000'
        all_uid_list.append([str(uid_value), user_name])
        uid_list.append([str(uid_value), user_name])  # = user_name  #取uid
        u_nodes_list[str(uid_value)] = user_name  #取uid
    # u_nodes_list.extend(uid_list)
    # all_uid_list.extend(uid_list)
    relation = get_graph_single(only_uid_no, node_type, relation_type, layer)
    # print relation,'len(relation)'
    relation.extend(origin_relation)
    relation = [i for i in set(relation)]
    result = draw_graph(relation)
    for i in only_uid_no:
        try:
            result['map_uid'].append(i)
        except:
            result['map_uid'] = []
            result['map_uid'].append(i)
    result['map_uid'] = [i for i in set(result['map_uid'])]
    # print len(result['node']['uid']), len(result['map_uid'])
    return result
Exemplo n.º 21
0
def show_relation(node_key1, node1_id, node1_index_name, node_key2, node2_id,
                  node2_index_name):
    Index = ManualIndexManager(graph)
    node_index = Index.get_index(Node, node1_index_name)
    group_index = Index.get_index(Node, node2_index_name)
    print node_index
    print group_index
    tx = graph.begin()
    node1 = node_index.get(node_key1, node1_id)[0]
    node2 = group_index.get(node_key2, node2_id)[0]
    if not (node1 and node2):
        print "node does not exist"
        return 'does not exist'
    c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') MATCH (start_node)-[r]-(end_node) RETURN r" % (
        node1_index_name, node_key1, node1_id, node2_index_name, node_key2,
        node2_id)
    # return c_string
    print c_string
    result = graph.run(c_string)
    # print result
    rel_list = []
    for item in result:
        relation = dict(item)['r'].type()
        relation_ch = relation_dict[relation]
        if relation in [other_rel, event_other, organization_tag, user_tag]:
            relation_name = dict(item)['r']['name']
            relaiton_name_list = relation_name.split(',')
            for i_name in relaiton_name_list:
                relation_ch2 = relation_ch + '-' + i_name
                relation = relation + '&' + relation_name
                rel_list.append(relation_ch2)
        else:
            rel_list.append(relation_ch)
    return rel_list
Exemplo n.º 22
0
def show_node_detail(node_type, item, submit_user):
    if node_type == 'User' or node_type == 'Org':
        field = p_column
        # field = ['uid', 'uname','location', 'influence', 'activeness', 'sensitive','keywords_string', 'user_tag']
        index_n = node_index_name
        index_key = people_primary
        node_key = group_node
        node_result = search_user(item, field, '')[0]
        tag = deal_user_tag(item, submit_user, '')[0]
        node_result.append(tag)

    if node_type == 'Event':
        field = [
            'en_name', 'name', 'event_type', 'real_time', 'real_geo',
            'uid_counts', 'weibo_counts', 'keywords', 'work_tag'
        ]
        node_result = search_event(item, field, submit_user)[0]
        tag = deal_event_tag(item, submit_user)[0]
        node_result.append(tag)
        index_n = event_index_name
        index_key = 'event'
        node_key = special_event_node

    s_string = 'START s3 = node:%s(%s="%s") MATCH (s0:%s)-[r]-(s3) return s0' \
               %(index_n, index_key, item, node_key)
    event_result = graph.run(s_string)
    events = []
    for special_event in event_result:
        events.append(special_event[0][index_key])
    # result = node_result[0]
    node_result.append(events)
    return node_result
Exemplo n.º 23
0
def create_rel(node_key1, node1_list, node1_index_name, rel, node_key2,
               node2_id, node2_index_name, submit_user):
    Index = ManualIndexManager(graph)
    node_index = Index.get_index(Node, node1_index_name)
    group_index = Index.get_index(Node, node2_index_name)
    tx = graph.begin()
    print node1_list, '00990909000000000000'
    for node1_id in node1_list:
        print node1_id, '----==!!!==----'
        node1 = node_index.get(node_key1, node1_id)
        node1 = node_index.get(node_key1, node1_id)[0]
        node2 = group_index.get(node_key2, node2_id)[0]
        if not (node1 and node2):
            print "node does not exist"
            return 'node does not exist'
        c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') MATCH (start_node)-[r:%s]->(end_node) RETURN r" % (
            node1_index_name, node_key1, node1_id, node2_index_name, node_key2,
            node2_id, rel)
        result = graph.run(c_string)
        rel_list = []
        for item in result:
            rel_list.append(item)
        if rel not in rel_list:
            rel2 = Relationship(node1, rel, node2)
            graph.create(rel2)
            print "create success"
    return 'success'
Exemplo n.º 24
0
def create_node_or_node_rel(node_key1, node1_id, node1_index_name, rel,
                            node_key2, node2_id, node2_index_name):
    Index = ManualIndexManager(graph)
    node_index = Index.get_index(Node, node1_index_name)
    group_index = Index.get_index(Node, node2_index_name)
    print node_index
    print group_index
    tx = graph.begin()
    node1 = node_index.get(node_key1, node1_id)[0]
    node2 = group_index.get(node_key2, node2_id)[0]
    if not (node1 and node2):
        print "node does not exist"
        return '1'
    c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') MATCH (start_node)-[r:%s]->(end_node) RETURN r" % (
        node1_index_name, 'pname', node1_id, node2_index_name, 'pname',
        node2_id, rel)
    print c_string
    result = graph.run(c_string)
    print result
    rel_list = []
    for item in result:
        rel_list.append(item)
    print rel_list
    if not rel_list:
        rel = Relationship(node1, rel, node2)
        graph.create(rel)
        print "create success"
    else:
        print "The current two nodes already have a relationship"
        return '0'
    return '2'
Exemplo n.º 25
0
def create_rel(node_key1, node1_list, node1_index_name, rel, node_key2, node2_id, node2_index_name, submit_user):
    Index = ManualIndexManager(graph)
    node_index = Index.get_index(Node, node1_index_name)
    group_index = Index.get_index(Node, node2_index_name)
    tx = graph.begin()
    for node1_id in node1_list:
        # print node2_id
        # print node1_id,'-----'
        # node1 = node_index.get(node_key1, node1_id)
        try:
            node1 = node_index.get(node_key1, node1_id)[0]
            # print node1, node1_id,'node1 hey2222!!'

        except:
            continue
            # print node1, node1_id,'node1 hey!!'
            return 'uid1 not exist'
        node2 = group_index.get(node_key2, node2_id)[0]
        if not (node1 and node2):
            print "node does not exist"
            return 'node does not exist'
        c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') MATCH (start_node)-[r:%s]->(end_node) RETURN r" % (
        node1_index_name, node_key1, node1_id, node2_index_name, node_key2, node2_id, rel)
        result = graph.run(c_string)
        rel_list = []
        for item in result:
            print item,'00000000000'
            rel_list.append(item)
        if rel not in rel_list:
            rel2 = Relationship(node1, rel, node2)
            graph.create(rel2)
            print "create success"
        else:
            return 'has relation'
    return '1'
Exemplo n.º 26
0
def create_node_or_node_rel(node_key1, node1_id, node1_index_name, rel,
                            node_key2, node2_id, node2_index_name):
    Index = ManualIndexManager(graph)
    node_index = Index.get_index(Node, node1_index_name)
    group_index = Index.get_index(Node, node2_index_name)

    tx = graph.begin()
    if len(node_index.get(node_key1, node1_id)) == 0:
        return 'Node1 does not exist'

    if len(group_index.get(node_key2, node2_id)) == 0:
        return "Node2 does not exist"

    node1 = node_index.get(node_key1, node1_id)[0]
    node2 = group_index.get(node_key2, node2_id)[0]
    if not (node1 and node2):
        return "Node1 and Node2 do not exist"

    c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') MATCH (start_node)-[r:%s]->(end_node) RETURN r" \
               % (node1_index_name, node_key1, node1_id, node2_index_name, node_key2, node2_id, rel)

    result = graph.run(c_string)
    # print result
    rel_list = []
    for item in result:
        rel_list.append(item)

    if not rel_list:
        rel = Relationship(node1, rel, node2)
        graph.create(rel)
        return "Create success"
    else:
        return "The current two nodes already have a relationship"
Exemplo n.º 27
0
def create_node_or_node_rel(node_key1, node1_id, node1_index_name, rel,
                            node_key2, node2_id, node2_index_name):
    Index = ManualIndexManager(graph)
    node_index = Index.get_index(Node, node1_index_name)
    group_index = Index.get_index(Node, node2_index_name)
    print node_index
    print group_index
    tx = graph.begin()
    node1 = node_index.get(node_key1, node1_id)[0]
    node2 = group_index.get(node_key2, node2_id)[0]
    if not (node1 and node2):
        print "node does not exist"
        return 'does not exist'
    c_string = "START start_node=node:%s(%s='%s'),end_node=node:%s(%s='%s') MATCH (start_node)-[r:%s]->(end_node) RETURN r" % (
        node1_index_name, node_key1, node1_id, node2_index_name, node_key2,
        node2_id, rel)
    # return c_string

    result = graph.run(c_string)
    # print result
    # rel_list = []
    for item in result:
        rel_list.append(item)
    # print rel_list
    if rel in rel_list:
        return 'has relation already'
    rel = Relationship(node1, rel, node2)
    graph.create(rel)
    print "create success"
    return True
Exemplo n.º 28
0
def show_node_detail(node_type, item, submit_user):
    if node_type == 'User' or node_type == 'Org':
        field = [
            'uid', 'uname', 'topic_string', 'domain', 'function_description'
        ]
        # field = ['uid', 'uname','location', 'influence', 'activeness', 'sensitive','keywords_string', 'user_tag']
        index_n = node_index_name
        index_key = people_primary
        node_key = group_node
        node_result = search_user(item, field)[0]
        tag = deal_user_tag(item, submit_user)[0]
        node_result.append(tag)

    if node_type == 'Event':
        field = ['en_name', 'name', 'real_geo', 'real_time',  'category', 'real_person', 'real_auth', \
              'start_ts', 'end_ts','description', 'related_docs']
        node_result = search_event(item, field)[0]
        tag = deal_event_tag(item, submit_user)[0]
        node_result.append(tag)
        index_n = event_index_name
        index_key = 'event'
        node_key = special_event_node

    s_string = 'START s3 = node:%s(%s="%s") MATCH (s0:%s)-[r]-(s3) return s0' \
               %(index_n, index_key, item, node_key)
    event_result = graph.run(s_string)
    events = []
    for special_event in event_result:
        events.append(special_event[0][index_key])
    # result = node_result[0]
    node_result.append(events)
    return node_result
Exemplo n.º 29
0
def neo4j_test():

    p_string = 'START n=node:event_index(event_id="bei-jing-fang-jia-zheng-ce-1480176000") return labels(n)'
    p_result = graph.run(p_string)
    for item in p_result:
        print item

    return 'sss'
Exemplo n.º 30
0
def del_e_theme_rel(theme_name, event_id):
    s_string = 'START s0 = node:special_event_index(event="%s"),s3 = node:event_index(event="%s")\
                MATCH (s0)-[r]-(s3) DELETE r' % (theme_name, event_id)

    # print s_string.encode('utf-8'),'00000000000000000000'
    graph.run(s_string)
    s_string2 = 'START s0 = node:special_event_index(event="%s")\
                MATCH (s0)-[r]-() return r' % (theme_name)
    print s_string2.encode('utf-8'), '00000000000000000000'
    result = graph.run(s_string2)
    result_list = []
    for i in result:
        result_list.append(i)
    # result_d = dict(result)
    print result_list, '@@@@@@@@@@@@@'

    return 'true'