Advertisement
Guest User

Untitled

a guest
Oct 18th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  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, dcoref");
  25.             props.put("annotators", "tokenize, ssplit, pos, lemma");
  26.  
  27.             //props.setProperty("sighanCorporaDict", @"..\..\..\..\temp\stanford-segmenter-2013-06-20\data");
  28.             //props.setProperty("serDictionary", @"..\..\..\..\temp\stanford-segmenter-2013-06-20\data\dict-chris6.ser.gz");
  29.             props.put("pos.model", @"PATH\stanford-postagger-2013-06-20\models\wsj-0-18-bidirectional-nodistsim.tagger");
  30.             props.put("ner.model", "PATH\\stanford-ner-2013-06-20\\classifiers\\english.all.3class.distsim.crf.ser.gz");
  31.            
  32.                 //"parse.model"
  33.            
  34.             StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
  35.  
  36.  
  37.             // read some text in the text variable
  38.             System.String text = "This is my text."; // Add your text here!
  39.  
  40.             // create an empty Annotation just with the given text
  41.             Annotation document = new Annotation(text);
  42.  
  43.             // run all Annotators on this text
  44.             pipeline.annotate(document);
  45.  
  46.             // these are all the sentences in this document
  47.             // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
  48.  
  49.             CoreAnnotations.SentencesAnnotation CASA = new CoreAnnotations.SentencesAnnotation();
  50.             Class cCASA = CASA.getClass();
  51.  
  52.             ArrayList myArrayList = (ArrayList)document.get(cCASA);
  53.             List<CoreMap> sentences = new List<CoreMap>(myArrayList.size());
  54.             foreach (CoreMap element in myArrayList)
  55.             {
  56.                 sentences.Add(element);
  57.             }
  58.  
  59.  
  60.             foreach (CoreMap sentence in sentences)
  61.             {
  62.                 CoreAnnotations.TokensAnnotation CATA = new CoreAnnotations.TokensAnnotation();
  63.                 Class cCATA = CATA.getClass();
  64.                 ArrayList myArrayListForcTA = (ArrayList)sentence.get(cCATA);
  65.                 List<CoreLabel> token_list = new List<CoreLabel>(myArrayListForcTA.size());
  66.                 foreach (CoreLabel element in myArrayListForcTA)
  67.                 {
  68.                     token_list.Add(element);
  69.                 }
  70.                 foreach (CoreLabel token in token_list)
  71.                 {
  72.                     // this is the text of the token
  73.                     CoreAnnotations.TextAnnotation TexA = new CoreAnnotations.TextAnnotation();
  74.                     Class cTexA = TexA.getClass();
  75.                     string word = (string)token.get(cTexA);
  76.                     System.Console.WriteLine(word);
  77.                     // this is the POS tag of the token
  78.                     CoreAnnotations.PartOfSpeechAnnotation PoSA = new CoreAnnotations.PartOfSpeechAnnotation();
  79.                     Class cPoSA = PoSA.getClass();
  80.                     string pos = (string)token.get(cPoSA);
  81.                     System.Console.WriteLine(pos);
  82.                     // this is the NER label of the token
  83.                     CoreAnnotations.NamedEntityTagAnnotation NETA = new CoreAnnotations.NamedEntityTagAnnotation();
  84.                     Class cNETA = NETA.getClass();
  85.                     string ne = (string)token.get(cNETA);
  86.                     System.Console.WriteLine(ne);
  87.                 }
  88.             }
  89.  
  90.             System.Console.ReadKey();
  91.         }
  92.        
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement