2012-07-03 49 views
1

有人可以請我提供斯坦福CoreNLP的Java實現將文本文件轉換爲XML文件。同樣的事情,我可以用斯坦福CoreNLP-文本到XML

java -cp stanford-corenlp-2012-05-22.jar;stanford-corenlp-2012-05-22-models.jar;xom.jar;joda-time.jar -Xmx3g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner,parse,dcoref -file input.txt 

在命令行中。

+0

的源代碼或做它在Java應用程序? –

+0

用於將input.txt轉換爲input.txt.xml的源代碼。執行與我發佈的命令相同的任務的代碼在命令行中運行時會執行相同的任務。就像在Java程序中使用斯坦福CoreNLP API來做到這一點一樣,或者可以通過其他方式來實現目的。 – agarwav

回答

2

在java中你可以這樣調用它:

import edu.stanford.nlp.pipeline.StanfordCoreNLP; 

...

StanfordCoreNLP.main(new String[] { 
    "-annotators", "tokenize,ssplit,pos,lemma,ner,parse,dcoref", 
    "-file", "input.txt" }); 

(如果這個就足夠了)

+0

非常感謝:D這工作:D – agarwav

4

喬普的回答肯定的作品,但如果你想要深入一點,而不是將主要方法用作API,下面是一個完整的示例,顯示了將XML語句分析寫入文件。

import java.io.*; 
import java.util.*; 

import edu.stanford.nlp.io.*; 
import edu.stanford.nlp.ling.*; 
import edu.stanford.nlp.pipeline.*; 
import edu.stanford.nlp.trees.*; 
import edu.stanford.nlp.util.*; 

public class StanfordCoreNlpDemo { 

    public static void main(String[] args) throws IOException { 
    PrintWriter out; 
    if (args.length > 1) { 
     out = new PrintWriter(args[1]); 
    } else { 
     out = new PrintWriter(System.out); 
    } 
    PrintWriter xmlOut = null; 
    if (args.length > 2) { 
     xmlOut = new PrintWriter(args[2]); 
    } 

    StanfordCoreNLP pipeline = new StanfordCoreNLP(); 
    Annotation annotation; 
    if (args.length > 0) { 
     annotation = new Annotation(IOUtils.slurpFileNoExceptions(args[0])); 
    } else { 
     annotation = new Annotation("Kosgi Santosh sent an email to Stanford University. He didn't get a reply."); 
    } 

    pipeline.annotate(annotation); 
    pipeline.prettyPrint(annotation, out); 
    if (xmlOut != null) { 
     pipeline.xmlPrint(annotation, xmlOut); 
    } 
    // An Annotation is a Map and you can get and use the various analyses individually. 
    // For instance, this gets the parse tree of the first sentence in the text. 
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class); 
    if (sentences != null && sentences.size() > 0) { 
     CoreMap sentence = sentences.get(0); 
     Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); 
     out.println(); 
     out.println("The first sentence parsed is:"); 
     tree.pennPrint(out); 
    } 
    } 

} 
+0

不知何故,這不再工作,或者我做錯了什麼。 「StanfordCoreNLP」的導入不再起作用,因爲它似乎不在'* .pipeline。*'包中。但是我在項目中的其他任何地方都找不到它。 – Ogofo

+0

仍適用於我! (使用目前的v3.3.1。)也許你的CLASSPATH不正確? –