public void Analysis(string path) { var document = this.ReadAnnotation(path); Properties props = new Properties(); //props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,relation,,mention,coref"); //var modelsDirectory = jarRoot ; // Loading POS Tagger //String modPath = @"D:\Tesis2016\Jarvis\Lincoln\Models\"; //props.put("pos.model", modPath + @"pos-tagger\english-bidirectional-distsim.tagger"); //props.put("ner.model", modPath + "ner/english.all.3class.distsim.crf.ser.gz"); //props.put("parse.model", modPath + "lexparser/englishPCFG.ser.gz"); //props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); //props.put("sutime.binders", "0"); //props.put("sutime.rules", modPath + "sutime/defs.sutime.txt, " + modPath + "sutime/english.sutime.txt"); //props.put("dcoref.demonym", modPath + "dcoref/demonyms.txt"); //props.put("dcoref.states", modPath + "dcoref/state-abbreviations.txt"); //props.put("dcoref.animate", modPath + "dcoref/animate.unigrams.txt"); //props.put("dcoref.inanimate", modPath + "dcoref/inanimate.unigrams.txt"); //props.put("dcoref.big.gender.number", modPath + "dcoref/gender.data.gz"); //props.put("dcoref.countries", modPath + "dcoref/countries"); //props.put("dcoref.states.provinces", modPath + "dcoref/statesandprovinces"); //props.put("dcoref.singleton.model", modPath + "dcoref/singleton.predictor.ser"); //props.put("ner.useSUTime", "0"); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); props.setProperty("sutime.binders", "0"); props.setProperty("ner.useSUTime", "false"); var jarRoot = @"D:\Tesis2016\Jarvis\Lincoln\Models"; var curDir = Environment.CurrentDirectory; System.IO.Directory.SetCurrentDirectory(jarRoot); var pipeline = new StanfordCoreNLP(props); pipeline.annotate(document); System.IO.Directory.SetCurrentDirectory(curDir); FileOutputStream os = new FileOutputStream(new File("coreference_output.xml")); pipeline.xmlPrint(document, os); }
//使用nlp將文章分析後回傳key private List<string> nlp(string sentence) { List<string> return_key = new List<string>(); string Relay_file = ".\\xml"; string Relay_name = "Relay.xml"; string Relay_path = Relay_file+ "\\" + Relay_name; // Path to the folder with models extracted from `stanford-corenlp-3.4-models.jar` var jarRoot = @"stanford-corenlp-3.5.2-models\"; // Annotation pipeline configuration var props = new java.util.Properties(); props.setProperty("ner.useSUTime", "false"); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); props.setProperty("sutime.binders", "0"); // We should change current directory, so StanfordCoreNLP could find all the model files automatically var curDir = Environment.CurrentDirectory; System.IO.Directory.SetCurrentDirectory(jarRoot); var pipeline = new StanfordCoreNLP(props); System.IO.Directory.SetCurrentDirectory(curDir); // Annotation var annotation = new Annotation(sentence); pipeline.annotate(annotation); //輸出nlp分析結果至Relay.xml FileOutputStream os = new FileOutputStream(new File(Relay_file, Relay_name)); pipeline.xmlPrint(annotation, os); os.close(); //呼叫ner將單字組合為有意義的key組裝 foreach(string k in ner(Relay_path)) { return_key.Add(k); } return return_key; }