我正在尝试使用 Apache OpenNLP POSTagger 示例代码,但出现错误,下面是代码
public String[] SentenceDetect(String qwe) throws IOException
{
POSModel model = new POSModelLoader().load(new File("/home/jebard/chabacano/Chabacano1/src/en-pos-maxent.bin"));
PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent");
POSTaggerME tagger = new POSTaggerME(model);
String input = "Hi. How are you? This is Mike.";
ObjectStream<String> lineStream = new PlainTextByLineStream(
new StringReader(input));
perfMon.start();
String line;
while ((line = lineStream.read()) != null) {
String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE
.tokenize(line);
String[] tags = tagger.tag(whitespaceTokenizerLine);
POSSample sample = new POSSample(whitespaceTokenizerLine, tags);
System.out.println(sample.toString());
perfMon.incrementCounter();
}
perfMon.stopAndPrintFinalResult();
这行错误
.load(new File("/home/jebard/chabacano/Chabacano1/src/en-pos-maxent.bin")
ModelLoader 类型中的方法加载(java.io.File)不适用于参数(org.apache.tomcat.jni.File)
最佳答案
这实际上不是 OpenNLP 中的错误。这是您代码中的错误,因为您从包(又名命名空间)org.apache.tomcat.jni.File
加载类 File
。
然而,API of OpenNLP要求您使用标准 JDK 包 java.io
中的类 File
,即您应该导入 java.io.File
相反。
一般来说,这应该可以解决您的问题。
重要提示
您应该迁移您的代码,因为不应通过 POSModelLoader
加载模型
Loads a POS Tagger Model for the command line tools.
Note: Do not use this class, internal use only!
相反,您可以使用构造函数 POSModel(InputStream in)
通过引用实际模型文件的 InputStream
加载您的模型文件。
此外,POSModelLoader
类仅出现在以前的 OpenNLP 版本中(版本 <= 1.5.x)。在最新的 OpenNLP 版本 1.6.0 中,它被完全删除了。相反,您现在可以而且应该使用 POSModel class 的构造函数加载/初始化您需要的模型。
关于java - Apache OpenNLP 错误它不加载 en-pos-maxent.bin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28314118/