Jawn78

Untitled

Jan 11th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. import edu.stanford.nlp.ling.CoreAnnotations;
  2. import edu.stanford.nlp.ling.CoreLabel;
  3. import edu.stanford.nlp.pipeline.Annotation;
  4. import edu.stanford.nlp.pipeline.StanfordCoreNLP;
  5. import edu.stanford.nlp.util.CoreMap;
  6. import java.io.BufferedInputStream;
  7. import java.io.BufferedWriter;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.nio.file.Files;
  13. import java.nio.file.Path;
  14. import java.nio.file.Paths;
  15. import java.util.Arrays;
  16.  
  17.  
  18. import java.util.List;
  19. import java.util.Properties;
  20. import org.apache.commons.csv.CSVFormat;
  21. import org.apache.commons.csv.CSVPrinter;
  22. import org.apache.tika.exception.TikaException;
  23. import org.apache.tika.metadata.Metadata;
  24. import org.apache.tika.parser.AutoDetectParser;
  25. import org.apache.tika.parser.ParseContext;
  26. import org.apache.tika.parser.Parser;
  27. import org.apache.tika.sax.BodyContentHandler;
  28. import org.xml.sax.SAXException;
  29.  
  30. public class runner {
  31.    
  32.         private static final String SAMPLE_CSV_FILE = "./sample.csv";
  33.    
  34.         public static BodyContentHandler process() throws Exception {
  35.      FileInputStream mypdfstream = new FileInputStream(new File("C:\\Users\\jonat\\Documents\\resume\\Dave.docx"));
  36.          BodyContentHandler handler = new BodyContentHandler();  
  37.          AutoDetectParser parser = new AutoDetectParser();
  38.         Metadata metadata = new Metadata();
  39.         ParseContext context = new ParseContext();
  40.         parser.parse(mypdfstream,handler,metadata,context);
  41.         return handler;
  42.         }
  43.  
  44.  
  45.     public static void main(String[] args) throws Exception {
  46.  
  47.         // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
  48.         Properties props = new Properties();
  49.         props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
  50.         StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
  51.  
  52.         // read some text in the text variable
  53.         String text = process().toString();
  54.         //"What is the Weather in Bangalore right now?";
  55.        
  56.         // create an empty Annotation just with the given text
  57.         Annotation document = new Annotation(text);
  58.  
  59.         // run all Annotators on this text
  60.         pipeline.annotate(document);
  61.  
  62.         List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
  63.  
  64.         for (CoreMap sentence : sentences) {
  65.             // traversing the words in the current sentence
  66.             // a CoreLabel is a CoreMap with additional token-specific methods
  67.             for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
  68.                 // this is the text of the token
  69.                 String word = token.get(CoreAnnotations.TextAnnotation.class);
  70.                 // this is the POS tag of the token
  71.                 String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
  72.                 // this is the NER label of the token
  73.                 String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);
  74.  
  75.                
  76.                
  77.            
  78.             BufferedWriter writer = Files.newBufferedWriter(Paths.get(SAMPLE_CSV_FILE));
  79.  
  80.             CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
  81.                     .withHeader("Print"));
  82.         {
  83.             csvPrinter.printRecord(String.format("Print: word: [%s] pos: [%s] ne: [%s]", word, pos, ne));
  84.            
  85.             csvPrinter.flush();    
  86.            
  87.             //check https://www.programcreek.com/java-api-examples/?class=org.apache.commons.csv.CSVPrinter&method=printRecord
  88.         }
  89.  
  90.              
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment