edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation Java Examples
The following examples show how to use
edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation.
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: Chapter5.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 6 votes |
private static void usingStanfordPOSTagger() { Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, pos"); props.put("pos.model", "C:\\Current Books in Progress\\NLP and Java\\Models\\english-caseless-left3words-distsim.tagger"); props.put("pos.maxlen", 10); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation document = new Annotation(theSentence); pipeline.annotate(document); List<CoreMap> sentences = document.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.print(word + "/" + pos + " "); } System.out.println(); try { pipeline.xmlPrint(document, System.out); pipeline.prettyPrint(document, System.out); } catch (IOException ex) { ex.printStackTrace(); } } }
Example #2
Source File: ReconTool.java From Criteria2Query with Apache License 2.0 | 6 votes |
public boolean isCEE(String text){ text = text.replace("/", " / "); Annotation annotation = new Annotation(text); pipeline.annotate(annotation); List<CoreMap> sentences = annotation.get(SentencesAnnotation.class); boolean flag=false; for (CoreMap sentence : sentences) { for (CoreLabel token : sentence.get(TokensAnnotation.class)) { String word = token.get(TextAnnotation.class);//token.get(LemmaAnnotation.class);//TextAnnotation.class String pos = token.get(PartOfSpeechAnnotation.class); //String lemma = token.get(LemmaAnnotation.class); boolean f = false; if ((word.equals("and") || word.equals(",") || word.equals("/") || word.equals("or"))) { flag = true; break; } } } return flag; }
Example #3
Source File: CoreNlpTokenizer.java From jstarcraft-nlp with Apache License 2.0 | 5 votes |
@Override public boolean incrementToken() { clearAttributes(); while (tokens == null || !tokens.hasNext()) if (!getNextSentence()) return false; CoreLabel token = tokens.next(); // Use the lemmatized word: String word = token.get(LemmaAnnotation.class); if (word == null) { // Fallback when no lemmatization happens. word = token.get(TextAnnotation.class); } termAttribute.setLength(0); termAttribute.append(word); // NER or part of speech annotation String pos = token.get(NamedEntityTagAnnotation.class); pos = (pos == null || "O".equals(pos)) ? token.get(PartOfSpeechAnnotation.class) : pos; typeAttribute.setType(pos != null ? pos : TypeAttribute.DEFAULT_TYPE); // Token character offsets int be = token.get(CharacterOffsetBeginAnnotation.class).intValue(); int en = token.get(CharacterOffsetEndAnnotation.class).intValue(); offsetAttribute.setOffset(be, en); // Token in-document position increment: positionAttribute.setPositionIncrement(1 + skippedTokens); skippedTokens = 0; return true; }
Example #4
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 #5
Source File: CoreNLP.java From gAnswer with BSD 3-Clause "New" or "Revised" License | 5 votes |
public Word[] getTaggedWords (String sentence) { CoreMap taggedSentence = getPOS(sentence); Word[] ret = new Word[taggedSentence.get(TokensAnnotation.class).size()]; int count = 0; for (CoreLabel token : taggedSentence.get(TokensAnnotation.class)) { // this is the text of the token String word = token.get(TextAnnotation.class); // this is the POS tag of the token String pos = token.get(PartOfSpeechAnnotation.class); //System.out.println(word+"["+pos+"]"); ret[count] = new Word(getBaseFormOfPattern(word.toLowerCase()), word, pos, count+1); count ++; } return ret; }
Example #6
Source File: CoreNLPPreprocessor.java From phrasal with GNU General Public License v3.0 | 5 votes |
@Override public Sequence<IString> process(String input) { String tokenizerInput = toUncased(input.trim()); Tokenizer<CoreLabel> tokenizer = tf.getTokenizer(new StringReader(tokenizerInput)); List<String> outputStrings = new ArrayList<>(); while (tokenizer.hasNext()) { String string = tokenizer.next().get(TextAnnotation.class); outputStrings.add(string); } Sequence<IString> rv = IStrings.toIStringSequence(outputStrings); if(compoundSplitter != null) rv = compoundSplitter.process(rv); return rv; }
Example #7
Source File: WiseOwlStanfordFilter.java From wiseowl with MIT License | 4 votes |
public static void main(String args[]) { Properties props = new Properties(); props.setProperty("annotators", "tokenize, cleanxml, ssplit,pos,lemma,ner"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); pipeline.addAnnotator(new TimeAnnotator("sutime", props)); String text = "<mydata> refeer</mydata>today is 12 jan 2016. what is tommorow? Who is Avtar? Does he work at Apple or Google? Sumit was born on 13 feb,2011."; Annotation document = new Annotation(text); pipeline.annotate(document); System.out.println(document.get(CoreAnnotations.TextAnnotation.class)); List<CoreMap> timexAnnsAll = document.get(TimeAnnotations.TimexAnnotations.class); for (CoreMap cm : timexAnnsAll) { List<CoreLabel> tokens = cm.get(CoreAnnotations.TokensAnnotation.class); TimeData td=new TimeData(); td.setTime(cm.get(TimeExpression.Annotation.class).getTemporal().toISOString()); td.setStart(tokens.get(0).get(CoreAnnotations.CharacterOffsetBeginAnnotation.class)); td.setEnd(tokens.get(tokens.size() - 1).get(CoreAnnotations.CharacterOffsetEndAnnotation.class)); } List<CoreMap> sentences = document.get(SentencesAnnotation.class); for(CoreMap sentence: sentences) { // traversing the words in the current sentence // a CoreLabel is a CoreMap with additional token-specific methods System.out.println("in sent"); for (CoreLabel token: sentence.get(TokensAnnotation.class)) { // this is the text of the token System.out.println("in token"); String word = token.get(TextAnnotation.class); // this is the POS tag of the token String pos = token.get(PartOfSpeechAnnotation.class); // this is the NER label of the token String ne = token.get(NamedEntityTagAnnotation.class); System.out.println("word : "+word+" pos: "+pos+" ner: "+ne); } } }
Example #8
Source File: WiseOwlStanfordFilter.java From wiseowl with MIT License | 4 votes |
public Iterator findTokens() throws IOException { /*char[] c = new char[256]; int sz = 0; StringBuilder b = new StringBuilder(); while ((sz = input.read(c)) >= 0) { b.append(c, 0, sz); }*/ //String text = b.toString(); if (!input.incrementToken()) return null; String text; text = input.getAttribute(CharTermAttribute.class).toString(); // read some text in the text variable //System.out.println("before annotation"); Annotation document = new Annotation(text); // these are all the sentences in this document // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types pipeline.annotate(document); List<CoreMap> timexAnnsAll = document.get(TimeAnnotations.TimexAnnotations.class); for (CoreMap cm : timexAnnsAll) { List<CoreLabel> tokens = cm.get(CoreAnnotations.TokensAnnotation.class); TimeData td=new TimeData(); td.setTime(cm.get(TimeExpression.Annotation.class).getTemporal().toString()); td.setStart(tokens.get(0).get(CoreAnnotations.CharacterOffsetBeginAnnotation.class)); td.setEnd(tokens.get(tokens.size() - 1).get(CoreAnnotations.CharacterOffsetEndAnnotation.class)); timeQueue.add(td); } List<CoreMap> sentences = document.get(SentencesAnnotation.class); //System.out.println("after annotation and sentence getting"+sentences.size()); 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(TokensAnnotation.class)) { // this is the text of the token System.out.println("in token"); String word = token.get(TextAnnotation.class); // this is the POS tag of the token String pos = token.get(PartOfSpeechAnnotation.class); // this is the NER label of the token String ne = token.get(NamedEntityTagAnnotation.class); // System.out.println("word : "+word+" pos: "+pos+" ner: "+ne); TokenData tok=new TokenData(); tok.setNER(ne); tok.setToken(word); tok.setPOS(pos); tokenQueue.add(tok); } } Iterator<TokenData> it=tokenQueue.iterator(); itr_cpy=tokenQueue.iterator(); tokenOffset=0; start=0; end=0; return it; }
Example #9
Source File: CoreNLPHelper.java From Heracles with GNU General Public License v3.0 | 4 votes |
public static Annotation reconstructStanfordAnnotations(Span sentenceSpan, HashMap<Integer, Word> wordIndex, boolean useWordOrderInsteadOfOffset){ String originalText = sentenceSpan.getAnnotation("text", String.class); Annotation a = new Annotation(originalText); a.set(TextAnnotation.class, originalText); //a.set(DocIDAnnotation.class, "document"); List<CoreMap> sentenceAnnotations = new ArrayList<CoreMap>(); a.set(SentencesAnnotation.class, sentenceAnnotations); List<CoreLabel> tokenAnnotations = new ArrayList<CoreLabel>(); a.set(TokensAnnotation.class, tokenAnnotations); ArrayCoreMap sentenceAnnotation = new ArrayCoreMap(); sentenceAnnotations.add(sentenceAnnotation); // int startOffset = sentenceSpan.first().getStartOffset(); for (Word w : sentenceSpan){ CoreLabel c = new CoreLabel(); c.set(TextAnnotation.class, w.getWord()); c.set(OriginalTextAnnotation.class, w.getWord()); c.set(ValueAnnotation.class, w.getWord()); c.set(CharacterOffsetBeginAnnotation.class, w.getStartOffset()); c.set(CharacterOffsetEndAnnotation.class, w.getEndOffset()); c.set(IndexAnnotation.class, w.getOrder()+1); // c.setIndex(w.getOrder()); c.set(SentenceIndexAnnotation.class, 0); // c.setSentIndex(0); c.set(DocIDAnnotation.class, "document"); c.setDocID("document"); if (w.hasAnnotation("pos")) c.set(PartOfSpeechAnnotation.class, w.getAnnotation("pos",String.class)); if (w.hasAnnotation("lemma")) c.set(LemmaAnnotation.class, w.getAnnotation("lemma", String.class)); if (w.hasAnnotation("nerLabel")) c.set(NamedEntityTagAnnotation.class, w.getAnnotation("nerLabel", String.class)); if (w.hasAnnotation("nerValue")) c.set(NormalizedNamedEntityTagAnnotation.class, w.getAnnotation("nerValue", String.class)); tokenAnnotations.add(c); if (useWordOrderInsteadOfOffset){ wordIndex.put(w.getOrder(), w); } else { wordIndex.put(w.getStartOffset(), w); } } //essential sentence annotation: TokensAnnotation sentenceAnnotation.set(TokensAnnotation.class, tokenAnnotations); //essential sentence annotation: TextAnnotation sentenceAnnotation.set(TextAnnotation.class, originalText); //essential sentence annotation: SentenceIndexAnnotation sentenceAnnotation.set(SentenceIndexAnnotation.class, 0); sentenceAnnotation.set(CharacterOffsetBeginAnnotation.class, 0); sentenceAnnotation.set(CharacterOffsetEndAnnotation.class, sentenceSpan.last().getEndOffset()); sentenceAnnotation.set(TokenBeginAnnotation.class, 0); sentenceAnnotation.set(TokenEndAnnotation.class, sentenceSpan.last().getOrder()); return a; }
Example #10
Source File: CoreNLPToJSON.java From phrasal with GNU General Public License v3.0 | 4 votes |
/** * Process an English text file. * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { if (args.length < 1) { System.err.printf("Usage: java %s file [inputproperties_str] > json_output%n", CoreNLPToJSON.class.getName()); System.exit(-1); } String textFile = args[0]; InputProperties inputProperties = args.length > 1 ? InputProperties.fromString(args[1]) : new InputProperties(); StanfordCoreNLP coreNLP = new StanfordCoreNLP(properties); // Configure tokenizer EnglishPreprocessor preprocessor = new EnglishPreprocessor(true); // Use a map with ordered keys so that the output is ordered by segmentId. Map<Integer,SourceSegment> annotations = new TreeMap<Integer,SourceSegment>(); LineNumberReader reader = IOTools.getReaderFromFile(textFile); for (String line; (line = reader.readLine()) != null;) { Annotation annotation = coreNLP.process(line); List<CoreMap> sentences = annotation.get(SentencesAnnotation.class); if (sentences.size() != 1) { throw new RuntimeException("Sentence splitting on line: " + String.valueOf(reader.getLineNumber())); } CoreMap sentence = sentences.get(0); Tree tree = sentence.get(TreeAnnotation.class); tree.indexLeaves(); int[] chunkVector = getChunkVector(tree); List<CoreLabel> tokens = sentence.get(TokensAnnotation.class); int numTokens = tokens.size(); SymmetricalWordAlignment alignment = preprocessor.processAndAlign(line); if (alignment.e().size() != numTokens) { throw new RuntimeException(String.format("Tokenizer configurations differ: %d/%d", alignment.e().size(), numTokens)); } SourceSegment segment = new SourceSegment(numTokens); segment.layoutSpec.addAll(makeLayoutSpec(alignment)); segment.inputProperties = inputProperties.toString(); for (int j = 0; j < numTokens; ++j) { CoreLabel token = tokens.get(j); String word = token.get(TextAnnotation.class); segment.tokens.add(unescape(word)); String pos = mapPOS(token.get(PartOfSpeechAnnotation.class)); segment.pos.add(pos); String ne = token.get(NamedEntityTagAnnotation.class); segment.ner.add(ne); segment.chunkVector[j] = chunkVector[j]; } annotations.put(reader.getLineNumber()-1, segment); } reader.close(); System.err.printf("Processed %d sentences%n", reader.getLineNumber()); final SourceDocument jsonDocument = new SourceDocument(textFile, annotations); // Convert to json Gson gson = new Gson(); String json = gson.toJson(jsonDocument); System.out.println(json); }