使用Stanford CorNLP手动标记单词

时间:2015-02-25 16:19:44

标签: java-7 stanford-nlp lemmatization

我有一个资源,我确切地知道单词的类型。我必须对它们进行引理,但为了获得正确的结果,我必须手动标记它们。我找不到任何手动标记单词的代码。我使用以下代码,但它返回错误的结果。即#34;绘画"为'"绘画"我期待的地方" paint"。

*//...........lemmatization starts........................

Properties props = new Properties(); 
props.put("annotators", "tokenize, ssplit, pos, lemma"); 
StanfordCoreNLP pipeline = new StanfordCoreNLP(props, false);
String text = "painting"; 
Annotation document = pipeline.process(text);  

List<edu.stanford.nlp.util.CoreMap> sentences = document.get(SentencesAnnotation.class);

for(edu.stanford.nlp.util.CoreMap sentence: sentences) 

{    
    for(CoreLabel token: sentence.get(TokensAnnotation.class))
    {       
        String word = token.get(TextAnnotation.class);      
        String lemma = token.get(LemmaAnnotation.class); 
        System.out.println("lemmatized version :" + lemma);
    }
}

//...........lemmatization ends.........................*

我必须在单词上运行lemmatizer而不是自动完成pos标记的句子。所以我先手动标记这些单词,然后找到它们的引理。帮助一些示例代码或对某个站点的引用会有很大的帮助。

1 个答案:

答案 0 :(得分:1)

如果您事先知道POS标签,可以通过以下方式获取结构标记:

Properties props = new Properties(); 
props.put("annotators", "tokenize, ssplit"); 
StanfordCoreNLP pipeline = new StanfordCoreNLP(props, false);
String text = "painting";

Morphology morphology = new Morphology();

Annotation document = pipeline.process(text);  

List<edu.stanford.nlp.util.CoreMap> sentences = document.get(SentencesAnnotation.class);

for(edu.stanford.nlp.util.CoreMap sentence: sentences) {

  for(CoreLabel token: sentence.get(TokensAnnotation.class)) {       
    String word = token.get(TextAnnotation.class);
    String tag = ... //get the tag for the current word from somewhere, e.g. an array
    String lemma = morphology.lemma(word, tag);
    System.out.println("lemmatized version :" + lemma);
  }
}

如果您只想获得单个单词的引理,您甚至不必运行CoreNLP进行标记化和句子分割,因此您可以将引理函数调用如下:

String tag = "VBG";      
String word = "painting";
Morphology morphology = new Morphology();
String lemma = morphology.lemma(word, tag);