Twiiter String Elements Removal

nmnnmn
edited May 2016 in Library Questions

Hi everybody,

I'm trying to write a sketch using Twitter API. I want to retrive tweets with a specific hashtag so I can use them. Unfortunately, I keep getting stuff like http related words in the middle of it.

Is there a practical way to prevent this or to remove them from the array?

This is the code I am using. These are all functions that are being called somewhere else.

Thank you so much

Best

    void makeobject(){ 


      //Make the twitter object and prepare the query
      twitterInstance = new TwitterFactory(cb.build()).getInstance();
      queryForTwitter = new Query("#summerparty");
      queryForTwitter.count(50);  

    }

void createtquery() {


  //Try making the query request.
  try {
    QueryResult result = twitterInstance.search(queryForTwitter);
    tweets = (ArrayList) result.getTweets();
  } //END of try()
  catch (TwitterException te) {
    println("Couldn't connect: " + te);
  } //END Twitter Exception
} //END of void


void twitterdraw() {


  for (int i = 0; i < tweets.size (); i++) {
      Status t = (Status) tweets.get(i);
      String user = t.getUser().getName();
      String msg = t.getText();
      // Date d = t.getCreatedAt();
      println(msg);
      //text(msg, 150, 150);



 //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]);
      }  //END of for()


  } //END of for()


  //Draw a word from the list 
  int i = (int(random(0, words.size())) % words.size());
  String  word = words.get(i);

  //Put it on the stage
  text(word, 350, 110);


} 
Tagged:

Answers

  • Http related words being...?

  • Hi,

    literally URL's like these:

    RT @garavoguebar: #HelloSummer with the ultimate #SummerParty this Friday #BBQ #Livemusic with Dean Mahon Music, JJ and... https://t.co/3QS

    When I wasn't sick yet. Hahaha. #Bright #Bright2016 #caesars #summerparty #party #partytime #clubbing #clubbin #clu… https://t.co/oEoT4GlNFC

    thanks

  • Those are pictures attached to the tweets.

    Split the string into words, use java's startsWith() method to weed out the words beginning with https://

  • _vk_vk
    edited May 2016 Answer ✓

    something like:

    String t ="When I wasn't sick yet. Hahaha. #Bright #Bright2016 #caesars #summerparty #party #partytime #clubbing #clubbin #clu… https://"+"t.co/oEoT4GlNFC";
    
    String[] words = splitTokens(t, WHITESPACE);
    
    printArray(words);
    println
    ("Ok each word in your slot... Lets clean urls now...\n");
    
    ArrayList <String> noHTTP = new ArrayList<String>();
    
    for (String s : words) {
      if (!s.startsWith("http") ) {
        noHTTP.add(s);
      }
    }
    
    printArray(noHTTP.toArray());
    println("\nthat's it...");
    
  • Thank you so much. It works perfectly! The thing was bothering me a long time now and it's finally solved! :)

Sign In or Register to comment.