任何想法为什么会发生,我正在使用Java版本10.0.1
public void POSTag() throws IOException {
POSModel model = new POSModelLoader().load(new File("lib/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();
}
在编译上面的程序时,我遇到了错误
像这样
构造函数PlainTextByLineStream(StringReader)未定义。
消息的原因是什么,如何解决?
最佳答案
根据PlainTextByLineStream类的官方OpenNLP JavaDoc,存在两个构造函数。PlainTextByLineStream(InputStreamFactory inputStreamFactory, Charset charset)
PlainTextByLineStream(InputStreamFactory inputStreamFactory, String charsetName)
可以看出,没有诸如PlainTextByLineStream(StringReader reader)
这样的构造函数。
但是,在您的代码中,可以按以下方式使用现有的构造函数之一:
// ...
Charset charset = Charset.forName("UTF-8");
InputStreamFactory isf = new MarkableFileInputStreamFactory(new File("myText.txt"));
ObjectStream<String> lineStream = new PlainTextByLineStream(isf, charset);
// ...
while ((line = lineStream.read()) != null) {
// process
}
请注意,您必须提供对提供文本内容的
File
(此处为:“ mytext.txt”)的引用。希望能帮助到你。