Example #1
0
    def TweetRank(self, unidentified_terms):
        ranked_tweets = defaultdict(list)

        # Create an instance of the indexer
        indexer = Indexer()
        # Index the tweets
        indexer.LoadIndexes()

        for term in unidentified_terms:
            # Get the tweets for the read term
            term_tweetids = indexer.GetTweetsForTerm(term)

            # The setup is that the number of retweets, favorites and author followers need to be normalized
            # Normalisation needs maximum and minimum of each of the tweet metrics
            # First loop finds the maximum and minimum value of all metrics. Let's declare them
            max_rt = 0  # will hold max value
            min_rt = 100000  # will hold min value
            max_fav = 0  # will hold max value
            min_fav = 100000  # will hold min value
            max_af = 0  # will hold max value
            min_af = 100000  # will hold min value

            for tweetid in term_tweetids:
                tweetid_rt = self.GetRetweetsForTweetid(indexer, tweetid)
                if tweetid_rt > max_rt:
                    max_rt = tweetid_rt
                if tweetid_rt < min_rt:
                    min_rt = tweetid_rt

                tweetid_fav = self.GetFavsForTweetid(indexer, tweetid)
                if tweetid_fav > max_fav:
                    max_fav = tweetid_fav
                if tweetid_fav < min_fav:
                    min_fav = tweetid_fav

                tweetid_af = self.GetFollowersForTweetid(indexer, tweetid)

                if tweetid_af > max_af:
                    max_af = tweetid_af
                if tweetid_af < min_af:
                    min_af = tweetid_af

            # Second loop uses the retrieved max and min of each metric to calculate a normalized score
            # of each tweet for that term
            for tweetid in term_tweetids:
                # For every tweet id get the number of retweets, favorites and author followers
                rt = self.GetRetweetsForTweetid(indexer, tweetid)
                fav = self.GetFavsForTweetid(indexer, tweetid)
                af = self.GetFollowersForTweetid(indexer, tweetid)

                tweet_term_score = self.GetScoreForTermTweetid(rt, max_rt, min_rt, fav, max_fav, min_fav,
                                                               af, max_af, min_af)

                ranked_tweets[term].append((tweetid, tweet_term_score))

        # Get the rankings, sorted descending on score and return only last x results
        self.sorted_cropped_rankings = self.SortCropRankings(ranked_tweets, unidentified_terms)

        self.rankings_output = list()
        for ut in unidentified_terms:
            number_rankings = len(self.sorted_cropped_rankings[ut])
            for i in range(0, number_rankings):
                self.rankings_output.append(self.sorted_cropped_rankings[ut][i])

        return self.rankings_output