help needed with multiple keywords in a string filtering twitter feeds
in
Contributed Library Questions
•
7 months ago
Hi There,
after a day of trying, and applying ideas within the forum I really could do with some help. I believe it's simple ( but not for me...)
I'm trying to create a simple api showing 'two moods' - good and bad- based on twitter feeds. ( this as a starting point to something more complex) One stream is based on positive words in tweets, one on negative.
I can make it work, based on a single search term, but when I add several words in the search string there is no output, while I expect an increase of output. Can someone help?
Here is the code:
//Build an ArrayList to hold all of the words that we get from the imported tweets
String good []= {"good", "wonderfull", "great"};
String bad[] = {"bad", "down", "shit"};
ArrayList<String> words = new ArrayList();
import java.util.Date;
void setup() {
//Set the size of the stage, and the background to black.
size(400, 400);
background(0);
smooth();
//Credentials (authentication to allow to get on twitter)
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("RvP3nHvi6ji3r49uriTnfQ");
cb.setOAuthConsumerSecret("VihHFYhfiGfWgF8tyL5DkAUfwWtyYYON6e0QkFv9XIA");
cb.setOAuthAccessToken("150607540-LSYjvnbYfo9aCt8MpRXwJQn5ZdWceOJRmj9BmPPj");
cb.setOAuthAccessTokenSecret("LjI7eBE9vaMiOJfaI4VHus11lJ2h0Dxy3KWVCsOU");
//Make the twitter object and prepare the query
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("feel");
query.count(10); // amount of queries shown
//Try making the query request.
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Status t = (Status) tweets.get(i);
String msg = t.getText();
//only print the messaage
println( msg);
//Break the tweet into words
String[] input = msg.split(" ");
for (int j = 0; j < input.length; j++) {
//Put each word into the words ArrayList
words.add(input[j]);
}
};
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
}
void draw() {
//Draw a word from the list of words that we've built
int i = (frameCount % words.size());
String word = words.get(i);
if (word.equals(good)) {
//Put the word centre on the stage
fill(0,255,0);
textSize(random(10, 50));
text(word, random(width), random(height));
}
if (word.equals(bad)) {
//Put the word centre on the stage
fill(255,0,0);
textSize(random(10, 50));
text(word, random(width), random(height));
// textSize(150);
// text(word, width/2, height/2);
}
textAlign (CENTER);
fill (255);
textSize(20);
text("today we feel:", width/2, height/2-100);
}
Many thanks
Nanette
1