2016-07-15 6 views
0

Я пробовал следующий код, однако код не работает и выводит только null.Как использовать StanfordNLP китайский сегмент в Java?

String text = "我爱北京天安门。"; 
StanfordCoreNLP pipeline = new StanfordCoreNLP(); 
Annotation annotation = pipeline.process(text); 
String result = annotation.get(CoreAnnotations.ChineseSegAnnotation.class); 
System.out.println(result); 

Результат:

... 
done [0.6 sec]. 
Using mention detector type: rule 
null 

Как использовать StanfordNLP китайский segmentor правильно?

ответ

0

Некоторые примеры кода:

import edu.stanford.nlp.pipeline.*; 
import edu.stanford.nlp.ling.CoreAnnotations; 
import edu.stanford.nlp.ling.CoreLabel; 
import edu.stanford.nlp.util.StringUtils; 

import java.util.*; 

public class ChineseSegmenter { 

    public static void main (String[] args) { 
     // set the properties to the standard Chinese pipeline properties 
     Properties props = StringUtils.argsToProperties("-props", "StanfordCoreNLP-chinese.properties"); 
     StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
     String text = "..."; 
     Annotation annotation = new Annotation(text); 
     pipeline.annotate(annotation); 
     List<CoreLabel> tokens = annotation.get(CoreAnnotations.TokensAnnotation.class); 
     for (CoreLabel token : tokens) 
      System.out.println(token); 
    } 
} 

Примечание: Убедитесь, что китайские модели банка на вашем CLASSPATH. Этот файл можно найти здесь: http://stanfordnlp.github.io/CoreNLP/download.html

Приведенный выше код должен распечатывать маркеры, созданные после запуска китайского сегментатора.