OpenNLP Parsing the Sentences - OpenNLP

How to parse raw text using OpenNLP API?

This chapter explains about how to parse raw text using OpenNLP API.

Parsing Raw Text using OpenNLP Library

OpenNLP uses a predefined model with a file named en-parserchunking.bin to detect sentences. You can use this predefined to parse the given raw text.

The Parser class of the package is mainly used to hold the parse constituents and the ParserTool class of the package to parse the content.

Below mentioned steps are used to be followed to write a program which parses the given raw text using the ParserTool class.

Step 1: Loading the model

The model for parsing text is highlighted by the class named ParserModel, which belongs to the package

To load a tokenizer model −

  • Create an InputStream object of the model (Instantiate the FileInputStream and pass the path of the model in String format to its constructor).
  • Instantiate the ParserModel class and pass the InputStream (object) of the model as a parameter to its constructor, as shown in the following code block.

Step 2: Creating an object of the Parser class

The Parser class of the package presents the data structure to capture the parse constituents. Now you can design an object of this class with the help of the static create()method of the ParserFactory class.

You can invoke the create() method of the ParserFactory by holding the model object created in the previous step, as mentioned below –

Step 3: Parsing the sentence

The parseLine() method of the ParserTool class is mainly used to parse the raw text in OpenNLP. This method accepts −

  • a String variable representing the text to be parsed.
  • a parser object.
  • an integer representing the number of parses to be carried out.

Execute this method by passing the sentence with the following parameters like the parse object and an integer representing the required number of parses to be carried out.


Below mentioned program explains about which parses the given raw text. Save this program in a file with the name

Let’s compile and execute the saved Java file from the Command prompt by using the following commands –

Upon executing, the above code reads the given raw text, parses it, and provides below output –

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

OpenNLP Topics