我正在创建一个应用程序,从Twitter的API中返回100条推文,然后对所有100条推文进行情绪分析。当用户键入感兴趣的查询并且浏览器向' / get_tweets'提出请求时,将调用以下方法。
def get_tweets
tweets = $twitter.search(query_params, count: 100).attrs[:statuses]
@tweets = []
datumbox = Datumbox.create('2ef48d67598553804617c9862dfcaf8f')
tweets.each do |t|
tweet = Tweet.new
tweet.twitter_id = t[:id_str]
tweet.text = t[:text]
tweet.user_id = t[:user][:id_str]
tweet.name = t[:user][:name]
tweet.screen_name = t[:user][:screen_name]
tweet.location = t[:user][:location]
tweet.profile_image_url = t[:user][:profile_image_url_https]
tweet.tweet_created_at = t[:created_at]
# Need to remove @ symbols to use datumbox API analysis
sentiment_text = tweet.text.gsub(/@/,'')
tweet.sentiment = datumbox.twitter_sentiment_analysis(text: sentiment_text)
@tweets << tweet
end
render :index
end
问题是这非常慢。一切都是当用户输入他/她感兴趣的查询时,此方法执行101个API调用。
加快这一过程的最佳方法是什么?在构建需要制作大型应用程序的应用程序时,是否需要考虑设计方法? API调用次数?最后,我想以标签格式向用户显示推文,他们可以在正面,中性和负面推文之间切换。我是否应该考虑批量调用情绪分析API?
建议非常感谢!
答案 0 :(得分:0)
可能的方法: