示例#1
0
    def recommends(cls, request):
        tracks = []
        pa = ParameterAdapter()
        limit = 10  # todo:get limit by parameter
        response = []

        # get request parameters
        request_body = None
        if request.method == "GET":
            request_body = list(request.GET.dict().keys())[0]
        else:
            request_body = request.body.decode("utf-8")

        posted = json.loads(request_body)
        posted_parameters = {} if not "parameters" in posted else posted["parameters"]

        # process by methods
        if request.method == "GET":
            # initialize when get
            SessionManager.set_session(request, SessionManager.CRITICIZE_SESSION, [])
            SessionManager.set_session(request, SessionManager.TRACK_SESSION, [])

            parameters = pa.request_to_parameters(TrackCriticizeType.Parameter, None, posted_parameters)
            tracks = cls.get_scored_tracks(parameters, None, [])

        else:
            track_id = posted["track_id"]
            criticize_type = TrackCriticizeType(posted["criticize_type"])
            track = cls.__get_track(track_id, tracks)
            tracks = cls.__get_session_tracks(request)  # get from session

            parameters = pa.request_to_parameters(criticize_type, track, posted_parameters)
            history = cls.__get_session_history(request)

            # merge history and make parameter
            parameters = ParameterAdapter.merge_parameters(history + [parameters])

            if criticize_type == TrackCriticizeType.Like:
                tracks = cls.get_favorite_tracks(parameters, track, tracks)
            else:
                tracks = cls.get_scored_tracks(parameters, track, tracks)

        if len(tracks) > 0:
            # to dictionary
            serialized_evaluated = [{"score": s.score, "item": s.item.to_dict(), "score_detail": s.score_detail} for s in tracks]

            # store to session
            SessionManager.set_session(request, SessionManager.TRACK_SESSION, [s["item"] for s in serialized_evaluated])
            SessionManager.add_session(request, SessionManager.CRITICIZE_SESSION, [p.to_dict() for p in parameters])

            if limit > 0:
                serialized_evaluated = serialized_evaluated[:limit]

            response = serialized_evaluated

        return HttpResponse(json.dumps(response), content_type="application/json")
示例#2
0
    def test_criticize_by_parameter(self):
        pa = ParameterAdapter()
        selected_track = random.sample(self.tracks, 1)[0]

        # by parameter
        criticize_type = TrackCriticizeType.Parameter
        post_parameters = {"bpm": "123"}  # dummy bpm value
        parameters = pa.request_to_parameters(criticize_type, selected_track, post_parameters)

        print(map(lambda p: p.__str__(), parameters))

        scored = RecommendApi.get_scored_tracks(parameters, selected_track, self.tracks)
        print("tracks: {0}".format(len(scored)))
        self.print_tracks(map(lambda s: s.item, scored[:10]))
示例#3
0
    def test_criticize_by_pattern(self):
        pa = ParameterAdapter()
        selected_track = random.sample(self.tracks, 1)[0]

        # by parameter
        criticize_type = TrackCriticizeType.Pattern
        evaluator = Track.make_evaluator()
        criticize_patterns = evaluator.make_pattern(self.tracks, selected_track)

        pattern = random.sample(criticize_patterns, 1)[0]
        post_parameters = {"pattern": pattern.pattern}
        parameters = pa.request_to_parameters(criticize_type, selected_track, post_parameters)

        print(map(lambda p: p.__str__(), parameters))

        scored = RecommendApi.get_scored_tracks(parameters, selected_track, self.tracks)
        print("tracks: {0}".format(len(scored)))
        self.print_tracks(map(lambda s: s.item, scored[:10]))