Exemplo n.º 1
0
def draw_graph(relation_list):
    result = {}
    key_dict = {
        'User': '******',
        'Event': 'event_id',
        'Group': 'group',
        'SpecialEvent': 'event'
    }
    map_eid = []
    result_relation = []  #[[node1,relation,node2],...]
    for i in relation_list:
        # print i,'**********************'
        this_relation = ['', '', '']  #[node1,relation,node2]
        only_relation = []  #[node1,node2]
        for m in walk(i):
            try:
                this_relation[1] = m.type()
                # print m.type(),'!!!!!!!!!'
            except:
                aa = m.labels()
                aa = [i for i in aa]
                if len(aa) == 1:
                    try:
                        primary_key = key_dict[aa[0]]
                    except:
                        continue
                    primary_value = m[primary_key]
                    only_relation.append(primary_value)
                    if aa[0] == 'User':
                        eu_name = user_name_search(m['uid'])
                    elif aa[0] == 'Event':
                        # print m['event_id'].encode('utf-8'),'************'
                        if m['event_id'] in [u'徐玉玉事件', u'大学生失联']:
                            continue
                        eu_name = event_name_search(m['event_id'])
                        map_eid.append(m['event_id'])
                    else:
                        eu_name = m[primary_key]
                if len(aa) > 1 or len(aa) < 1:
                    primary_key = 'User'
                    primary_value = m[primary_key]
                    eu_name = user_name_search(m['uid'])
                    only_relation.append(m['uid'])
                try:
                    result[primary_key][primary_value] = eu_name
                except:
                    result[primary_key] = {}
                    result[primary_key][primary_value] = eu_name
        if len(only_relation) < 2:
            continue
        this_relation[0] = only_relation[0]
        this_relation[2] = only_relation[1]
        result_relation.append(this_relation)
    return {
        'result_relation': result_relation,
        'node': result,
        'map_eid': map_eid
    }
Exemplo n.º 2
0
def event_list_theme(event):
    s_string = 'START s0 = node:special_event_index(event="%s")\
                MATCH (s0)-[r]-(s:Event) RETURN s' % (event)
    # print s_string
    e_list = graph.run(s_string)
    e_list_l = []
    for i in e_list:
        print i
        e_this = dict(i)['s']['event_id']
        e_name = event_name_search(e_this)
        e_list_l.append([e_this, e_name])
    return e_list_l
Exemplo n.º 3
0
def search_way(node1, node2, node_type1, node_type2):
    relation_type = [
        'join', 'pusher', 'maker', 'other_relationship', 'friend', 'relative',
        'colleague', 'user_tag'
    ]
    index_type_dict = {'User': '******', 'Event': 'event_index'}
    primary_idct = {'User': '******', 'Event': 'event'}
    key_dict = {'User': '******', 'Event': 'event_id'}
    origin_idlist = [node1, node2]
    print origin_idlist
    if node_type1 == 'User':
        start_node_card = related_user_search([node1], 'activeness')[0]
    else:
        start_node_card = event_detail_search([node1], 'start_ts')[0]

    if node_type2 == 'User':
        end_node_card = related_user_search([node2], 'activeness')[0]
    else:
        end_node_card = event_detail_search([node2], 'start_ts')[0]

    c_string = 'START node1 = node:' + index_type_dict[
        node_type1] + '(' + primary_idct[node_type1] + '="' + node1 + '"),'
    c_string += 'node2 = node:' + index_type_dict[
        node_type2] + '(' + primary_idct[node_type2] + '="' + node2 + '") '
    c_string += 'MATCH p = allShortestPaths(node1-[r*..5]-node2) return r'
    print c_string
    result = graph.run(c_string)

    # uid_list = []#for card
    # eid_list = []#for card
    middle_card = []  #for card, middle nodes
    uid_dict = {}  #for graph
    eid_dict = {}  #for graph
    relation_all = list(result)
    # print relation_all,'!!!!!!!!!!!!!!!1'
    relation_result = []
    relation_result2 = []
    # print [relation_all[0]['r'] , relation_all[1]['r']]
    # if relation_all[0]['r'] == relation_all[1]['r']:
    #     return 'haha'
    # else:
    #     return [relation_all[0]['r'] , relation_all[1]['r']]
    length_relation = [0, 0]
    length_relation[0] = len(relation_all)
    for relation in relation_all:
        # print list(relation['r']),'99999999999999999'
        if len(list(relation['r'])) < 2:
            return 0  #返回0 说明这两个节点有直接关系
        print relation, 'relation'
        length_relation[1] = len(list(relation['r']))
        line_rel = []
        for i in relation['r']:
            # print i
            a = walk(i)
            # print a
            this_relation = []
            # aa = []
            for m in a:  #a=[node1,r,node2]
                try:
                    m.type()
                except:
                    aa = m.labels()
                    aa = [i for i in aa]
                    mm = dict(m)
                    # print mm,'========'
                    if mm.has_key('uid'):
                        # print mm
                        if m['uid'] == '1765891182':
                            print a, m, '----000000000000000000'
                        eu_name = user_name_search(m['uid'])
                        if uid_dict.has_key(m['uid']) == False:
                            uid_dict[m['uid']] = eu_name
                            if m['uid'] not in origin_idlist:
                                # print m['uid'], origin_idlist,'inininini'
                                mid_card = related_user_search([m['uid']],
                                                               'activeness')
                                if len(mid_card) == 0:
                                    middle_card.append({'uid': m['uid']})
                                else:
                                    # print len(mid_card), '!!!!!!!!!!!!!'
                                    middle_card.append(mid_card[0])
                        this_relation.append([m['uid'], eu_name])
                        if [m['uid'], eu_name] not in line_rel:
                            line_rel.append([m['uid'], eu_name])
                    elif mm.has_key('event_id'):
                        eu_name = event_name_search(m['event_id'])
                        if eid_dict.has_key(m['event_id']) == False:
                            eid_dict[m['event_id']] = eu_name
                            if m['event_id'] not in origin_idlist:
                                mid_card = event_detail_search([m['event_id']],
                                                               'start_ts')
                                if len(mid_card) == 0:
                                    middle_card.append(
                                        {'event_id': m['event_id']})
                                else:
                                    middle_card.append(mid_card[0])
                        this_relation.append([m['event_id'], eu_name])
                        if [m['event_id'], eu_name] not in line_rel:
                            line_rel.append([m['event_id'], eu_name])
                    else:
                        break

                if len(this_relation) > 1:
                    if this_relation not in relation_result:
                        relation_result.append(this_relation)
        # print len(line_rel), length_relation[1],'000000000'
        # if len(line_rel) == length_relation[1]:
        #     relation_result2.append(line_rel)
    return {'relation':relation_result, 'start_node_card':start_node_card, 'end_node_card':end_node_card,\
            'user_nodes':uid_dict, 'event_nodes': eid_dict, 'middle_card':middle_card,'length_relation':length_relation}
Exemplo n.º 4
0
def search_related_user(item):
    query_body = {
        "query": {
            'bool': {
                'should': [{
                    "wildcard": {
                        'uid': '*' + str(item.encode('utf-8')) + '*'
                    }
                }, {
                    "wildcard": {
                        'uname': '*' + str(item.encode('utf-8')) + '*'
                    }
                }]
            }
        },
        'size': 10
    }
    only_uid = []
    user_uid_list = []
    u_nodes_list = {}

    try:
        name_results = es_user_portrait.search(index=portrait_name, doc_type=portrait_type, \
                body=query_body, fields=['uid','uname'])['hits']['hits']
        # print name_results,'@@@@@@@@@@@@@@@@@'
    except:
        return 'node does not exist'
    if len(name_results) == 0:
        return 'node does not exist'
    for i in name_results:
        # print i
        uid = i['fields']['uid'][0]
        uname = i['fields']['uname'][0]
        only_uid.append(uid)
        u_nodes_list[uid] = uname
        user_uid_list.append([uid, uname])
    print len(user_uid_list)
    e_nodes_list = {}
    user_relation = []
    mid_uid_list = []  #存放第一层的数据,再以这些为起始点,扩展第二层
    mid_eid_list = []
    for uid_value in user_uid_list:
        c_string = 'START s0 = node:node_index(uid="' + str(
            uid_value[0]) + '") '
        c_string += 'MATCH (s0)-[r1]-(s1) return s0,r1,s1 LIMIT 1'

        result = graph.run(c_string)
        # print list(result),'-----------------'
        for i in list(result):
            start_id = i['s0']['uid']
            # # start_id = s0['uid']
            relation1 = i['r1'].type()
            m_id = dict(i['s1'])
            if m_id.has_key('uid'):
                middle_id = m_id['uid']
                mid_uid_list.append(middle_id)
                user_name = user_name_search(middle_id)
                # print middle_id,'2222222222222222222'
                u_nodes_list[str(middle_id)] = user_name
                user_relation.append([start_id, relation1, middle_id])
            if m_id.has_key('envent_id'):
                middle_id = m_id['envent_id']
                mid_eid_list.append(middle_id)
                event_name = event_name_search(middle_id)
                e_nodes_list[str(middle_id)] = event_name
                user_relation.append([start_id, relation1, middle_id])
    print len(mid_uid_list)
    print len(mid_eid_list), '++++++++++++++++'
    for mid_uid in mid_uid_list:
        c_string = 'START s1 = node:node_index(uid="' + str(mid_uid) + '") '
        c_string += 'MATCH (s1)-[r2]->(s2:User) return s1,r2,s2 LIMIT 5'
        # print c_string
        result = graph.run(c_string)
        for i in result:
            start_mid_id = i['s1']['uid']
            relation2 = i['r2'].type()
            end_id = dict(i['s2'])
            if end_id.has_key('uid'):
                user_name = user_name_search(end_id['uid'])
                # print end_id['uid'],'333333333333333333333333'
                u_nodes_list[end_id['uid']] = user_name
                user_relation.append([start_mid_id, relation2, end_id['uid']])
            if end_id.has_key('envent_id'):
                event_name = event_name_search(end_id['event_id'])
                e_nodes_list[end_id['event_id']] = event_name
                user_relation.append(
                    [start_mid_id, relation2, end_id['envent_id']])
    for mid_eid in mid_eid_list:
        c_string = 'START s1 = node:event_index(event="' + str(mid_eid) + '") '
        c_string += 'MATCH (s1)-[r2]->(s2:User) return s1,r2,s2 LIMIT 3'
        event_result = graph.run(c_string)
        for i in event_result:
            relation2 = i['r2'].type()
            end_id = dict(i['s2'])
            if end_id.has_key('uid'):
                # print end_id['uid'],'44444444444444444444444'
                user_name = user_name_search(end_id['uid'])
                u_nodes_list[end_id['uid']] = user_name
                user_relation.append([mid_eid, relation2, end_id['uid']])
            if end_id.has_key('envent_id'):
                event_name = event_name_search(end_id['event_id'])
                e_nodes_list[end_id['event_id']] = event_name
                user_relation.append([mid_eid, relation2, end_id['envent_id']])
    return {'total_user':len(user_uid_list),'user_nodes':u_nodes_list,'event_nodes':e_nodes_list,\
            'relation':user_relation,'draw_nodes_length':len(u_nodes_list)}
Exemplo n.º 5
0
def compare_graph_theme(theme_name1, theme_name2, layer, diff):
    s_string1 = 'START s0 = node:special_event_index(event="%s")\
                MATCH (s0)-[r]-(s) RETURN s.event_id' % theme_name1
    theme_result1 = graph.run(s_string1)
    eid_list1 = []
    for i in theme_result1:
        e_dict = dict(i)
        print e_dict, '&&&&&&&&&&&&'
        esd = e_dict['s.event_id']
        eid_list1.append(esd)
    print len(eid_list1)

    s_string2 = 'START s0 = node:special_event_index(event="%s")\
                MATCH (s0)-[r]-(s) RETURN s.event_id' % theme_name2
    theme_result2 = graph.run(s_string2)
    eid_list2 = []
    for i in theme_result2:
        e_dict = dict(i)
        esd = e_dict['s.event_id']
        eid_list2.append(esd)
    print len(eid_list2)
    relation_1 = get_graph(eid_list1, layer)
    relation_2 = get_graph(eid_list2, layer)
    if diff == '0':
        e1 = draw_graph(relation_1)
        e2 = draw_graph(relation_2)
    if diff == '1':
        same_relation = set(relation_1) & set(relation_2)
        same_relation = [i for i in same_relation]
        e1 = draw_graph(same_relation)
        e2 = e1
    if diff == '2':
        same_relation = set(relation_1) & set(relation_2)
        only1_relation = set(relation_1) - same_relation
        only2_relation = set(relation_2) - same_relation
        e1 = draw_graph(only1_relation)
        e2 = draw_graph(only2_relation)

    for i in eid_list1:
        e_name = event_name_search(i)
        try:
            e1['node']['event_id'][i] = e_name

        except:
            e1['node']['event_id'] = {}
            e1['node']['event_id'][i] = e_name
        try:
            e1['map_eid'].append(i)
        except:
            e1['map_eid'] = []
            e1['map_eid'].append(i)
    for i in eid_list2:
        e_name = event_name_search(i)
        try:
            e2['node']['event_id'][i] = e_name
            # e2[]
        except:
            e2['node']['event_id'] = {}
            e2['node']['event_id'][i] = e_name
        try:
            e2['map_eid'].append(i)
        except:
            e2['map_eid'] = []
            e2['map_eid'].append(i)
    return {'e1': e1, 'e2': e2}
Exemplo n.º 6
0
def search_related_event_f(item):
    query_body = {
        "query": {
            'bool': {
                'should': [{
                    "wildcard": {
                        'keywords': '*' + str(item.encode('utf-8')) + '*'
                    }
                }, {
                    "wildcard": {
                        'en_name': '*' + str(item.encode('utf-8')) + '*'
                    }
                }, {
                    "wildcard": {
                        'name': '*' + str(item.encode('utf-8')) + '*'
                    }
                }]
            }
        },
        'size': 10
    }
    only_eid = []
    event_id_list = []
    u_nodes_list = {}
    e_nodes_list = {}
    event_relation = []
    try:
        name_results = es_event.search(index=event_name, doc_type=event_type, \
                body=query_body, fields=['name','en_name'])['hits']['hits']
    except:
        return 'node does not exist'
    if len(name_results) == 0:
        return 'node does not exist'
    print name_results, '*********************'
    for i in name_results:
        name = i['fields']['name'][0]
        en_name = i['fields']['en_name'][0]
        only_eid.append(en_name)
        e_nodes_list[en_name] = name
        event_id_list.append([en_name, name])

    for event_value in event_id_list:
        c_string = 'START s0 = node:event_index(event="' + str(
            event_value[0]) + '") '
        c_string += 'MATCH (s0)-[r1]-(s1) return s0,r1,s1 LIMIT 10'
        # print c_string,'==========='

        mid_eid_list = []  #存放第一层的数据,再以这些为起始点,扩展第二层
        mid_uid_list = []
        result = graph.run(c_string)
        # print list(result),'-----------------'
        for i in list(result):
            print i
            start_id = i['s0']['event_id']
            # start_id = s0['event']
            relation1 = i['r1'].type()
            m_id = dict(i['s1'])
            if m_id.has_key('uid'):
                middle_id = m_id['uid']
                mid_uid_list.append(middle_id)
                user_name = user_name_search(middle_id)
                u_nodes_list[middle_id] = user_name
                event_relation.append([start_id, relation1, middle_id])
            if m_id.has_key('envent_id'):
                middle_id = m_id['envent_id']
                mid_eid_list.append(middle_id)
                event_name2 = event_name_search(middle_id)
                e_nodes_list[middle_id] = event_name2
                event_relation.append([start_id, relation1, middle_id])

    # print mid_uid_list
    # print mid_eid_list,'++++++++++++++++'
    for mid_uid in mid_uid_list:
        c_string = 'START s1 = node:node_index(uid="' + str(mid_uid) + '") '
        c_string += 'MATCH (s1)-[r2]->(s2:Event) return s1,r2,s2 LIMIT 5'
        uid_result = graph.run(c_string)

        for i in uid_result:
            relation2 = i['r2'].type()
            end_id = dict(i['s2'])
            if end_id.has_key('uid'):
                user_name = user_name_search(end_id['uid'])
                u_nodes_list[end_id['uid']] = user_name
                event_relation.append([mid_uid, relation2, end_id['uid']])
            if end_id.has_key('envent_id'):
                event_name2 = event_name_search(end_id['envent_id'])
                e_nodes_list[end_id['envent_id']] = event_name2
                event_relation.append(
                    [mid_uid, relation2, end_id['envent_id']])
    for mid_eid in mid_eid_list:
        c_string = 'START s1 = node:event_index(event="' + str(mid_eid) + '") '
        c_string += 'MATCH (s1)-[r2]->(s2:Event) return s1,r2,s2 LIMIT 5'
        eid_result = graph.run(c_string)
        for i in eid_result:
            relation2 = i['r2'].type()
            end_id = dict(i['s2'])
            if end_id.has_key('uid'):
                user_name = user_name_search(end_id['uid'])
                u_nodes_list[end_id['uid']] = user_name
                event_relation.append([mid_eid, relation2, end_id['uid']])
            if end_id.has_key('envent_id'):
                event_name2 = event_name_search(end_id['envent_id'])
                e_nodes_list[end_id['envent_id']] = event_name2
                event_relation.append(
                    [mid_eid, relation2, end_id['envent_id']])

    return {'total_event':len(event_id_list),'user_nodes':u_nodes_list,'event_nodes':e_nodes_list,\
            'relation':event_relation}
Exemplo n.º 7
0
def theme_tab_graph(theme_name, node_type, relation_type, layer):
    s_string = 'START s0 = node:special_event_index(event="%s")\
                MATCH (s0)-[r]-(s) RETURN s.event_id as event' % theme_name
    # print s_string
    all_event_id = []  #for map,event_id
    event_result = graph.run(s_string)
    s_string2 = 'START s0 = node:special_event_index(event="%s")\
                MATCH (s0)-[r]-(s) RETURN r' % theme_name
    event_result_r = graph.run(s_string2)
    relation_list_o = []
    for r in event_result_r:
        r1 = dict(r)['r']
        relation_list_o.append(r1)

    # print event_list
    # b = Node("Group", group=group_name)
    # print g.degree(b),'-=-=-=-=-=----------------'
    if node_type != '':
        node_type = ':' + node_type
    # if relation_type!='':
    #     relation_type = ':' + relation_type
    event_relation = []
    only_event_o = []
    # total_event = len(list(event_list))
    event_list = []
    e_nodes_list = {}  #all event nodes
    u_nodes_list = {}  #all user nodes
    for event in event_result:
        event_value = event['event']
        only_event_o.append(event_value)
        event_name = event_name_search(event_value)
        event_list.append([event_value, event_name])  #取event
        e_nodes_list[event_value] = event_name
    all_event_id.extend(event_list)

    relation = get_graph_single(only_event_o, node_type, relation_type, layer)
    relation.extend(relation_list_o)
    relation = [i for i in set(relation)]
    result = draw_graph(relation)

    # try:
    #     result['node']['event'][theme_name] = theme_name
    # except:
    #     result['node']['event'] = {}
    #     result['node']['event'][theme_name] = theme_name

    for i in only_event_o:
        # e_name = event_name_search(i)
        # try:
        #     result['node']['event_id'][i] = e_name
        # except:
        #     result['node']['event_id'] ={}
        #     result['node']['event_id'][i] = e_name
        try:
            result['map_eid'].append(i)
        except:
            result['map_eid'] = []
            result['map_eid'].append(i)
    result['map_eid'] = [i for i in set(result['map_eid'])]
    # print len(result['node']['event_id']), len(result['map_eid'])
    return result