def similar_content(self,
                     request,
                     user_id,
                     content_id,
                     model='Recommendation'):
     """get content similar to specific content."""
     INDEX, PAGE_SIZE = self.index_count(request)
     LOGGER.info("request params are :{}".format(self.personalization))
     LOGGER.info("get simmilar content for content_id : %s and user_id :%s",
                 content_id, user_id)
     content_seen_list = ContentSeenViewSet.content_seen_list(user_id)
     content_seen_list[
         1] = content_seen_list[1] if self.personalization.get(
             'remove_content_seen', True) else []
     LOGGER.debug("content seen by user {}".format(content_seen_list[1]))
     model = apps.get_model('recommendation', model)
     order_by = self.personalization.get('order_by', 'confidence')
     similar_contents = filter_by_key(
         model, content_seen=content_id).order_by('-' + order_by).exclude(
             content_recommended__in=content_seen_list[1])
     similar_contents = list(similar_contents) if similar_contents else []
     if len(similar_contents) < REC_MINLIMIT:
         LOGGER.debug(
             "length of similar contents  is %s which is less than min limit %s",
             len(similar_contents), str(REC_MINLIMIT))
         default = RecommendationGenerator.default(
             content_seen_list=content_seen_list)
         similar_contents += default
         LOGGER.debug("added from coldstart : %d", len(default))
         return []
     similar_contents = similar_contents[INDEX:PAGE_SIZE]
     contents = [value.content_recommended for value in similar_contents]
     LOGGER.debug("similar_content for content_id :{} is {}".format(
         content_id, contents))
     return contents
 def defualt_content():
     default = RecommendationGenerator.default(
         content_seen_list,
         filter=filter,
         model=kwargs['model'].get('default'))
     contents = [recomm.content_recommended
                 for recomm in default][INDEX:PAGE_SIZE]
     return contents
 def get_recommendation(self, request, user_id, recommendation_bucket,
                        **kwargs):
     INDEX, PAGE_SIZE = self.index_count(request)
     filter = self.get_filter(request)
     models = kwargs.get('models') if kwargs.get('models') else {}
     """Get Recommendation For User Corrosponding to user_id and recommendation_bucket."""
     LOGGER.info(
         "fetch recommendation for user_id : %s , recommendation-bucket : %s ",
         str(user_id), str(recommendation_bucket))
     LOGGER.info("request params are :{}".format(self.personalization))
     # 1.get recommendation_id corrosponding to user_gender,user_language,recommendation_bucket.
     recomm_id = RecommendationGenerator.decide(user_id,
                                                recommendation_bucket)
     LOGGER.debug("recomm_id for user : %s  is %s", str(user_id),
                  str(recomm_id))
     # 2.get conteen_seen by user
     content_seen_list = ContentSeenViewSet.content_seen_list(
         user_id, recomm_id, content_type=kwargs.get(
             'content_type'))  # list of content seen having recommendation
     content_seen_list[
         1] = content_seen_list[1] if self.personalization.get(
             'remove_content_seen', True) else []
     LOGGER.debug("content seen by user :%s is :%s", str(user_id),
                  str(content_seen_list[1]))
     # 3.get recommendation excluding content seen by user
     recommendations = RecommendationGenerator.generate(
         recomm_id,
         content_seen_list,
         filter=filter,
         model=models.get('recomm_model'))
     # 4.remove recommendation having lower confidence
     recommendations = RecommendationGenerator.remove_duplicates(
         recommendations)
     # 5.if length of recommendation is less than 10 then add from coldstart
     if len(recommendations) < REC_MINLIMIT:
         LOGGER.debug(
             "len of recommendation is %d which is less than min Limit %s.",
             len(recommendations), REC_MINLIMIT)
         default = RecommendationGenerator.default(
             content_seen_list, filter=filter, model=models.get('default'))
         recommendations += default
         LOGGER.debug("coldstart %d recommendation  added.", len(default))
     recommendations = recommendations[INDEX:PAGE_SIZE]
     recommendations = [
         recomm.content_recommended for recomm in recommendations
     ]
     LOGGER.debug("recommendations is: {}".format(recommendations))
     return recommendations
 def content_by_circle(self, request, user_id, content_type=None):
     """get top content of user's circle."""
     INDEX, PAGE_SIZE = self.index_count(request)
     LOGGER.info("request params are :{}".format(self.personalization))
     LOGGER.info(
         "fetch top content by circle for user_id : %s and content_type: %s",
         user_id, content_type)
     if not content_type:
         content_type = RECOMMENDATION_ENGINE.get('CIRCLE_CONTENT_TYPE',
                                                  "movies")
     #get the user circle
     circle = UserViewSet.get_user_circle(user_id)
     circle = circle if circle else 'all'
     LOGGER.debug('content_type : %s , circle : %s ', content_type, circle)
     content_seen_list = [[], []]
     LIMIT = LIVE_LIMIT if content_type is "live" else REC_MAXLIMIT
     if content_type != "live":
         content_seen_list = ContentSeenViewSet.content_seen_list(user_id)
         content_seen_list[
             1] = content_seen_list[1] if self.personalization.get(
                 'remove_content_seen', True) else []
         LOGGER.debug('content_seen by user {}'.format(
             content_seen_list[1]))
     order_by = self.personalization.get('order_by', 'uu_played')
     contents = filter_by_key(
         ContentAll, circle=circle,
         content_type=content_type).order_by('-' + order_by).exclude(
             content_id__in=content_seen_list[1])[:LIMIT]
     contents = [value.content_id for value in contents if contents]
     if content_type != 'live' and len(contents) < REC_MINLIMIT:
         LOGGER.debug(
             "length of content: {} is less than mon limit:{} ".format(
                 len(contents), REC_MINLIMIT))
         default = RecommendationGenerator.default(
             content_seen_list)[:REC_MAXLIMIT]
         contents += [recomm.content_recommended for recomm in default]
     elif len(contents) < REC_MINLIMIT:
         contents = filter_by_key(
             ContentAll, circle='all',
             content_type=content_type).order_by('-' + order_by).exclude(
                 content_id__in=content_seen_list[1])[:LIMIT]
         contents = [value.content_id for value in contents if contents]
     contents = contents[INDEX:PAGE_SIZE]
     LOGGER.debug("circle contents are {}".format(contents))
     return contents
    def combined_recommendation(self, request, user_id, **kwargs):
        """
        :param request:
        :param user_id:
        :param kwargs:
        :return: combination of recommended tv show and movie
        """
        INDEX, PAGE_SIZE = self.index_count(request)

        LOGGER.info("request params are :".format(self.personalization))
        filter = self.get_filter(request)
        content_seen_list = ContentSeenViewSet.content_seen_list(user_id)
        recommendations, default = [], []
        for model in kwargs['models'].get("recomm_model"):
            recommendations.append(
                RecommendationGenerator.generate(1,
                                                 content_seen_list,
                                                 filter=filter,
                                                 model=model))
        #mixed recommendation
        recommendations = [(a, b) for a, b in zip(*recommendations)]
        recommendations = list(itertools.chain(*recommendations))
        recommendations = RecommendationGenerator.remove_duplicates(
            recommendations)
        if len(recommendations) < REC_MINLIMIT:
            for model in kwargs['models'].get('default'):
                default.append(
                    RecommendationGenerator.default(content_seen_list,
                                                    filter=filter,
                                                    model=model))
            default = [(a, b) for a, b in zip(*default)]
            default = list(itertools.chain(*default))
        recommendations += default
        shuffle(recommendations)
        recommendations = recommendations[INDEX:PAGE_SIZE]
        content_ids = [
            recomm.content_recommended for recomm in recommendations
        ]
        LOGGER.debug("mixed recommendation are :{}".format(content_ids))
        return content_ids
 def get_from_recommendation_all(self, request, user_id,
                                 recommendation_bucket):
     """get recommendation for bucket meva_recommended_movies"""
     INDEX, PAGE_SIZE = self.index_count(request)
     LOGGER.info("request params are :{}".format(self.personalization))
     content_type = self.personalization.get('recomm_type', 'movie')
     content_seen_list = ContentSeenViewSet.content_seen_list(user_id)
     content_seen_list[
         1] = content_seen_list[1] if self.personalization.get(
             'remove_content_seen', True) else []
     order_by = self.personalization.get('order_by', 'recomm_score')
     queryset = filter_by_key(
         RecommendationAll, user_id=user_id,
         content_type=content_type).order_by('-' + order_by).exclude(
             content_id__in=content_seen_list[1])[:REC_MAXLIMIT]
     content_id = [value.content_id
                   for value in list(queryset)] if queryset else []
     if len(queryset) < REC_MINLIMIT:
         default = RecommendationGenerator.default(content_seen_list)
         content_id += [value.content_recommended for value in default]
     content_ids = content_id[INDEX:PAGE_SIZE]
     return content_ids
Exemple #7
0
def test(request, **kwargs):
    body_unicode = request.body.decode('utf-8')
    body = json.loads(body_unicode)
    user_id = body.get('user_id') if body.get('user_id') else "None"
    recommendation_bucket = body.get("recommendation_bucket")
    content_id = body.get('content_id')
    content_id = content_id if content_id else "None"
    table1, table2 = {}, {}
    params = {}
    print(recommendation_bucket)
    params[
        'include_recency'] = True if 'recency' in recommendation_bucket else False
    params['include_mou'] = True if 'mou' in recommendation_bucket else False
    recommendation_bucket = "recommendation_by_recency"
    print(params)
    if recommendation_bucket != 'recommendation_by_recency':
        domain = "http://" + request.get_host() + "/"
        sub_url = "recommendation/v1/list/" + str(user_id) + "/" + str(
            recommendation_bucket) + "/" + str(content_id)
        url = domain + sub_url
        resp = requests.get(url)
        results = resp.json()
        results = results.get('results')
        if results:
            table2["heading"] = "Recommendation."
            table2["results"] = results
        content_seen = filter_by_key(
            ContentSeen, user_id=user_id,
            mou__gte=MOU).order_by('-date').values()[:CONTENT_SEEN_LIMIT]
        content_seen = ContentSeenSerializer(content_seen, many=True).data
        if content_seen:
            table1['heading'] = "Content Seen By user."
            table1['results'] = content_seen
        response = render(request, 'recommendation/recomm.html', {
            'table1': table1,
            "table2": table2
        })
        return response
    INDEX, PAGE_SIZE = 0, 100
    filter = {}
    recomm_id = RecommendationGenerator.decide(user_id, recommendation_bucket)
    content_seen_list = ContentSeenViewSet.content_seen_list(
        user_id, recomm_id)
    content_seen = content_seen_list[2]
    default = RecommendationGenerator.default(content_seen_list, filter=filter)
    contents = [recomm.content_recommended
                for recomm in default][INDEX:PAGE_SIZE]
    content_seen = list(
        map(
            lambda x: {
                'content_id': x.content_id,
                'date': x.date,
                'mou': float(x.mou)
            }, content_seen))
    recommendations = RecommendationGenerator.generate(recomm_id,
                                                       content_seen_list,
                                                       filter=filter)
    if not recommendations:
        table2['heading'] = "No Recommendation."
        response = render(request, 'recommendation/recomm.html', {
            'table1': table1,
            "table2": table2
        })
        return response
    recommendations = list(
        map(
            lambda recomm: {
                'content_seen': recomm.content_seen,
                'content_recommended': recomm.content_recommended,
                'confidence': float(recomm.confidence)
            }, recommendations))
    import pandas as pd
    try:
        df1 = pd.DataFrame(
            recommendations,
            columns=['content_seen', 'content_recommended', 'confidence'],
            dtype=object)
        df2 = pd.DataFrame(content_seen, dtype=object)
        df2['content_id'] = df2['content_id'].apply(str)
        df2['mou'] = df2['mou']**(1 / 3)
        df1['content_seen'] = df1['content_seen'].apply(str)
        df = pd.merge(df1,
                      df2,
                      how='left',
                      left_on='content_seen',
                      right_on='content_id')
        df = df.drop(columns='content_id')
        d1 = datetime.datetime.now().date()
        df['confidence'] = df.apply(
            lambda row: row['confidence'] * (
                (RCF / ((d1 - row['date'].date()).days + 1) if
                 (d1 - row['date'].date()).days < RCF else 1)),
            axis=1) if params['include_recency'] else df['confidence']
        df['confidence'] = df['confidence'] * df['mou'] if params[
            'include_mou'] else df['confidence']
        df = df[['content_seen', 'content_recommended', 'confidence']]
        df.sort_values(by=['confidence'],
                       ascending=False,
                       axis=0,
                       inplace=True)
        recommendations = list(
            map(lambda x: Recommendation(**x),
                df.to_dict('records', OrderedDict)))
        recommendations = RecommendationGenerator.remove_duplicates(
            recommendations)
        recommendations = recommendations[INDEX:PAGE_SIZE]
        if len(recommendations) < REC_MINLIMIT:
            LOGGER.debug("length is less than 10 adding default.")
            recommendations = default
    except Exception as e:
        LOGGER.exception(e)
        recommendations = default
    recomm = convert_to_dict(recommendations)
    import pandas as pd
    try:
        df1 = pd.DataFrame(recomm)
        content_ids = df1['content_recommended'].tolist()
        content_detail = filter_by_key(Content,
                                       content_id__in=content_ids).values()
        content_detail = list(content_detail) if content_detail else []
        df2 = pd.DataFrame(content_detail, dtype=object)
        content_ids = df1['content_seen'].tolist()
        content_detail = filter_by_key(Content,
                                       content_id__in=content_ids).values()
        content_detail = list(content_detail) if content_detail else []
        df3 = pd.DataFrame(content_detail, dtype=object)
        df1 = pd.merge(df1,
                       df2,
                       left_on='content_recommended',
                       right_on='content_id',
                       how='left')
        df1.rename(columns={
            'content_name': 'content_recommended_name',
            'language': 'content_recommened_lang'
        },
                   inplace=True)
        df1 = pd.merge(df1,
                       df3,
                       left_on='content_seen',
                       right_on='content_id',
                       how='left')
        df1.rename(columns={
            'content_name': 'content_seen_name',
            'language': 'content_seen_lang'
        },
                   inplace=True)
        df1.fillna('NA', inplace=True)
        columns = [
            'content_seen_name', 'content_seen_lang',
            'content_recommended_name', 'content_recommened_lang', 'confidence'
        ]
        df1 = df1[columns]
        df1.rename(inplace=True,
                   columns={
                       'content_seen_name': 'Content Watched',
                       'content_seen_lang': 'Content Watched Language',
                       'content_recommended_name': 'Content Recommended',
                       'content_recommened_lang':
                       'Content Recommended Language',
                       'confidence': ' Composite Score'
                   })
        #for table 1
        content_seen = content_seen_list[2]
        content_seen = list(
            map(
                lambda x: {
                    'user_id': x.user_id,
                    'content_id': x.content_id,
                    'date': x.date,
                    'mou': float(x.mou)
                }, content_seen))
        df2 = pd.DataFrame(content_seen, dtype=object)
        content_detail = filter_by_key(
            Content, content_id__in=df2['content_id'].tolist()).values()
        content_detail = list(content_detail) if content_detail else []
        df3 = pd.DataFrame(content_detail, dtype=object)
        df = pd.merge(df2,
                      df3,
                      left_on='content_id',
                      right_on='content_id',
                      how='left')
        df = df[['user_id', 'content_id', 'content_name', 'date', 'mou']]
        df.fillna('NA', inplace=True)
        content_seen = df.to_dict('records', OrderedDict)
        if content_seen:
            table1['heading'] = "Content Seen By user."
            table1['results'] = content_seen
    except Exception as e:
        LOGGER.exception(e)
    table2 = {
        'heading': "content_recommened",
        'results': df1.to_dict('records', OrderedDict)
    }
    response = render(request, 'recommendation/recomm.html', {
        'table1': table1,
        "table2": table2
    })
    return response