【问题标题】:changing the color of an ellipse using hashtag data using Twitter4j 3.0.3使用 Twitter4j 3.0.3 使用标签数据更改椭圆的颜色
【发布时间】:2016-06-11 23:56:40
【问题描述】:

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

我希望该主题标签数据将简单椭圆的颜色修改为介于红色和绿色之间的颜色,具体取决于 #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

任何帮助将不胜感激。甚至为我指明了如何自己编写代码的方向。如果您需要我提供更多信息,请尽快回复!

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

第二版:

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

}

【问题讨论】:

    标签: java twitter colors processing twitter4j


    【解决方案1】:

    你必须把你的问题分解成更小的步骤。

    第 1 步:创建一个简单地返回推文 ArrayList 的函数。

    第 2 步: 创建一个函数,该函数接受 ArrayListString 值,并返回 StringArrayList 的推文中出现的次数。

    此代码假设您有一个ArrayList&lt;String&gt; tweets

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

    第 3 步:根据包含每个单词的推文数量计算颜色。你说你总是有 100 条推文,所以你可以把推文数除以 100,然后乘以 255 得到颜色值。

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

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

    【讨论】:

    • 非常感谢您的快速回复。它似乎有效,因为它目前是我这边的绿色背景。一个问题,你说“你实际上会从 twitter 上得到这些,但为了测试,我们自己填一下。”我不确定我是否完全理解。再次感谢您与我合作,对不起,我是菜鸟!
    • @zack.gray.ou 没问题。有了这个评论,我的意思是在你的真实程序中,你连接到 Twitter 以获取推文,但对于我的小例子,我手动填充 htem 与 "#good""#bad",因为你并没有真正询问问题的推特部分。
    • 哦,我现在明白了!然而,我确实遇到了另一个问题。我假设我的代码正常工作,但我运行了一个 println,它似乎实际上并没有收集主题标签数据。我可能是错的,但看起来它只是在任何给定时间重复运行 50 个“#good”和 50 个“#bad”......我将新代码添加到上面“第二版”下的主要问题中
    • @zack.gray.ou 你不应该使用我在setup() 函数中的代码。那只是为了测试目的。这就是我的评论的意思。你应该用推文填充你的ArrayLists,而不是硬编码Strings。我只是使用硬编码的Strings 来演示用于迭代它们以计算好坏数量的代码。你的真实代码应该使用来自 twitter 的数据,而不是硬编码的 Strings
    • 啊。我希望我可以由我的教授来管理这一切,但他无法回复我。我现在对这一切都很困惑。我以为我知道我在做什么,但我想我没有我想象的那么好。好的,所以在第一步中,您的原始答案基本上就是我在问题的第一个代码中所拥有的?我很抱歉这么难。我只是迷路了。我的教授原本应该指导我完成这个,因为我是处理和编程的新手,但正如我提到的那样,他在这一点上根本没有帮助......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多