changing the color of an ellipse using hashtag dat

2019-09-03 19:44发布

问题:

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

}

回答1:

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

}