不兼容的类型:对象不能转换为CoreLabel
问题描述:
我试图使用斯坦福标记者从他们的网站下面的例子:不兼容的类型:对象不能转换为CoreLabel
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.process.CoreLabelTokenFactory;
import edu.stanford.nlp.process.DocumentPreprocessor;
import edu.stanford.nlp.process.PTBTokenizer;
public class TokenizerDemo {
public static void main(String[] args) throws IOException {
for (String arg : args) {
// option #1: By sentence.
DocumentPreprocessor dp = new DocumentPreprocessor(arg);
for (List sentence : dp) {
System.out.println(sentence);
}
// option #2: By token
PTBTokenizer ptbt = new PTBTokenizer(new FileReader(arg),
new CoreLabelTokenFactory(), "");
for (CoreLabel label; ptbt.hasNext();) {
label = ptbt.next();
System.out.println(label);
}
}
}
}
,我得到以下错误,当我尝试编译:
TokenizerDemo.java:24: error: incompatible types: Object cannot be converted to CoreLabel
label = ptbt.next();
有谁知道可能的原因是什么?如果您有兴趣,我使用Java 1.8并确保CLASSPATH包含jar文件。
答
尝试参数化PTBTokenizer
类。例如:
PTBTokenizer<CoreLabel> ptbt = new PTBTokenizer<>(new FileReader(arg),
new CoreLabelTokenFactory(), "");
谢谢!这工作:) –