Пример #1
0
    def handle(self, *args, **options):
 
        # Get the configuration parameters

        c = config.Config()
        d = c.cfg

        # Load the classifier

        f = open('bayesclass.pickle')
        classifier = pickle.load(f)
    
        t = login()


        KW = {'user' : d.get('api', 'user'), 
              'count' : d.get('api', 'count'), 
              'skip_users' : d.get('api', 'skip_users'), 
              'include_entities' : 'true',
              'since_id' : 1, 
              'id' : 2} 

        p = Tweet.objects.aggregate(Max('tweet_id'))

        latestId = p['tweet_id__max']
        if latestId == None:
           latestId = 1

        KW['since_id'] = int(latestId)

        api_call = getattr(t.statuses, 'user_timeline')
        tweets = makeTwitterRequest(t, api_call, **KW)

        print 'Fetched %i tweets' % len(tweets)

        a = len(tweets)           

        for i in range(a):

            txt = tweets[i]['text']
            ref = tweets[i]['id']
            src = tweets[i]['source']
            outc = int(classifier.classify(word_feats(txt)))
            created = mysql_date(tweets[i]['created_at'])

            q = Tweet( datetime = created, 
                       user = K['user'],
                       content = txt, 
                       source = src,
                       tweet_id = ref,
                       prop = outc )

            q.save()
        f.close()
Пример #2
0
    def handle(self, *args, **options):

        n = 10   # Number of training tweets
        SEARCH_TERM = ':)'
        
        MAX_PAGES = 1
        RESULTS_PER_PAGE = 1
        LANGUAGE = "en"
        INCLUDE_ENTITIES = "true"

        KW = {
            'domain': 'search.twitter.com',
            'count': 1000,
            'rpp': 100,
            'q': SEARCH_TERM,
            'lang': LANGUAGE,
            'include_entities': INCLUDE_ENTITIES 
            }

        t = twitter.Twitter(domain='search.twitter.com')

        posfeats = []

        for i in range(n):

               tweets = makeTwitterRequest(t, t.search, **KW)

               txt = tweets['results'][0]['text']
               itemb = extractwords(txt)
               posfeats.append((word_feats(itemb), '1'))

        classifier = NaiveBayesClassifier.train(posfeats)

        f = open('bayesclass.pickle', 'wb')
        pickle.dump(classifier, f)
        f.close()