Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using java.util;
- using java.io;
- using edu.stanford.nlp.io;
- using edu.stanford.nlp.trees;
- using edu.stanford.nlp.pipeline;
- using edu.stanford.nlp.util;
- using edu.stanford.nlp.ling;
- using java.lang;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
- Properties props = new Properties();
- //props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
- props.put("annotators", "tokenize, ssplit, pos, lemma");
- //props.setProperty("sighanCorporaDict", @"..\..\..\..\temp\stanford-segmenter-2013-06-20\data");
- //props.setProperty("serDictionary", @"..\..\..\..\temp\stanford-segmenter-2013-06-20\data\dict-chris6.ser.gz");
- props.put("pos.model", @"PATH\stanford-postagger-2013-06-20\models\wsj-0-18-bidirectional-nodistsim.tagger");
- props.put("ner.model", "PATH\\stanford-ner-2013-06-20\\classifiers\\english.all.3class.distsim.crf.ser.gz");
- //"parse.model"
- StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
- // read some text in the text variable
- System.String text = "This is my text."; // Add your text here!
- // create an empty Annotation just with the given text
- Annotation document = new Annotation(text);
- // run all Annotators on this text
- pipeline.annotate(document);
- // 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
- CoreAnnotations.SentencesAnnotation CASA = new CoreAnnotations.SentencesAnnotation();
- Class cCASA = CASA.getClass();
- ArrayList myArrayList = (ArrayList)document.get(cCASA);
- List<CoreMap> sentences = new List<CoreMap>(myArrayList.size());
- foreach (CoreMap element in myArrayList)
- {
- sentences.Add(element);
- }
- foreach (CoreMap sentence in sentences)
- {
- CoreAnnotations.TokensAnnotation CATA = new CoreAnnotations.TokensAnnotation();
- Class cCATA = CATA.getClass();
- ArrayList myArrayListForcTA = (ArrayList)sentence.get(cCATA);
- List<CoreLabel> token_list = new List<CoreLabel>(myArrayListForcTA.size());
- foreach (CoreLabel element in myArrayListForcTA)
- {
- token_list.Add(element);
- }
- foreach (CoreLabel token in token_list)
- {
- // this is the text of the token
- CoreAnnotations.TextAnnotation TexA = new CoreAnnotations.TextAnnotation();
- Class cTexA = TexA.getClass();
- string word = (string)token.get(cTexA);
- System.Console.WriteLine(word);
- // this is the POS tag of the token
- CoreAnnotations.PartOfSpeechAnnotation PoSA = new CoreAnnotations.PartOfSpeechAnnotation();
- Class cPoSA = PoSA.getClass();
- string pos = (string)token.get(cPoSA);
- System.Console.WriteLine(pos);
- // this is the NER label of the token
- CoreAnnotations.NamedEntityTagAnnotation NETA = new CoreAnnotations.NamedEntityTagAnnotation();
- Class cNETA = NETA.getClass();
- string ne = (string)token.get(cNETA);
- System.Console.WriteLine(ne);
- }
- }
- System.Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement