【问题标题】:populating an arraylist in twitter4j在 twitter4j 中填充数组列表
【发布时间】:2016-03-10 08:18:39
【问题描述】:

我需要使用来自在线的推文而不是硬编码的字符串来填充我的数组列表。我正在使用 twitter4j 3.0.3。

我对这个项目感到很困惑。既然是上学,我真的需要弄清楚。我是初学者程序员,我很困惑。

   int numGood = 50;
   int numBad = 50;
  for (int i = 0; i < numGood; i++) {
    tweets.add("test");
  }
  for (int i = 0; i < numBad; i++) {
    tweets.add("#bad");
  }

}

ArrayList<String> tweets = new ArrayList<String>();



//create a function that counts the tweets
//that contain a certain hashtag
int countTweets(String hashtag){
  int total = 0;
  for(String tweet : tweets){
    if(tweet.contains(hashtag)){
      total++;
    }
  }
  return total;
}

完整版:

ConfigurationBuilder cb = new ConfigurationBuilder();
Twitter twitterInstance;
Query queryForTwitter;

//ArrayList tweets;

void setup() {
  cb.setOAuthConsumerKey("xxxx");
  cb.setOAuthConsumerSecret("xxxx");
  cb.setOAuthAccessToken("xxxx");
  cb.setOAuthAccessTokenSecret("xxxx");
  cb.setUseSSL(true);

  size(640,440);

} //setup

//ArrayList<String> tweets = new ArrayList<String>();

public static void main (String args[]) throws TwitterException {
    Twitter twitter = new TwitterFactory().getInstance();
    List<Status> statuses = twitter.getUserTimeline("google");
    String hashtag = "#AlphaGo";
    System.out.println("The Twitter page contains " 
                        + countTweets(hashtag, statuses) 
                        + " tweets with the hashtag : " + hashtag);

}

public static int countTweets(String hashtag, List<Status> statuses){
    return (int) statuses.stream()
                         .filter(x -> x.getText().contains(hashtag))
                         .count();
}

//create a function that counts the tweets
//that contain a certain hashtag
int countTweets(String hashtag){
  int total = 0;
  for(String tweet : tweets){
    if(tweet.contains(hashtag)){
      total++;
    }
  }
  return total;
}

void draw(){

  //count the good and bad tweets
  int goodTweets = countTweets("#good");
  int badTweets = countTweets("#bad");

  //calculate color based on tweet counts
  float r = badTweets/100.0 * 255;
  float g = goodTweets/100.0 * 255;
  float b = 0;

  background(r, g, b);

}

编辑:我希望该主题标签数据将背景颜色修改为介于红色和绿色之间的颜色,具体取决于#good 和 #bad 推文的数量。我喜欢将其视为 +100/-100 频谱。每条#good 推文都是 +1,每条 #bad 都是 -1。如果是 -100 条推文,则椭圆是全红色的。如果是 +100 条推文,则椭圆为全绿色。

我知道这有点复杂,但它适用于我正在做的一个艺术项目。我遵循了一个教程,目前有推特数据响应一个简单的推文数组列表(教程@@https://www.youtube.com/watch?v=gwS6irtGK-c)我正在使用处理、java、twitter4j 3.0.3 和带有 OSX el capitan 10.11.3 的 macbook pro

【问题讨论】:

  • 你忘了问问题。
  • @JBNizet 我提供的代码是硬编码字符串,我的问题是如何让代码从 twitter 中提取标签数据。

标签: java twitter arraylist twitter4j


【解决方案1】:

在这里,使用 的简单实现

public static void main (String args[]) throws TwitterException {
    Twitter twitter = new TwitterFactory().getInstance();
    List<Status> statuses = twitter.getUserTimeline("google");
    String hashtag = "#AlphaGo";
    System.out.println("The Twitter page contains " 
                        + countTweets(hashtag, statuses) 
                        + " tweets with the hashtag : " + hashtag);

}

public static int countTweets(String hashtag, List<Status> statuses){
    return (int) statuses.stream()
                         .filter(x -> x.getText().contains(hashtag))
                         .count();
}

【讨论】:

  • 感谢您的回复!但是,我在集成它时遇到了问题。我取出了旧的硬编码字符串并添加了您的代码。我在“完整版本”下的原始帖子中包含了我的整个代码,并在最底部添加了我的意图。希望这能让事情更清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 2011-02-02
  • 2014-08-11
  • 1970-01-01
  • 2010-10-26
  • 1970-01-01
  • 2019-07-28
相关资源
最近更新 更多