如何将String标记转换为CoreLabel实例(StanfordNLP)?

时间:2015-11-20 13:06:12

标签: nlp stanford-nlp text-mining

我们可以将String标记转换为CoreLabel实例吗?

到目前为止,我正在使用:

CoreLabelTokenFactory c = new CoreLabelTokenFactory();  
CoreLabel tokens = c.makeToken("going",0,"going".length());

字符串被转换,但是使用这种方法,CoreLabel无法找到引理和pos。

1 个答案:

答案 0 :(得分:0)

以下是一些示例代码,用于演示从原始String到Annotation对象:

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.ling.CoreAnnotations.*;
import edu.stanford.nlp.util.*;

public class TokenizeExample {

    public static void main (String[] args) throws IOException {
        String text = "Here is a sentence.  Here is another sentence.";
        Annotation document = new Annotation(text);
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        pipeline.annotate(document);
        for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
            for (CoreLabel cl : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
                System.out.println("---");
                System.out.println(cl);
                System.out.println(cl.get(CoreAnnotations.PartOfSpeechAnnotation.class));
                System.out.println(cl.get(CoreAnnotations.LemmaAnnotation.class));
            }
        }
    }
}

确保从这里获得Stanford CoreNLP 3.5.2:http://nlp.stanford.edu/software/corenlp.shtml