0
當我嘗試調用另一個servlet類中的parserAction()方法時,我得到一個空白數組。我無法在我的servlet中打印名詞。但是這個類中MAIN METHOD名詞數組打印正確。這是什麼原因?執行分析器的方法沒有主要方法
package com.books.servlet;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import com.sun.corba.se.impl.orb.ParserAction;
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 {
public static Set<String> nounPhrases = new HashSet<>();
public String line = "I need the java book";
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());
// System.out.println(p.getCoveredText());
}
for (Parse child : p.getChildren()) {
getNounPhrases(child);
}
}
public void parserAction() throws Exception {
InputStream is = new FileInputStream("en-parser-chunking.bin");
ParserModel model = new ParserModel(is);
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);
}
}
下面是我的示例servlet類。它向我展示了一個空白數組[
public class test extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
.......
.......
.......
ParserTest pt = new ParserTest();
pt.parserAction();
System.out.println("List of Noun Parse : "+pt.nounPhrases);
System.out.println("List of Noun Parse : "+ParserTest.nounPhrases);
}
}
這裏我需要提取名詞而不執行main方法。由於我正在開發一個Web應用程序。我需要在我的一個servlet類中顯示這些提取的名詞。
它只給出輸出[]。爲什麼解析器方法沒有執行?當我用主要方法嘗試它時,它工作正常......但在Servlet中它並未執行 – user8048032
嘗試並調試您的代碼,我無法猜測ParserTool/ParserModel正在執行的任何操作。我猜你沒有找到文件「en-parser-chunking.bin」,因爲當你使用容器啓動你的web應用程序時,當前目錄是不同的,並且你的代碼在這種情況下不會崩潰可能應該。 –