我如何通过使用计数器方法打印出仅以英文显示的推文数量?

问题描述 投票:0回答:1

我正在使用Twitter api,在其中搜索包含关键字“ Lebron”的推文。我正在尝试编写一个循环,该循环计数仅英语形式的推文的数量,并包含关键字“ Lebron”。我正在使用Atom作为文本编辑器在ruby中编写此代码。

p.s-我已经可以访问Twitter,只是不显示完整的密钥

require 'twitter'

client = Twitter::REST::Client.new do |config|
  config.consumer_key        = "8t4G...aa6"
  config.consumer_secret     = "tmq...bFA"
  config.access_token        = "106...OwI"
  config.access_token_secret = "LDh...VnB"
end

def tweet_count
  tweet_count.count do |e|
  e == lang: "en"
  end

client.search("Lebron -rt", result_type: "recent").take(2).each do |tweet|
  puts "#{tweet.user.screen_name}: #{tweet.text}" #tweet.text
end
ruby rest api twitter rubygems
1个回答
1
投票

documentation for the Twitter Ruby gem指出您可以使用:lang选项将搜索结果限制为任何ISO-639-1 language id

tweet_count = client.search("Lebron", lang: "en", result_type: "recent").count
puts "Found #{tweet_count} tweets in English"
© www.soinside.com 2019 - 2024. All rights reserved.