2016-02-29 78 views
0

好吧,所以我对编写代码还真不知道多少。我正在研究使用twitter API数据的项目。我的项目目标是使用散列标签来表示好的和坏的东西(为了简单起见,我们使用#good和#bad)。使用Twitter4j使用hashtag数据改变椭圆的颜色3.0.3

我希望该标签数据可以将简单椭圆的颜色修改为红色和绿色之间的颜色阴影,具体取决于#good和#bad推文的数量。 我喜欢把它想象成一个+ 100/-100的光谱。每个#好消息是+1,每#消息是-1。如果它在-100推文,那么椭圆是全红。如果它是在+100推文,那么椭圆是全绿色的。

我知道这有点复杂,但它对于一个艺术项目即将完成。我跟着一个教程,目前拥有 我使用的处理,JAVA,twitter4j 3.0.3 Twitter的数据响应微博的简单数组列表(教程@https://www.youtube.com/watch?v=gwS6irtGK-c),并与OSX埃尔卡皮坦一个的MacBook Pro 10.11.3

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

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

} 

回答

0

你必须下来打破你的问题分成更小的步骤。

第1步:创建一个函数,只需返回一个推文ArrayList

步骤2:创建一个函数,该ArrayListString值,并返回该String发生在ArrayList的鸣叫的次数。

此代码假定你有ArrayList<String> 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); 

} 
+0

非常感谢你的快速反应。它似乎工作,因为它是我目前的绿色背景。有一个问题,你说:“你实际上会从Twitter获得这些信息,但是对于测试,我们可以自己填充它们。” 林不知道我完全理解。再次感谢与我合作,对不起,我是一个noobie! –

+0

@ zack.gray.ou没问题。有了这个评论,我的意思是说,在你的真实程序中,你是连接到twitter来获取推文,但只是为了我的小例子,我手动填写“#good”或者“#bad ''“,因为你并没有真正询问问题的推特部分。 –

+0

哦,我明白了! 但是,我遇到了另一个问题。我认为我的代码正常工作,但我运行了一个println,它似乎并没有实际收集hashtag数据。我可能是错的,但它看起来像它在任何给定的时间只是反复运行50“#良好”和50“#bad”... 我在“第二个版本” –