如何从文本中提取提及特定单词和/或短语的推文?

2024-04-23 11:56:49 发布

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

所以我想知道人们对肯德基、大力水手和ChickfilA鸡肉三明治的看法。注意:我已经有了我需要的所有Twitter数据

我成功地提取了用户和他们的用户名,但还没有弄清楚如何更进一步,弄清楚谁在推文中提到了“三明治”

我很确定这段代码所做的是提取所有推特完全是“三明治”的用户。。。我不知道如何提取刚刚提到三明治的推文。我已经研究并认为我可以用re.findall()或Tweepy库来完成这个任务?有人能告诉我到底需要做什么吗

以下是我迄今为止所尝试的:

uniqueusers = {}

keyword = 'sandwich'

for tweetzipfile in tweetzipfiles:
  zf = zipfile.ZipFile(tweetzipfile)
  for i, obj in enumerate(zf.infolist()):
    tweetjson = json.load(zf.open(obj))
    userwhotweeted = tweetjson['user']['screen_name']
    tweettext = tweetjson['text']
    if tweettext == keyword:
      if userwhotweeted in uniqueusers:
        uniqueusers[userwhotweeted] += 1
      if userwhotweeted not in uniqueusers:
        uniqueusers[userwhotweeted] = 1

Tags: 用户inobjforifkeyword水手zf
1条回答
网友
1楼 · 发布于 2024-04-23 11:56:49

我需要更多的东西来测试,但如果你在寻找这个问题,那是因为你被搜索到的tweettext等于一个单词。这就是为什么它的回报如此之高

您需要执行以下操作:

    if keyword in tweettext:
      if userwhotweeted in uniqueusers:
        uniqueusers[userwhotweeted] += 1
      elif userwhotweeted not in uniqueusers:
        uniqueusers[userwhotweeted] = 1
    else:
      print("No Results")

某种程度上

如果您想将tweet中的文本块转换为列表中的单个项目,还可以使用.split()的变体

这将使使用关键字变得更容易

相关问题 更多 >