使用Twitter4j 3.0.3使用主题标签数据更改椭圆的颜色 [英] changing the color of an ellipse using hashtag data using Twitter4j 3.0.3

查看:165
本文介绍了使用Twitter4j 3.0.3使用主题标签数据更改椭圆的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我真的不知道写代码,但。我正在开发一个使用twitter API数据的项目。我的目标是使用哈希标签来表示好的东西(为了简单起见,让我们使用#good和#bad)。



我想要主题标签数据将一个简单椭圆的颜色修改为红色和绿色之间的颜色阴影,具体取决于#good和#bad tweets的数量。
我喜欢把它当作一个+ 100 / -100光谱。每个#good tweet是+1,每个#bad是-1。如果它在-100 tweets,那么椭圆是全红色。如果它是在+100 tweets,那么椭圆是全绿色。



我知道这是一个有点复杂,但它的一个艺术项目。我按照一个教程,目前有Twitter数据在一个简单的数组列表的tweets响应(教程@ https://www.youtube.com/watch?v=gwS6irtGK-c
我正在使用处理,java,twitter4j 3.0.3和一个macbook pro与OSX el capitan 10.11.3



任何帮助将非常感谢。甚至指示我在如何自己编码的方向。如果您需要我的更多信息,请尽快响应我的反应。

  ConfigurationBuilder cb = new ConfigurationBuilder 
Twitter twitterInstance;
查询queryForTwitter;

ArrayList tweets;

void setup(){
cb.setOAuthConsumerKey(****);
cb.setOAuthConsumerSecret(****);
cb.setOAuthAccessToken(****);
cb.setOAuthAccessTokenSecret(****);
cb.setUseSSL(true);
twitterInstance = new TwitterFactory(cb.build()
).getInstance();
queryForTwitter = new Query(#good);

size(640,440);
FetchTweets();

} // setup

void draw(){
background(0);
DrawTweets();
} // draw

void DrawTweets(){
for(int i = 0; i< tweets.size(); i ++){
Status t = (状态)tweets.get(i);
String user = t.getUser()。getName();
String msg = t.getText();
text(user +:+ msg,
20,15 + i * 30-mouseY,width-20,40)
} // for
} // drawTweets

void FetchTweets(){
try {
QueryResult result = twitterInstance.search(
queryForTwitter);
tweets =(ArrayList)result.getTweets();
} catch(TwitterException te){
println(Could not connect:+ te);
} // end of catch TwitterException
} // FetchAndDrawTweets()结束

第二版:

  ConfigurationBuilder cb = new ConfigurationBuilder 
Twitter twitterInstance;
查询queryForTwitter;

// ArrayList tweets;

void setup(){
cb.setOAuthConsumerKey(****);
cb.setOAuthConsumerSecret(****);
cb.setOAuthAccessToken(****);
cb.setOAuthAccessTokenSecret(****);
cb.setUseSSL(true);
// twitterInstance = new TwitterFactory(cb.build()
//).getInstance();
// queryForTwitter = new Query(#feelthebern);

size(640,440);

int numGood = 50;
int numBad = 50;
for(int i = 0; i tweets.add(#good);
}
for(int i = 0; i tweets.add(#bad);
}

} // setup

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



//创建一个计算tweets的函数
//包含某个标签标签
int countTweets(String hashtag){
int total = 0;
for(String tweet:tweets){
if(tweet.contains(hashtag)){
total ++;
}
}
return total;
}

void draw(){

//计算好和坏的tweets
int goodTweets = countTweets(#good);
int badTweets = countTweets(#bad);

//根据tweet计数计算颜色
float r = badTweets / 100.0 * 255;
float g = goodTweets / 100.0 * 255;
float b = 0;

background(r,g,b);

}


解决方案

第一步:创建一个只返回一个 ArrayList 的函数。 code> of tweets。



第2步:创建一个函数,使 ArrayList String 值,并返回 String 在< c $ c> ArrayList



此代码假设您有一个 ArrayList< String& tweets

  int countTweets(String hashtag){
int total = 0;
for(String tweet:tweets){
if(tweet.contains(hashtag)){
total ++;
}
}
return total;
}

第3步:包含每个字的tweet数。你说你总是有100条tweets,所以你可以把tweet计数除以100,然后乘以255得到颜色值。



把它们放在一起,它看起来像这样:

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

void setup(){

//你实际上可以从twitter,
//得到这些,但是为了测试,我们只是自己填写
int numGood = 50;
int numBad = 50;
for(int i = 0; i tweets.add(#good);
}
for(int i = 0; i tweets.add(#bad);
}
}

//创建一个计算tweets的函数
//包含某个hashtag
int countTweets(String hashtag){
int total = 0;
for(String tweet:tweets){
if(tweet.contains(hashtag)){
total ++;
}
}
return total;
}

void draw(){

//计算好和坏的tweets
int goodTweets = countTweets(#good);
int badTweets = countTweets(#bad);

//根据tweet计数计算颜色
float r = badTweets / 100.0 * 255;
float g = goodTweets / 100.0 * 255;
float b = 0;

background(r,g,b);

}


Okay, so i dont really know much about writing code, yet. I am working on a project that uses twitter API data. My goal for the project is to use hash tags to represent both good and bad things (for sake of simplicity, lets use #good and #bad).

I want that hashtag data to modify the color of a simple ellipse to a shade of color in between red and green, depending on the number of #good and #bad tweets. I like to think of it as a +100/-100 spectrum. each #good tweet is +1, each #bad is -1. If it is at -100 tweets, then the ellipse is full red. If it is at +100 tweets, then the ellipse is full green.

I know this is a little complicated, but its for an art project im doing. I followed a tutorial and currently have the twitter data responding on a simple array list of tweets (tutorial @ https://www.youtube.com/watch?v=gwS6irtGK-c) I am using processing, java, twitter4j 3.0.3, and a macbook pro with OSX el capitan 10.11.3

Any help would be greatly appreciated. Even pointing me in the direction on how to code it myself. If you need more information from me, ill respond as quickly as I see it!

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

ArrayList tweets;

void setup() {
  cb.setOAuthConsumerKey("****");
  cb.setOAuthConsumerSecret("****");
  cb.setOAuthAccessToken("****");
  cb.setOAuthAccessTokenSecret("****");
  cb.setUseSSL(true);
  twitterInstance = new TwitterFactory( cb.build()
                                  ).getInstance();
  queryForTwitter = new Query("#good");

  size(640,440);
  FetchTweets();

} //setup

void draw() {
  background(0);
  DrawTweets();
} //draw

void DrawTweets() {
  for(int i=0; i<tweets.size(); i++) {
    Status t = (Status) tweets.get(i);
    String user = t.getUser().getName();
    String msg = t.getText();
    text(user + ": " + msg,
         20,15+i*30-mouseY, width-20, 40);
  } //for
} //drawTweets

void FetchTweets(){
  try {
    QueryResult result = twitterInstance.search(
                                queryForTwitter );
    tweets = (ArrayList) result.getTweets();
  } catch(TwitterException te) {
    println("Couldn't connect: " +te);
  } // end of catch TwitterException
}// end of FetchAndDrawTweets()

SECOND VERSION:

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

//ArrayList tweets;

void setup() {
  cb.setOAuthConsumerKey("****");
  cb.setOAuthConsumerSecret("****");
  cb.setOAuthAccessToken("****");
  cb.setOAuthAccessTokenSecret("****");
  cb.setUseSSL(true);
  //twitterInstance = new TwitterFactory( cb.build()
  //                                ).getInstance();
  //queryForTwitter = new Query("#feelthebern");

  size(640,440);

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

} //setup

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;
}

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);

}

解决方案

You have to break your problem down into smaller steps.

Step 1: Create a function that simply returns an ArrayList of tweets.

Step 2: Create a function that takes that ArrayList and a String value, and returns the number of times that String occurs in the tweets in the ArrayList.

This code assumes you have a ArrayList<String> tweets:

int countTweets(String hashtag){
  int total = 0;
  for(String tweet : tweets){
    if(tweet.contains(hashtag)){
      total++;
    }
  }
  return total;
}

Step 3: Calculate the color based on the number of tweets containing each word. You said you'll always have 100 tweets, so you can just divide the tweet count by 100, then multiply by 255 to get the color value.

Putting it all together, it looks like this:

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

void setup() {

  //you would actually get these from twitter,
  //but for testing let's just fill them ourselves
  int numGood = 50;
  int numBad = 50;
  for (int i = 0; i < numGood; i++) {
    tweets.add("#good");
  }
  for (int i = 0; i < numBad; i++) {
    tweets.add("#bad");
  }
}

//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);

}

这篇关于使用Twitter4j 3.0.3使用主题标签数据更改椭圆的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆