Search code examples
pythontwittersentiment-analysis

Tweet Feels: Always returns the same Sentiment Score, regardless tags


I am trying to use this library to generate sentiment score for cryptocurrencies:

https://github.com/uclatommy/tweetfeels/blob/master/README.md

When I use the code from the example trump, it returns a sentiment score of -0.00082536637608123106.

I have changed the tags to the following:

btc_feels = TweetFeels(login, tracking=['bitcoin'])
btc_feels.start(20)
btc_feels.sentiment.value

and it still gives me the same value.

I did notice something strange when I installed the library.

from the instructions:

If for some reason pip did not install the vader lexicon:

python3 -m nltk.downloader vader_lexicon

When I ran this, I got:

/anaconda/lib/python3.6/runpy.py:125: RuntimeWarning: 'nltk.downloader' found in sys.modules after import of package 'nltk', but prior to execution of 'nltk.downloader'; this may result in unpredictable behaviour warn(RuntimeWarning(msg))

Could this be why it appears not to be working?


Solution

  • By default, tweetfeels creates a database in your current directory. The next time you start the program, it will continue using the same database, and pick up where it left off. I don't know what tweetfeels does to handle you changing the keyword on it, but this behaviour of tweetfeels could be a problem. The solution would be to use a different database for different keywords, and then pass in the location of your database to the TweetFeels constructor.

    I don't know that much about Tweetfeels, it just sounded interesting, so I've downloaded the project, and I have a working script that will perform the sentiment analysis on any keyword I give it. I can add a copy of the script here, if you're still having problems getting TweetFeels to work.


    Edit: here the script I am using

    I am currently having the following problems with the script.

    1) I was getting some error that was different from the one you'd got, but I was able to fix the issue by replacing the tweetfeels library from pip with the latest code in their Github repository.

    2) If a sentiment value does not get reported, sometimes tweetfeels fails to come to a complete stop, without forcefully sending a ctrl+c keyboard interrupt.

    import os, sys, time
    from threading import Thread
    from pathlib import Path
    
    from tweetfeels import TweetFeels
    
    consumer_key = 'em...'
    consumer_secret = 'aF...'
    access_token = '25...'
    access_token_secret = 'd3...'
    login = [consumer_key, consumer_secret, access_token, access_token_secret]
    
    try:
        kw = sys.argv[1]
    except IndexError:
        kw = "iota"
    
    try:
        secs = int(sys.argv[2])
    except IndexError:
        secs = 15
    
    for arg in sys.argv:
        if (arg == "-h" or arg == "--help"):
            print("Gets sentiment from twitter.\n"
                  "Pass in a search term, and how frequently you would like the sentiment recalculated (defaults to 15 seconds).\n"
                  "The keyword can be a comma seperated list of keywords to look at.")
            sys.exit(0)
    
    db = Path(f"~/tweetfeels/{kw}.sqlite").expanduser()
    if db.exists():
        print("existing db detected. Continueing from where the last sentiment stream left off")
    else:
        #ensure the parent folder exists, the db will be created inside of this folder
        Path(f"~/tweetfeels").expanduser().mkdir(exist_ok=True)
    
    feels = TweetFeels(login, tracking=kw.split(","), db=str(db))
    
    go_on = True
    def print_feels(feels, seconds):
        while go_on:
            if feels.sentiment:
                print(f"{feels.sentiment.volume} tweets analyzed from {feels.sentiment.start} to {feels.sentiment.end}")
                print(f'[{time.ctime()}] Sentiment Score: {feels.sentiment.value}')
                print(flush=True)
            else:
                print(f"The datastream has not reported a sentiment value.")
                print(f"It takes a little bit for the first tweets to be analyzed (max of {feels._stream.retry_time_cap + seconds} seconds).")
                print("If this problem persists, there may not be anyone tweeting about the keyword(s) you used")
                print(flush=True)
            time.sleep(seconds)
    
    
    t = Thread(target=print_feels, kwargs={"feels":feels,"seconds":secs}, daemon=True)
    print(f'Twitter posts containing the keyword(s) "{kw}" will be streamed, and a new sentiment value will be recalculated every {secs} seconds')
    feels.start()
    time.sleep(5)
    t.start()
    
    try:
        input("Push enter at any time to stop the feed...\n\n")
    except (Exception, KeyboardInterrupt) as e:
        feels.stop()
        raise e
    
    feels.stop()
    go_on = False
    print(f"Stopping feed. It may take up to {feels._stream.retry_time_cap} for the feed to shut down.\n")
    #we're waiting on the feels thread to stop