Tag Tweet using stanford nlp - Java Social Media

Java examples for Social Media:Twitter

Description

Tag Tweet using stanford nlp

Demo Code



import twitter4j.*;
import java.io.*;
import java.util.ArrayList;

import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;

import twitter4j.conf.*;

public class PrintTweets {

    public static void main(String[] args) {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)/*  w w  w .j  a  va  2s .co m*/
                .setOAuthConsumerKey("a")
                .setOAuthConsumerSecret(
                        "a")
                .setOAuthAccessToken(
                        "a-a")
                .setOAuthAccessTokenSecret(
                        "a");
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        QueryResult result;
        FileWriter write;
        PrintWriter pw;
        String message;
        ArrayList<String> corpus = new ArrayList<String>();

        try {
            write = new FileWriter("fetched_tweets2.txt",true);
            pw = new PrintWriter(write);
        } catch (Exception e) {
            System.out.println("File could not be found");
            return;
        }

        Query query = new Query("to:VodafoneIN");
        try {
            result = twitter.search(query);

            for (Tweet tweet : result.getTweets()) {
                message = tweet.getFromUser() + ": " + tweet.getText();
                System.out.println(message);
                pw.println(message);
                corpus.add(tweet.getText());
            }

            try {
                MaxentTagger tagger = new MaxentTagger(
                        "left3words.tagger");

                String taggedString = tagger
                        .tagString("Here's a tagged string.");
                System.out.println(taggedString);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (TwitterException te) {
            System.out.println("could not search!!");
        }

        pw.close();
    }

}

Related Tutorials