简体   繁体   中英

Exception while calling Parser method outside main class

In my application I have a method which I cant execute without main method. It only runs inside the main method. When I call that method inside my servlet class. It show an exception

  1. My class with Main Method
    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);

    }
}

It gives me below output

List of Noun Parse : [barren,, world, Moon]

Then I commented the main method and. Called the ParserAction() method in my servlet class

if (name.equals("bkDescription")) {

                    bookDes = value;

                    try {

                        new ParserTest().parserAction(); 

                        System.out.println("Nouns Are"+ParserTest.nounPhrases);

                    } catch (Exception e) {

                    }

It gives me the below exceptions

例外

And below error in my Browser

浏览器错误

Why is this happening ? I can run this with main method. But when I remove main method and called in my servlet. it gives an exception. Is there any way to fix this issue ?

NOTE - I have read below instructions in OpenNLP documentation , but I have no clear idea about it. Please help me to fix his issue.

Unlike the other components to instantiate the Parser a factory method should be used instead of creating the Parser via the new operator. The parser model is either trained for the chunking parser or the tree insert parser the parser implementation must be chosen correctly. The factory method will read a type parameter from the model and create an instance of the corresponding parser implementation.

创建ParserTest类的对象或删除这一行的new ParserTest().parserAction();关键字new ParserTest().parserAction();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM