Advertisement
markgrenader

NLP.java

Aug 5th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.util.List;
  4.  
  5. import edu.stanford.nlp.io.IOUtils;
  6. import edu.stanford.nlp.ling.CoreAnnotations;
  7. import edu.stanford.nlp.pipeline.*;
  8. import edu.stanford.nlp.trees.*;
  9. import edu.stanford.nlp.util.CoreMap;
  10.  
  11.  
  12. public class NLP {
  13.  
  14.     public NLP() {
  15.         // TODO Auto-generated constructor stub
  16.     }
  17.  
  18.    
  19.      public static void main(String[] args) throws IOException {
  20.             PrintWriter out;
  21.             if (args.length > 1) {
  22.               out = new PrintWriter(args[1]);
  23.             } else {
  24.               out = new PrintWriter(System.out);
  25.             }
  26.             PrintWriter xmlOut = null;
  27.             if (args.length > 2) {
  28.               xmlOut = new PrintWriter(args[2]);
  29.             }
  30.  
  31.             StanfordCoreNLP pipeline = new StanfordCoreNLP();
  32.             Annotation annotation;
  33.             if (args.length > 0) {
  34.               annotation = new Annotation(IOUtils.slurpFileNoExceptions(args[0]));
  35.             } else {
  36.               annotation = new Annotation("Kosgi Santosh sent an email to Stanford University. He didn't get a reply.");
  37.             }
  38.  
  39.             pipeline.annotate(annotation);
  40.             pipeline.prettyPrint(annotation, out);
  41.             if (xmlOut != null) {
  42.               pipeline.xmlPrint(annotation, xmlOut);
  43.             }
  44.             // An Annotation is a Map and you can get and use the various analyses individually.
  45.             // For instance, this gets the parse tree of the first sentence in the text.
  46.             List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
  47.             if (sentences != null && sentences.size() > 0) {
  48.               CoreMap sentence = sentences.get(0);
  49.               Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
  50.               out.println();
  51.               out.println("The first sentence parsed is:");
  52.               tree.pennPrint(out);
  53.             }
  54.           }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement