python - 推文感觉 : Always returns the same Sentiment Score, 无论标签如何

标签 python twitter sentiment-analysis

我正在尝试使用这个库来生成加密货币的情绪评分:

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

当我使用示例 trump 中的代码时,它返回情绪分数 -0.00082536637608123106 .

我已将标签更改为以下内容:

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

它仍然给我相同的值(value)。

当我安装这个库时,我确实注意到了一些奇怪的事情。

根据说明:

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

python3 -m nltk.downloader vader_lexicon

当我运行这个时,我得到:

/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))

这可能是它不起作用的原因吗?

最佳答案

默认情况下,tweetfeels 在您的当前目录中创建一个数据库。下次启动程序时,它将继续使用相同的数据库,并从上次中断的地方继续。我不知道 tweetfeels 会做什么来处理您更改其上的关键字,但 tweetfeels 的这种行为可能是一个问题。解决方案是对不同的关键字使用不同的数据库,然后将数据库的位置传递给 TweetFeels 构造函数。

我对 Tweetfeels 不太了解,它只是听起来很有趣,所以我下载了该项目,并且我有一个工作脚本,可以对我提供的任何关键字执行情感分析。如果您在使用 TweetFeels 时仍然遇到问题,我可以在此处添加脚本的副本。


编辑:这里是我正在使用的脚本

我目前在使用脚本时遇到以下问题。

1) 我遇到了一些与您遇到的错误不同的错误,但我能够通过将 pip 中的 tweetfeels 库替换为 Github 存储库中的最新代码来解决该问题。

2) 如果未报告情绪值,有时 tweetfeels 无法完全停止,而无需强制发送 ctrl+c 键盘中断。

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

关于python - 推文感觉 : Always returns the same Sentiment Score, 无论标签如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48030920/

相关文章:

python - 如何使用 tqdm 实现 JSON 文件加载进度条?

python - Pygame 创建无限数量的敌人

Python 多处理池作为装饰器

python - 如何将字符串值传递给情感分析 RNN 序列模型并获取预测

python - 如何对 Python 程序进行版本控制

php - 缓存从 API 提取的信息的最有效方法是什么?

javascript - Facebook 和 Twitter 通过 iframe 共享

javascript - Facebook、Twitter、LinkedIn 共享链接数

python - 预期 conv1d_1_input 具有形状 (15, 512),但得到的数组具有形状 (4, 512)

machine-learning - 文档向量中的哪些“信息”使情感预测起作用?