Tweet感觉:总是返回相同的情感分数,不管标签是什么

2024-06-02 04:57:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用此库为加密货币生成情感分数:

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

它仍然给我同样的价值。在

我安装图书馆的时候确实注意到了一些奇怪的东西。在

根据说明:

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

这就是为什么它看起来不起作用吗?在


Tags: ofinhttpsgithubcom货币downloader分数
2条回答

下载数据集的时候,你看到的数据集和你的情绪没有关联。在

相同情绪得分的问题来自these lines

for s in sentiments:
    pass
return s

我怀疑这个未绑定的变量s记住了情绪得分的前一个值。在

但是,问题本身就是你在执行start()函数后打印出分数,这个函数启动了一个多线程程序,不断地从twitter上更新数据——你不应该期望情绪分数在你开始更新后马上到达。在

请注意,自述文件中的示例是从Python终端显示的,它们在执行start()函数之后等待Timer completed. Disconnecting now...消息出现。在

默认情况下,TweetFeel在当前目录中创建一个数据库。下一次启动该程序时,它将继续使用同一个数据库,并从它停止的地方恢复。我不知道tweetfeels如何处理你在上面更改关键字的问题,但是TweetFeel的这种行为可能是个问题。解决方案是为不同的关键字使用不同的数据库,然后将数据库的位置传递给tweetfeel构造函数。在

我对TweetFeel了解不多,只是听起来很有趣,所以我下载了这个项目,我有一个工作脚本,可以对我给它的任何关键字执行情感分析。我可以在这里添加一个脚本的副本,如果你仍然无法让tweetfelfeel工作。在


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

我目前有以下问题与脚本。在

1)我遇到了一些与您所得到的不同的错误,但是我能够通过用Github存储库中的最新代码替换pip中的tweetfeel库来解决这个问题。在

2)如果没有报告情绪值,有时TweetFeel无法完全停止,而不强制发送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

相关问题 更多 >