【问题标题】:How do I extract tweets that mentions a specific word and/or phrase from the text?如何从文本中提取提及特定单词和/或短语的推文?
【发布时间】:2021-06-16 11:59:46
【问题描述】:

所以我想知道人们对肯德基、大力水手和 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

【问题讨论】:

  • if tweettext == keyword:更改为if keyword in tweettext:

标签: python json dictionary twitter text-extraction


【解决方案1】:

我需要更多的东西来测试,但如果你正在寻找打嗝,那是因为你搜索的 tweettext 等于单个单词。这就是它返回的原因。

您需要执行以下操作:

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

某种程度。

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

这将使使用关键字更容易。

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    相关资源
    最近更新 更多