Esempio n. 1
0
 def test_is_english(self):
     """
     Test that the sentiment class can classify text as english.
     """
     sentimentanalysis = Sentimentanalysis()
     tweet = 'This is a dumb tweet'
     score = sentimentanalysis.evaluatetweet(tweet)
     self.assertTrue(score > 0.9)
Esempio n. 2
0
 def test_special_characters(self):
     """
     Test that sentiment class will not break on special characters.
     """
     sentimentanalysis = Sentimentanalysis()
     strange_tweet = 'ÅÚÍÎÏ””ŒER""øøø…weqrwqerqw wwcrqwe lchrjqekwchr http://asf.adfasdfæ…ø圜åÅ'
     sentimentanalysis.afinnsentiment(strange_tweet)
     sentimentanalysis.labmtsentiment(strange_tweet)
     # If we made it this far we passed the test
     self.assertTrue(True)
Esempio n. 3
0
def dump(method='afinn'):
    """
    Do sentiment analysis on all tweets for all companies and 
    write the results to a file.
    """
    sentimentanalysis = Sentimentanalysis()
    
    sent_method = None
    if method == 'afinn':
        sent_method = sentimentanalysis.afinnsentiment
    elif method == 'labmt':
        sent_method = sentimentanalysis.labmtsentiment      

    for stock_symbol, company in settings.STOCK_SYMBOL_MAPPINGS.items():
        print 'Company: ' + str(company)
        data = tweets.tweets(stock_symbol)
        sentiment = {}
        filename = filename_for_company(method, company)
        print 'Filename: ' + str(filename)
        try:
            sentiment = json.loads(open(filename).read())
        except:
            print 'No file data'

        for key in data:
            docs = data[key]
            first = True
            count = 0
            for doc in docs:
                count += 1
                print 'Iterating %d of %d' % (count, len(docs))
                tweetdate = datetime.datetime.fromtimestamp(doc['timestamp']).strftime('%Y-%m-%d %H:%M:%S').split(' ')[0]
                if first:
                    sentiment[tweetdate] = 0
                    first = False
                tweet = doc['tweet']
                isenglish = sentimentanalysis.evaluatetweet(tweet)
                if isenglish > 0.8:
                    sentval = sent_method(tweet)
                    if sentiment.get(tweetdate, 0) == 0:
                        sentiment[tweetdate] = sentval
                    else:
                        sentiment[tweetdate] = (sentiment.get(tweetdate, 0) + sentval)/2

        with io.open(filename, 'wb') as outfile:
            json.dump(sentiment, outfile)
        print 'Saved file: ' + filename