Ejemplo n.º 1
0
    def _populate(self, tweets):
        """
        :param tweets: A python dictionary containing trends as keys and list of tweets as
        values against each trend.
        :return: None

        This is a private method used by the constructor to populate the inverted index object
        """
        for trendName in tweets:
            self.trends.append(trendName)
            self.totalTweets += len(tweets[trendName])

            # classify trend
            tweetsDoc = " ".join([tweet.text for tweet in tweets[trendName]])
            model = NaiveBayes()
            model.loadModelFromDB()
            self.categories.append(model.classify(tweetsDoc))

            for tweet in tweets[trendName]:
                if tweet.user.screen_name not in self.twitterHandles:
                    self.twitterHandles.append(tweet.user.screen_name)
                    posts = [(self.trends.index(trendName), tweet)]
                    self.indexLists.append(posts)
                else:
                    posts = self.indexLists[self.twitterHandles.index(
                        tweet.user.screen_name)]
                    posts.append((self.trends.index(trendName), tweet))
        self.logger.debug(
            'Created and populated Inverted Index: Trends-{}, Tweets-{}'.
            format(len(self.trends), self.totalTweets))