Advertisement
Guest User

Untitled

a guest
Oct 21st, 2013
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using java.util;
  7. using java.io;
  8. using edu.stanford.nlp.io;
  9. using edu.stanford.nlp.trees;
  10. using edu.stanford.nlp.pipeline;
  11. using edu.stanford.nlp.util;
  12. using edu.stanford.nlp.ling;
  13. using java.lang;
  14.  
  15.  
  16. namespace ConsoleApplication1
  17. {
  18.     class Program
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.             // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
  23.             Properties props = new Properties();
  24.             props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse");
  25.  
  26.  
  27.             props.put("pos.model", @"****\stanford-postagger-2013-06-20\models\wsj-0-18-bidirectional-nodistsim.tagger");
  28.             props.put("ner.model", "****\\stanford-ner-2013-06-20\\classifiers\\english.all.3class.distsim.crf.ser.gz");
  29.            
  30.             props.put("parse.model", "****\\englishPCFG.ser.gz");
  31.             props.put("sutime.binders", "0"); // bugfix for NER
  32.             System.String SuTimePath = "****\\stanford-corenlp-full-2013-06-20\\sutime\\defs.sutime.txt";
  33.             System.String SuTimeEnglishPath = "****\\stanford-corenlp-full-2013-06-20\\sutime\\english.sutime.txt";
  34.              props.put("sutime.rules", System.String.Format("{0}, {1}", SuTimePath, SuTimeEnglishPath));
  35.  
  36.  
  37.  
  38. /*   Attempt to get dcoref working. Here are some lines that you probably need, but not all of them.
  39. If you want to try out "dcoref", use
  40.     props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
  41. instead. Also add
  42.  
  43.              props.put("dcoref.demonym", "****\\demonyms.txt");
  44.              props.put("dcoref.animate", "****\\animate.unigrams.txt");
  45.              props.put("dcoref.states", "****\\state-abbreviations.txt");
  46.              props.put("dcoref.female", "****\\female.unigrams.txt");
  47.              props.put("dcoref.inanimate", "****\\inanimate.unigrams.txt");
  48.              props.put("dcoref.male", "****\\male.unigrams.txt");
  49.              props.put("dcoref.neutral", "****\\neutral.unigrams.txt");
  50.              props.put("dcoref.plural", "****\\plural.unigrams.txt");
  51.              props.put("dcoref.singular", "****\\singular.unigrams.txt");
  52.              props.put("dcoref.extra.gender", "****\\namegender.combine.txt");
  53.  
  54.  
  55.  
  56.  
  57. */
  58.  
  59.              StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
  60.  
  61.  
  62.             // read some text in the text variable
  63.             System.String text = "This is my text."; // Add your text here!
  64.  
  65.             text = System.IO.File.ReadAllText(@"c:\yourfile.txt");
  66.  
  67.             // create an empty Annotation just with the given text
  68.             Annotation document = new Annotation(text);
  69.  
  70.             // run all Annotators on this text
  71.             pipeline.annotate(document);
  72.  
  73.             // these are all the sentences in this document
  74.             // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
  75.  
  76.             CoreAnnotations.SentencesAnnotation CASA = new CoreAnnotations.SentencesAnnotation();
  77.             Class cCASA = CASA.getClass();
  78.  
  79.             ArrayList myArrayList = (ArrayList)document.get(cCASA);
  80.             List<CoreMap> sentences = new List<CoreMap>(myArrayList.size());
  81.             foreach (CoreMap element in myArrayList)
  82.             {
  83.                 sentences.Add(element);
  84.             }
  85.  
  86.  
  87.             foreach (CoreMap sentence in sentences)
  88.             {
  89.                 System.Console.WriteLine(sentence.ToString());
  90.                 CoreAnnotations.TokensAnnotation CATA = new CoreAnnotations.TokensAnnotation();
  91.                 Class cCATA = CATA.getClass();
  92.                 ArrayList myArrayListForcTA = (ArrayList)sentence.get(cCATA);
  93.                 List<CoreLabel> token_list = new List<CoreLabel>(myArrayListForcTA.size());
  94.                 foreach (CoreLabel element in myArrayListForcTA)
  95.                 {
  96.                     token_list.Add(element);
  97.                 }
  98.                 foreach (CoreLabel token in token_list)
  99.                 {
  100.                     // this is the text of the token
  101.                     CoreAnnotations.TextAnnotation TexA = new CoreAnnotations.TextAnnotation();
  102.                     Class cTexA = TexA.getClass();
  103.                     string word = (string)token.get(cTexA);
  104.                     System.Console.WriteLine(word);
  105.                     // this is the POS tag of the token
  106.                     CoreAnnotations.PartOfSpeechAnnotation PoSA = new CoreAnnotations.PartOfSpeechAnnotation();
  107.                     Class cPoSA = PoSA.getClass();
  108.                     string pos = (string)token.get(cPoSA);
  109.                     System.Console.WriteLine(pos);
  110.                     // this is the NER label of the token
  111.                     CoreAnnotations.NamedEntityTagAnnotation NETA = new CoreAnnotations.NamedEntityTagAnnotation();
  112.                     Class cNETA = NETA.getClass();
  113.                     string ne = (string)token.get(cNETA);
  114.                     System.Console.WriteLine(ne);
  115.                 }
  116.             }
  117.  
  118.             System.Console.ReadKey();
  119.         }
  120.        
  121.     }
  122. }
Advertisement
RAW Paste Data Copied
Advertisement