Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import edu.stanford.nlp.ling.CoreAnnotations;
- import edu.stanford.nlp.ling.CoreLabel;
- import edu.stanford.nlp.pipeline.Annotation;
- import edu.stanford.nlp.pipeline.StanfordCoreNLP;
- import edu.stanford.nlp.util.CoreMap;
- import java.io.BufferedInputStream;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Properties;
- import org.apache.commons.csv.CSVFormat;
- import org.apache.commons.csv.CSVPrinter;
- import org.apache.tika.exception.TikaException;
- import org.apache.tika.metadata.Metadata;
- import org.apache.tika.parser.AutoDetectParser;
- import org.apache.tika.parser.ParseContext;
- import org.apache.tika.parser.Parser;
- import org.apache.tika.sax.BodyContentHandler;
- import org.xml.sax.SAXException;
- public class runner {
- private static final String SAMPLE_CSV_FILE = "./sample.csv";
- public static BodyContentHandler process() throws Exception {
- FileInputStream mypdfstream = new FileInputStream(new File("C:\\Users\\jonat\\Documents\\resume\\Dave.docx"));
- BodyContentHandler handler = new BodyContentHandler();
- AutoDetectParser parser = new AutoDetectParser();
- Metadata metadata = new Metadata();
- ParseContext context = new ParseContext();
- parser.parse(mypdfstream,handler,metadata,context);
- return handler;
- }
- public static void main(String[] args) throws Exception {
- // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
- Properties props = new Properties();
- props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
- StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
- // read some text in the text variable
- String text = process().toString();
- //"What is the Weather in Bangalore right now?";
- // create an empty Annotation just with the given text
- Annotation document = new Annotation(text);
- // run all Annotators on this text
- pipeline.annotate(document);
- List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
- for (CoreMap sentence : sentences) {
- // traversing the words in the current sentence
- // a CoreLabel is a CoreMap with additional token-specific methods
- for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
- // this is the text of the token
- String word = token.get(CoreAnnotations.TextAnnotation.class);
- // this is the POS tag of the token
- String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
- // this is the NER label of the token
- String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);
- BufferedWriter writer = Files.newBufferedWriter(Paths.get(SAMPLE_CSV_FILE));
- CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
- .withHeader("Print"));
- {
- csvPrinter.printRecord(String.format("Print: word: [%s] pos: [%s] ne: [%s]", word, pos, ne));
- csvPrinter.flush();
- //check https://www.programcreek.com/java-api-examples/?class=org.apache.commons.csv.CSVPrinter&method=printRecord
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment