Java 在主类外部调用解析器方法时发生异常
在我的应用程序中,我有一个没有main方法就无法执行的方法。它只在main方法内部运行。当我在servlet类中调用该方法时。它显示了一个例外Java 在主类外部调用解析器方法时发生异常,java,exception,servlets,model,nlp,Java,Exception,Servlets,Model,Nlp,在我的应用程序中,我有一个没有main方法就无法执行的方法。它只在main方法内部运行。当我在servlet类中调用该方法时。它显示了一个例外 我用主要的方法教我的课 它给了我以下例外 下面是我的浏览器中的错误 为什么会这样?我可以用main方法运行它。但当我删除main方法并调用servlet时。它给出了一个例外。有没有办法解决这个问题 注意-我已经阅读了OpenNLP文档中的以下说明,但我对此没有明确的概念。请帮我解决他的问题 与实例化解析器的其他组件不同,它使用工厂方法 应该使用,而不
创建ParserTest类的对象或删除此行中的new关键字
new ParserTest().parserAction()代码>
package com.books.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.HashSet;
import java.util.Set;
import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;
public class ParserTest {
// download
public void download(String url, File destination) throws IOException, Exception {
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
public static Set<String> nounPhrases = new HashSet<>();
private static String line = "The Moon is a barren, rocky world ";
public void getNounPhrases(Parse p) {
if (p.getType().equals("NN") || p.getType().equals("NNS") || p.getType().equals("NNP")
|| p.getType().equals("NNPS")) {
nounPhrases.add(p.getCoveredText());
}
for (Parse child : p.getChildren()) {
getNounPhrases(child);
}
}
public void parserAction() throws Exception {
// InputStream is = new FileInputStream("en-parser-chunking.bin");
File modelFile = new File("en-parser-chunking.bin");
if (!modelFile.exists()) {
System.out.println("Downloading model.");
download("https://drive.google.com/uc?export=download&id=0B4uQtYVPbChrY2ZIWmpRQ1FSVVk", modelFile);
}
ParserModel model = new ParserModel(modelFile);
Parser parser = ParserFactory.create(model);
Parse topParses[] = ParserTool.parseLine(line, parser, 1);
for (Parse p : topParses) {
// p.show();
getNounPhrases(p);
}
}
public static void main(String[] args) throws Exception {
new ParserTest().parserAction();
System.out.println("List of Noun Parse : " + nounPhrases);
}
}
if (name.equals("bkDescription")) {
bookDes = value;
try {
new ParserTest().parserAction();
System.out.println("Nouns Are"+ParserTest.nounPhrases);
} catch (Exception e) {
}