Example #1
0
    def get_twitter_topics(self):
        """
        Loads Tweepy api, get users' friends and info and resets profile ratings
        """

        id2word = {v: k for k, v in list_dict.token2id.items()}

        self.update("twitter_topics",  {topics.index(t): 0 for t in topics})
        self.update("explicit_topics",  {topics.index(t): 0 for t in topics})

        my_votes = Counter()

        api = self.init_auth()

        self.get_twitter_info(api)

        for member in self.get_twitter_list(api):
            try:
                all_topics = sorted(corp_50[expert_map_50[member]], key=operator.itemgetter(1), reverse=True)
                t = 5
                for k, v in all_topics:
                    for topic in topics:
                        if id2word[k].startswith(topic):
                            my_votes.update({topics.index(topic): t})
                            t -= 1
                            continue

                        if t < 0:
                            break
            except KeyError:
                try:
                    all_topics = sorted(corp_10[expert_map_10[member]], key=operator.itemgetter(1), reverse=True)
                    t = 5
                    for k, v in all_topics:
                        for topic in topics:
                            if id2word[k].startswith(topic):
                                my_votes.update({topics.index(topic): t})
                                t -= 1
                                continue

                            if t < 0:
                                break
                except KeyError:
                    continue

        range1 = float(max(my_votes.values()) - min(my_votes.values()))

        normalised_votes = {}

        for k, v in my_votes.items():
            a = 100.0 * (float(v) - float(min(my_votes.values()))) / float(range1)
            normalised_votes[k] = round(a)

        self.update("twitter_topics", normalised_votes)
        self.update("explicit_topics", normalised_votes)
Example #2
0
def user_registered_callback(sender, user, request, **kwargs):
    """
    :param user: user_auth model object
    Callback to create profile when creating a user auth object
    """
    profile = UserProfileRec(user=user)

    """
    initialise profile ratings
    """
    profile_topics = {"twitter_topics": {topics.index(t): 0 for t in topics},
                      "explicit_topics": {topics.index(t): 0 for t in topics},
                      "clicked_topics": Counter({topics.index(t): 0 for t in topics}),
                      "shown_topics": Counter({topics.index(t): 0 for t in topics})}

    profile.profile_topics = base64.b64encode(pickle.dumps(profile_topics))

    profile.save()