示例#1
0
class Utils(object):
    _instances = {}

    @classmethod
    def get_instance(cls, graph_tag='default'):
        if graph_tag not in cls._instances:
            cls._instances[graph_tag] = Utils(graph_tag)
        return cls._instances[graph_tag]

    def __init__(self, graph_tag):
        self.g = Graph(graph_tag)

    @staticmethod
    def format_graph_fields(graph_data):
        format_data = {}
        for k, v in graph_data.items():
            format_data[str(k)] = v[0] if type(v) == list else v

        format_data['id'] = format_data['T.id']
        format_data['label'] = format_data['T.label']

        del format_data['T.id']
        del format_data['T.label']

        return format_data

    def graph_visualization(self, size=100):
        """
        将图数据库中的数据可视化

        :param size: 起始点的数量(歌曲)
        :return:
        """
        dsl = """
            g.V().hasLabel('song').limit(size).as('SONGS').
                project('song', 'user', 'singer').
                    by(select('SONGS').valueMap(true)).
                    by(select('SONGS').in('favorite_song').valueMap(true).fold()).
                    by(select('SONGS').in('create_song').valueMap(true).fold())
        """
        bindings = {"size": size}
        callback = self.g.query_dsl(dsl, bindings)

        relation = {"vertex": [], "edge": []}

        for ret in callback.result():
            for data in ret:
                song = self.format_graph_fields(data["song"])
                relation["vertex"].append(song)

                user_list = []
                for user in data["user"]:
                    user = self.format_graph_fields(user)
                    user_list.append(user)
                    relation["edge"].append({
                        "from": user["id"],
                        "to": song["id"],
                        "type": "favorite_song"
                    })
                relation["vertex"].extend(user_list)

                singer_list = []
                for singer in data["singer"]:
                    singer = self.format_graph_fields(singer)
                    singer_list.append(singer)
                    relation["edge"].append({
                        "from": singer["id"],
                        "to": song["id"],
                        "type": "create_song"
                    })
                relation["vertex"].extend(singer_list)

        result = {
            "type": "force",
            "categories": [{
                "name": "用户"
            }, {
                "name": "歌曲"
            }, {
                "name": "歌手"
            }],
            "nodes": [],
            "links": []
        }
        category_index = {"user": 0, "song": 1, "singer": 2}

        vertex_index = {}

        for i, vertex in enumerate(relation["vertex"]):
            result["nodes"].append({
                "name": vertex["name"],
                "value": 1,
                "category": category_index[vertex["label"]]
            })
            vertex_index[vertex["id"]] = i

        for edge in relation["edge"]:
            result["links"].append({
                "source": vertex_index[edge["from"]],
                "target": vertex_index[edge["to"]]
            })

        return json.dumps(result)
class User(object):
    _instances = {}

    @classmethod
    def get_instance(cls, graph_tag='default'):
        if graph_tag not in cls._instances:
            cls._instances[graph_tag] = User(graph_tag)
        return cls._instances[graph_tag]

    def __init__(self, graph_tag):
        self.g = Graph(graph_tag)

    def find_similar_user(self, uid, top_n=3):
        """
        找到与当前用户兴趣相近的用户

        :param uid: 用户 ID
        :return: 兴趣相近的用户信息
        """
        dsl = """
            g.V(uid).as('v1').
                out('favorite_song').in('favorite_song').dedup().where(neq('v1')).as('v2').
                project('v1', 'v2', 'v1n', 'v2n').
                    by(select('v1')).
                    by(select('v2')).
                    by(select('v1').out('favorite_song').fold()).
                    by(select('v2').out('favorite_song').fold()).
                as('q1').
                project('v1', 'v2', 'i', 'u').
                    by(select('v1')).
                    by(select('v2')).
                    by(select('v1n').as('n').
                       select('q1').select('v2n').unfold().
                          where(within('n')).
                          count()).
                    by(union(select('v1n').
                             select('q1').select('v2n')).unfold().
                       dedup().count()).
                project('v1', 'v2', 'jaccard').
                    by(select('v1').valueMap(true)).
                    by(select('v2').valueMap(true)).
                    by(math('i/u')).order().by(select('jaccard'), desc).
                limit(top_n)
        """
        bindings = {"uid": uid, "top_n": top_n}
        callback = self.g.query_dsl(dsl, bindings)

        result = []

        for ret in callback.result():
            for data in ret:
                v2_vertex = Utils.format_graph_fields(data["v2"])
                result.append({
                    "uid": v2_vertex["id"],
                    "name": v2_vertex["name"],
                    "jaccard_score": round(data["jaccard"], 5)
                })
        return result

    def save_vertex(self, uid, properties):
        """
        保存用户

        :param uid: 用户 ID
        :param properties: 用户属性
        :return: 保存结果
        """
        return self.g.save_vertex("user", str(uid), properties)

    def save_edge(self, uid, related_id, related_type, properties):
        """
        保存用户关系

        :param uid: 用户 ID
        :param related_id: 相关联的顶点 ID
        :param related_type: 关系类型
        :param properties: 关系属性
        :return: 保存结果
        """
        return self.g.save_edge(related_type, str(uid), str(related_id),
                                properties)