示例#1
0
def sentiments(stock_symbol, method='afinn', start=None, end=None):
    """
    Return tweet sentiments for a stock symbol between two epoch timestamps.
    """
    company_name = settings.STOCK_SYMBOL_MAPPINGS[stock_symbol]
    try:
        company_file = open(file_handler.filename_for_company(method, company_name))
    except:
        return {}
    data = json.loads(company_file.read())

    # if start or end:
    data = _filter_on_date(data, company_name, start, end)

    return data
示例#2
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