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");
- props.put("pos.model", @"****\stanford-postagger-2013-06-20\models\wsj-0-18-bidirectional-nodistsim.tagger");
- props.put("ner.model", "****\\stanford-ner-2013-06-20\\classifiers\\english.all.3class.distsim.crf.ser.gz");
- props.put("parse.model", "****\\englishPCFG.ser.gz");
- props.put("sutime.binders", "0"); // bugfix for NER
- System.String SuTimePath = "****\\stanford-corenlp-full-2013-06-20\\sutime\\defs.sutime.txt";
- System.String SuTimeEnglishPath = "****\\stanford-corenlp-full-2013-06-20\\sutime\\english.sutime.txt";
- props.put("sutime.rules", System.String.Format("{0}, {1}", SuTimePath, SuTimeEnglishPath));
- /* Attempt to get dcoref working. Here are some lines that you probably need, but not all of them.
- If you want to try out "dcoref", use
- props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
- instead. Also add
- props.put("dcoref.demonym", "****\\demonyms.txt");
- props.put("dcoref.animate", "****\\animate.unigrams.txt");
- props.put("dcoref.states", "****\\state-abbreviations.txt");
- props.put("dcoref.female", "****\\female.unigrams.txt");
- props.put("dcoref.inanimate", "****\\inanimate.unigrams.txt");
- props.put("dcoref.male", "****\\male.unigrams.txt");
- props.put("dcoref.neutral", "****\\neutral.unigrams.txt");
- props.put("dcoref.plural", "****\\plural.unigrams.txt");
- props.put("dcoref.singular", "****\\singular.unigrams.txt");
- props.put("dcoref.extra.gender", "****\\namegender.combine.txt");
- */
- StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
- // read some text in the text variable
- System.String text = "This is my text."; // Add your text here!
- text = System.IO.File.ReadAllText(@"c:\yourfile.txt");
- // 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)
- {
- System.Console.WriteLine(sentence.ToString());
- 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