javatomcatservletsopennlpresource-loading

Why does the loading of a POSModel file not work from inside the WEB-INF folder?


I'm using Spring MVC for my web project. I placed the model files inside the WEB-INF directory

String taggerModelPath = "/WEB-INF/lib/en-pos-maxent.bin";
String chunkerModelPath = "/WEB-INF/lib/en-chunker.bin";

POSModel model = new POSModelLoader()
.load(new File(servletContext.getResource(taggerModelPath).toURI().getPath()));

This worked Windows environment. However when I deployed it on my remote Linux server I got an error

HTTP Status 500 - Request processing failed; nested exception is opennlp.tools.cmdline.TerminateToolException: The POS Tagger model file does not exist! Path: /localhost/nlp/WEB-INF/lib/en-pos-maxent.bin

What is the best way to access file resources? Thanks


Solution

  • Let's assume you're using OpenNLP 1.5.3, then you should use another way of loading resource files, which uses no "hard" path references via URI conversion.

    Given an environment in which within the directory WEB-INF another directory resources exists that contains your OpenNLP model files, you're code fragment should be written as follows:

    String taggerModelPath = "/WEB-INF/resources/en-pos-maxent.bin";
    String chunkerModelPath= "/WEB-INF/resources/en-chunker.bin";
    
    POSModel model = new POSModelLoader().load(servletContext.getResourceAsStream(taggerModelPath));
    

    See Javadoc for ServletContext#getResourceAsStream and this StackOverflow post.

    Important Hint

    Sadly, there are other issues with your code. The OpenNLP class POSModelLoader is only for internal use, see official Javadoc for POSModelLoader:

    Loads a POS Tagger Model for the command line tools.

    Note: Do not use this class, internal use only!

    Therefore, loading a POSModel in a Web context should be done differently: Via one of the available constructors of that class. You can reformulate the above code fragment like so:

    try {
        InputStream in = servletContext.getResourceAsStream(taggerModelPath);
        POSModel posModel;
        if(in != null) {
            posModel = new POSModel(in);
            
            // from here, <posModel> is initialized and you can start playing with it...
            // ...
        }
        else {
            // resource file not found - whatever you want to do in this case
        }
    }
    catch (IOException | InvalidFormatException ex) {
        // proper exception handling here... cause: getResourcesAsStream or OpenNLP...
    } 
    

    This way, you're compliant with the OpenNLP API and at the same time you do proper exception handling. Moreover, you can now use a Debugger in case something remains unclear with the resource path references of your model files.

    Hope it helps.