Java Code Examples for edu.stanford.nlp.pipeline.StanfordCoreNLP#annotate()
The following examples show how to use
edu.stanford.nlp.pipeline.StanfordCoreNLP#annotate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CorefExample.java From blog-codes with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { Annotation document = new Annotation( "Barack Obama was born in Hawaii. He is the president. Obama was elected in 2008."); Properties props = new Properties(); props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,coref"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); pipeline.annotate(document); System.out.println("---"); System.out.println("coref chains"); for (CorefChain cc : document.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) { System.out.println("\t" + cc); } for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) { System.out.println("---"); System.out.println("mentions"); for (Mention m : sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)) { System.out.println("\t" + m); } } }
Example 2
Source File: CorefTool.java From Criteria2Query with Apache License 2.0 | 6 votes |
public void extractCoref() { String s="Subjects with hypothyroidism who are on stable treatment for 3 months prior to screening are required to have TSH and free thyroxine (FT4) obtained. If the TSH value is out of range, but FT4 is normal, such cases should be discussed directly with the JRD responsible safety physician before the subject is enrolled. If the FT4 value is out of range, the subject is not eligible."; Annotation document = new Annotation(s); Properties props = new Properties(); props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,mention,coref"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); pipeline.annotate(document); System.out.println("---"); System.out.println("coref chains"); for (CorefChain cc : document.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) { System.out.println("\t" + cc); } for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) { System.out.println("---"); System.out.println("mentions"); for (Mention m : sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)) { System.out.println("\t" + m); } } }
Example 3
Source File: StopwordAnnotatorTest.java From coreNlp with Apache License 2.0 | 5 votes |
/** * Test to validate that the custom stopword list words * @throws Exception */ @org.junit.Test public void testCustomStopwordList() throws Exception { //setup coreNlp properties for stopwords. Note the custom stopword list property Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, stopword"); props.setProperty("customAnnotatorClass.stopword", "intoxicant.analytics.coreNlp.StopwordAnnotator"); props.setProperty(StopwordAnnotator.STOPWORDS_LIST, customStopWordList); //get the custom stopword set Set<?> stopWords = StopwordAnnotator.getStopWordList(Version.LUCENE_36, customStopWordList, true); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation document = new Annotation(example); pipeline.annotate(document); List<CoreLabel> tokens = document.get(CoreAnnotations.TokensAnnotation.class); for (CoreLabel token : tokens) { //get the stopword annotation Pair<Boolean, Boolean> stopword = token.get(StopwordAnnotator.class); String word = token.word().toLowerCase(); if (stopWords.contains(word)) { assertTrue(stopword.first()); } else { assertFalse(stopword.first()); } //not checking lemma, so always false assertFalse(stopword.second()); } }
Example 4
Source File: VerbTest.java From tint with GNU General Public License v3.0 | 5 votes |
public static void main(String[] args) { try { Properties properties = new Properties(); properties.setProperty("annotators", "ita_toksent, udpipe, ita_verb"); properties.setProperty("customAnnotatorClass.udpipe", "eu.fbk.fcw.udpipe.api.UDPipeAnnotator"); properties.setProperty("customAnnotatorClass.ita_toksent", "eu.fbk.dh.tint.tokenizer.annotators.ItalianTokenizerAnnotator"); properties.setProperty("customAnnotatorClass.ita_verb", "eu.fbk.dh.tint.verb.VerbAnnotator"); properties.setProperty("udpipe.server", "gardner"); properties.setProperty("udpipe.port", "50020"); properties.setProperty("udpipe.keepOriginal", "1"); // properties.setProperty("udpipe.model", "/Users/alessio/Desktop/model"); StanfordCoreNLP pipeline = new StanfordCoreNLP(properties); Annotation annotation = new Annotation("Il caporale alpino Giampietro Civati caduto in combattimento il 5 dicembre 1944, come racconta Silvestri, ha scritto questo mirabile testamento: «sono figlio d’Italia, d’anni 21, non di Graziani e nemmeno di Badoglio, ma sono italiano e seguo la via che salverà l’onore d’Italia»."); pipeline.annotate(annotation); String out = JSONOutputter.jsonPrint(annotation); System.out.println(out); // for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) { // System.out.println(sentence.get(VerbAnnotations.VerbsAnnotation.class)); // } } catch (Exception e) { e.printStackTrace(); } }
Example 5
Source File: SentenceDetectors.java From java_in_examples with Apache License 2.0 | 5 votes |
private static String[] testStanfordCoreNLP(String text) throws Exception { StanfordCoreNLP coreNLP = getStanfordCoreNLP(); Annotation document = new Annotation(text); coreNLP.annotate(document); List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class); String[] result = new String[sentences.size()]; int i = 0; for (CoreMap sentence : sentences) { result[i] = sentence.toString(); i++; } return result; }
Example 6
Source File: StopwordAnnotatorTest.java From coreNlp with Apache License 2.0 | 5 votes |
/** * Test to validate that stopwords are properly annotated in the token list * @throws Exception */ @org.junit.Test public void testLuceneStopwordList() throws Exception { Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, stopword"); props.setProperty("customAnnotatorClass.stopword", "intoxicant.analytics.coreNlp.StopwordAnnotator"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation document = new Annotation(example); pipeline.annotate(document); List<CoreLabel> tokens = document.get(CoreAnnotations.TokensAnnotation.class); //get the standard lucene stopword set Set<?> stopWords = StopAnalyzer.ENGLISH_STOP_WORDS_SET; for (CoreLabel token : tokens) { //get the stopword annotation Pair<Boolean, Boolean> stopword = token.get(StopwordAnnotator.class); String word = token.word().toLowerCase(); if (stopWords.contains(word)) { assertTrue(stopword.first()); } else { assertFalse(stopword.first()); } //not checking lemma, so always false assertFalse(stopword.second()); } }
Example 7
Source File: CoreNLPUtils.java From minie with GNU General Public License v3.0 | 5 votes |
/** * Given a CoreNLP pipeline and an input sentence, generate dependency parse for the sentence and return * the SemanticGraph object as a result * @param pipeline - CoreNLP pipeline * @param snt - input sentence * @return dependency parse in SemanticGraph object */ public static SemanticGraph parse(StanfordCoreNLP pipeline, String snt) { Annotation document = new Annotation(snt); pipeline.annotate(document); //A CoreMap is a sentence with annotations List<CoreMap> sentences = document.get(SentencesAnnotation.class); SemanticGraph semanticGraph = null; for(CoreMap sentence: sentences) { semanticGraph = sentence.get(BasicDependenciesAnnotation.class); } return semanticGraphUniversalEnglishToEnglish(semanticGraph); }
Example 8
Source File: CoreNlpExample.java From core-nlp-example with MIT License | 5 votes |
public static void main(String[] args) { // 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 = "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); System.out.println(String.format("Print: word: [%s] pos: [%s] ne: [%s]", word, pos, ne)); } } }
Example 9
Source File: CoreNLPParser.java From Heracles with GNU General Public License v3.0 | 5 votes |
@Override public void validatedProcess(Dataset dataset, String spanTypeOfSentenceUnit) { Properties prop1 = new Properties(); prop1.setProperty("annotators", "parse"); StanfordCoreNLP pipeline = new StanfordCoreNLP(prop1, false); for (Span span : dataset.getSpans(spanTypeOfSentenceUnit)){ HashMap<Integer, Word> wordIndex = new HashMap<>(); Annotation a = CoreNLPHelper.reconstructStanfordAnnotations(span, wordIndex); // Annotation a = new Annotation((String)span.getAnnotations().get("text")); if (a == null){ System.out.println(a); } pipeline.annotate(a); for (CoreMap sentence : a.get(SentencesAnnotation.class)){ //per sentence, check the syntax tree Tree tree = sentence.get(TreeAnnotation.class); // tree.percolateHeadAnnotations(headFinder); // tree.indentedListPrint(); try { analyzeTree(tree, span, wordIndex); } catch (IllegalSpanException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Example 10
Source File: NERPipelineTest.java From FXDesktopSearch with Apache License 2.0 | 5 votes |
@Test public void testEnglish() throws IOException { // set up pipeline properties final Properties props = new Properties(); props.load(NERPipelineTest.class.getResourceAsStream("/StanfordCoreNLP.properties")); props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner"); props.setProperty("ner.useSUTime", "false"); props.setProperty("ner.applyFineGrained", "false"); // set up pipeline System.out.println("A"); final StanfordCoreNLP pipeline = new StanfordCoreNLP(props); // make an example document System.out.println("B"); //CoreDocument doc = new CoreDocument("Mirko Sertic is living in Münster, Germany. He is almost 40 years old. He likes movies. It is strange how things go wrong in New York City."); final CoreDocument doc = new CoreDocument("Mirko Sertic lebt und wohnt in der Stadt Münster, Deutschland."); // annotate the document System.out.println("C"); pipeline.annotate(doc); // view results System.out.println("D"); System.out.println("---"); System.out.println("entities found"); for (final CoreEntityMention em : doc.entityMentions()) System.out.println("\tdetected entity: \t"+em.text()+"\t"+em.entityType()); System.out.println("---"); System.out.println("tokens and ner tags"); final String tokensAndNERTags = doc.tokens().stream().map(token -> "("+token.word()+","+token.ner()+")").collect( Collectors.joining(" ")); System.out.println(tokensAndNERTags); }
Example 11
Source File: CoreferenceDemo.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 5 votes |
public static void main(String args[]){ String sentence = "He took his cash and she took her change " + "and together they bought their lunch."; Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation annotation = new Annotation(sentence); pipeline.annotate(annotation); // Map<Integer, CorefChain> corefChainMap = annotation.get(CorefChainAnnotation.class); Map<Integer, CorefChain> corefChainMap = annotation.get(CorefChainAnnotation.class); Set<Integer> set = corefChainMap.keySet(); Iterator<Integer> setIterator = set.iterator(); while(setIterator.hasNext()) { CorefChain corefChain = corefChainMap.get(setIterator.next()); System.out.println("CorefChain: " + corefChain); System.out.print("ClusterId: " + corefChain.getChainID()); CorefMention mention = corefChain.getRepresentativeMention(); System.out.println(" CorefMention: " + mention + " Span: [" + mention.mentionSpan + "]"); List<CorefMention> mentionList = corefChain.getMentionsInTextualOrder(); Iterator<CorefMention> mentionIterator = mentionList.iterator(); while(mentionIterator.hasNext()) { CorefMention cfm = mentionIterator.next(); System.out.println("tMention: " + cfm + " Span: [" + mention.mentionSpan + "]"); System.out.print("tMention Mention Type: " + cfm.mentionType + " Gender: " + cfm.gender); System.out.println(" Start: " + cfm.startIndex + " End: " + cfm.endIndex); } System.out.println(); } }
Example 12
Source File: StopwordAnnotatorTest.java From coreNlp with Apache License 2.0 | 5 votes |
/** * Test to validate that lemma values are checked against the (custom) stopword list * * NOTE: since we're loading the pos model into memory you'll need to set the VM memory size via '-Xms512m -Xmx1048m' * @throws Exception */ @org.junit.Test public void testStopwordsWithLemma() throws Exception { //setup coreNlp properties for stopwords. Note the custom stopword list and check for lemma property Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, pos, lemma, stopword"); props.setProperty("customAnnotatorClass.stopword", "intoxicant.analytics.coreNlp.StopwordAnnotator"); props.setProperty(StopwordAnnotator.STOPWORDS_LIST, customStopWordList); props.setProperty(StopwordAnnotator.CHECK_LEMMA, "true"); //get the custom stopword set Set<?> stopWords = StopwordAnnotator.getStopWordList(Version.LUCENE_36, customStopWordList, true); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation document = new Annotation(example); pipeline.annotate(document); List<CoreLabel> tokens = document.get(CoreAnnotations.TokensAnnotation.class); for (CoreLabel token : tokens) { //get the stopword annotation Pair<Boolean, Boolean> stopword = token.get(StopwordAnnotator.class); String word = token.word().toLowerCase(); if (stopWords.contains(word)) { assertTrue(stopword.first()); } else { assertFalse(stopword.first()); } String lemma = token.lemma().toLowerCase(); if (stopWords.contains(lemma)) { assertTrue(stopword.first()); } else { assertFalse(stopword.first()); } } }
Example 13
Source File: Chapter8.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 5 votes |
private static void usingStanfordPipelineParallel() { Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); String path = "C:\\Current Books\\NLP and Java\\Downloads\\stanford-ner-2014-10-26\\classifiers"; props.put("ner.model", path + "/english.muc.7class.distsim.crf.ser.gz"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation annotation1 = new Annotation("The robber took the cash and ran."); Annotation annotation2 = new Annotation("The policeman chased him down the street."); Annotation annotation3 = new Annotation("A passerby, watching the action, tripped the thief as he passed by."); Annotation annotation4 = new Annotation("They all lived happily everafter, except for the thief of course."); ArrayList<Annotation> list = new ArrayList(); list.add(annotation1); list.add(annotation2); list.add(annotation3); list.add(annotation4); Iterable<Annotation> iterable = list; pipeline.annotate(iterable); System.out.println("Total time: " + pipeline.timingInformation()); List<CoreMap> sentences = annotation2.get(SentencesAnnotation.class); for (CoreMap sentence : sentences) { for (CoreLabel token : sentence.get(TokensAnnotation.class)) { String word = token.get(TextAnnotation.class); String pos = token.get(PartOfSpeechAnnotation.class); System.out.println("Word: " + word + " POS Tag: " + pos); } } }
Example 14
Source File: CoreNLPCorefResolver.java From Heracles with GNU General Public License v3.0 | 5 votes |
@Override public void validatedProcess(Dataset dataset, String spanTypeOfTextualUnit) { Properties prop1 = new Properties(); // prop1.setProperty("annotators", "parse dcoref"); prop1.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,mention,coref"); prop1.setProperty("coref.algorithm", "neural"); StanfordCoreNLP pipeline = new StanfordCoreNLP(prop1, false); // Annotation document = new Annotation("Barack Obama was born in Hawaii. He is the president. Obama was elected in 2008."); for (Span span : dataset.getSpans(spanTypeOfTextualUnit)){ // TreeSet<Span> sentences = span.getDataset().getSpans(span, "sentence"); //// Framework.log("Sentences: "+sentences); // String reviewTextCorrected = ""; // for (Span sentence : sentences){ // reviewTextCorrected += " " + sentence.getAnnotations().get("text"); // } // reviewTextCorrected = reviewTextCorrected.trim().replaceAll(" "," "); Framework.log(span.getAnnotation("text")); HashMap<Integer, Word> wordIndex = new HashMap<>(); Annotation a = new Annotation(span.getAnnotation("text", String.class)); // Annotation a = CoreNLPHelper.reconstructStanfordAnnotations(span, wordIndex); pipeline.annotate(a); System.out.println("coref chains"); for (CorefChain cc : a.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) { System.out.println("\t" + cc); } // for (CoreMap sentence : a.get(CoreAnnotations.SentencesAnnotation.class)) { // System.out.println("---"); // System.out.println("mentions"); // for (Mention m : sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)) { // System.out.println("\t" + m); // } // } } }
Example 15
Source File: Chapter2.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 4 votes |
private static void usingTheStanfordTokenizer() { // Using PTBTokenizer System.out.println("----PTBTokenizer Example"); // First example // PTBTokenizer ptb = new PTBTokenizer(new StringReader(paragraph), // new CoreLabelTokenFactory(),null); // while (ptb.hasNext()) { // System.out.println(ptb.next()); // } // CoreLabel example CoreLabelTokenFactory ctf = new CoreLabelTokenFactory(); PTBTokenizer ptb = new PTBTokenizer(new StringReader(paragraph), ctf, "invertible=true"); // PTBTokenizer ptb = new PTBTokenizer(new StringReader(paragraph), // new WordTokenFactory(), null); while (ptb.hasNext()) { CoreLabel cl = (CoreLabel) ptb.next(); System.out.println(cl.originalText() + " (" + cl.beginPosition() + "-" + cl.endPosition() + ")"); } // Using a DocumentPreprocessor System.out.println("----DocumentPreprocessor Example"); Reader reader = new StringReader(paragraph); DocumentPreprocessor documentPreprocessor = new DocumentPreprocessor(reader); Iterator<List<HasWord>> it = documentPreprocessor.iterator(); while (it.hasNext()) { List<HasWord> sentence = it.next(); for (HasWord token : sentence) { System.out.println(token); } } // for (List<HasWord> sentence : documentPreprocessor) { //// List<HasWord> sentence = it.next(); // for (HasWord token : sentence) { // System.out.println(token); // } // } // Using a pipeline System.out.println("----pipeline Example"); Properties properties = new Properties(); properties.put("annotators", "tokenize, ssplit"); StanfordCoreNLP pipeline = new StanfordCoreNLP(properties); Annotation annotation = new Annotation(paragraph); pipeline.annotate(annotation); pipeline.prettyPrint(annotation, System.out); }
Example 16
Source File: CoreNLPPosTagger.java From Heracles with GNU General Public License v3.0 | 4 votes |
/** * Process the Dataset in chunks, as defined by the <code>spanType</code> parameter. * The Spans denoted by spanType must each contain Words belonging to a single sentence. * */ @Override public void validatedProcess(Dataset dataset, String spanTypeOfSentenceUnit){ // if (dataset.getPerformedNLPTasks().contains(getTask())){ // Framework.error("This dataset has already been tagged with POS."); // return; // } //check if prerequisites are satisfied if (!dataset.getPerformedNLPTasks().containsAll(prerequisites)){ HashSet<NLPTask> missingTasks = new HashSet<>(); missingTasks.addAll(prerequisites); missingTasks.removeAll(dataset.getPerformedNLPTasks()); Framework.error("This dataset does not meet the requirements to use this component! Missing tasks: " + missingTasks); return; } Properties prop1 = new Properties(); prop1.setProperty("annotators", "pos"); StanfordCoreNLP pipeline = new StanfordCoreNLP(prop1, false); for (Span span : dataset.getSpans(spanTypeOfSentenceUnit)){ HashMap<Integer, Word> wordIndex = new HashMap<>(); Annotation a = CoreNLPHelper.reconstructStanfordAnnotations(span, wordIndex); if (a == null){ System.out.println(a); } pipeline.annotate(a); List<CoreMap> sentenceAnnotations = a.get(SentencesAnnotation.class); for (CoreMap sentence : sentenceAnnotations){ for (CoreLabel token: sentence.get(TokensAnnotation.class)) { Word w = wordIndex.get(token.get(CharacterOffsetBeginAnnotation.class)); String tempPos = token.get(PartOfSpeechAnnotation.class); if (w.hasAnnotation("URI")){ w.putAnnotation("pos", "NNP"); } else { w.putAnnotation("pos", tempPos); } // System.out.println(w.getAnnotations()); } } } }
Example 17
Source File: CoreNLPDependencyParser.java From Heracles with GNU General Public License v3.0 | 4 votes |
@Override public void validatedProcess(Dataset dataset, String spanTypeOfSentenceUnit) { Properties prop1 = new Properties(); prop1.setProperty("annotators", "depparse"); StanfordCoreNLP pipeline = new StanfordCoreNLP(prop1, false); for (Span span : dataset.getSpans(spanTypeOfSentenceUnit)){ HashMap<Integer, Word> wordIndex = new HashMap<>(); Annotation a = CoreNLPHelper.reconstructStanfordAnnotations(span, wordIndex); // Main.debug(span.toString()); pipeline.annotate(a); for (CoreMap sentence : a.get(SentencesAnnotation.class)){ //per sentence, get the dependencies SemanticGraph dependencies = sentence.get(EnhancedPlusPlusDependenciesAnnotation.class); for (TypedDependency td : dependencies.typedDependencies()){ // Main.debug(td.toString()); String relationType = td.reln().getLongName(); Word dep = wordIndex.get(td.dep().beginPosition()); DataEntity gov = wordIndex.get(td.gov().beginPosition()); if (gov == null){ //this is the root, link to sentence gov = span; } if (dep == null || gov == null){ Framework.debug(td.toString()); Framework.debug(td.dep().beginPosition() + "\t" + td.gov().beginPosition()); Framework.debug(wordIndex.toString()); } Relation rel = new Relation("deps", gov, dep); rel.putAnnotation("relationLongName", td.reln().getLongName()); if (td.reln().getParent() != null) rel.putAnnotation("relationParentShortName", td.reln().getParent().getShortName()); rel.putAnnotation("relationShortName", td.reln().getShortName()); // rel.putAnnotation("relationSpecific", td.reln().getSpecific()); dep.getRelations().addRelationToParent(rel); gov.getRelations().addRelationToChild(rel); } // dependencies.prettyPrint(); } } }
Example 18
Source File: Chapter6.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 4 votes |
private static void usingStanfordSentimentAnalysis() { String review = "An overly sentimental film with a somewhat " + "problematic message, but its sweetness and charm " + "are occasionally enough to approximate true depth " + "and grace. "; String sam = "Sam was an odd sort of fellow. Not prone to angry and " + "not prone to merriment. Overall, an odd fellow."; String mary = "Mary thought that custard pie was the best pie in the " + "world. However, she loathed chocolate pie."; Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, parse, sentiment"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation annotation = new Annotation(review); pipeline.annotate(annotation); System.out.println("---sentimentText"); String[] sentimentText = {"Very Negative", "Negative", "Neutral", "Positive", "Very Positive"}; for (CoreMap sentence : annotation.get( CoreAnnotations.SentencesAnnotation.class)) { Tree tree = sentence.get( SentimentCoreAnnotations.AnnotatedTree.class); System.out.println("---Number of children: " + tree.numChildren()); System.out.println("[" + tree.getChild(0) + "][" + tree.getChild(1) + "]"); tree.printLocalTree(); int score = RNNCoreAnnotations.getPredictedClass(tree); System.out.println(sentimentText[score]); } // Classifer CRFClassifier crf = CRFClassifier.getClassifierNoExceptions( "C:/Current Books in Progress/NLP and Java/Models" + "/english.all.3class.distsim.crf.ser.gz"); String S1 = "Good afternoon Rajat Raina, how are you today?"; String S2 = "I go to school at Stanford University, which is located in California."; System.out.println(crf.classifyToString(S1)); System.out.println(crf.classifyWithInlineXML(S2)); System.out.println(crf.classifyToString(S2, "xml", true)); Object classification[] = crf.classify(S2).toArray(); for (int i = 0; i < classification.length; i++) { System.out.println(classification[i]); } }
Example 19
Source File: POSExample.java From core-nlp-example with MIT License | 4 votes |
public static void main(String[] args) { StanfordCoreNLP stanfordCoreNLP = Pipeline.getPipeline(); String text = "Hey! I am Dinesh Krishnan."; CoreDocument coreDocument = new CoreDocument(text); stanfordCoreNLP.annotate(coreDocument); List<CoreLabel> coreLabelList = coreDocument.tokens(); for(CoreLabel coreLabel : coreLabelList) { String pos = coreLabel.get(CoreAnnotations.PartOfSpeechAnnotation.class); System.out.println(coreLabel.originalText() + " = "+ pos); } }
Example 20
Source File: NERExample.java From core-nlp-example with MIT License | 3 votes |
public static void main(String[] args) { StanfordCoreNLP stanfordCoreNLP = Pipeline.getPipeline(); String text = "Hey! My name is Krishnan and I have friend his name is Robert." + " We both are living in Berlin"; CoreDocument coreDocument = new CoreDocument(text); stanfordCoreNLP.annotate(coreDocument); List<CoreLabel> coreLabels = coreDocument.tokens(); for(CoreLabel coreLabel : coreLabels) { String ner = coreLabel.get(CoreAnnotations.NamedEntityTagAnnotation.class); System.out.println(coreLabel.originalText() + " = "+ ner); } /* List nameList = coreLabels .stream() .filter(coreLabel -> "Person".equalsIgnoreCase(coreLabel.get(CoreAnnotations.NamedEntityTagAnnotation.class))) .collect(Collectors.toList()); System.out.println(nameList); */ }