Java Reference
In-Depth Information
Using OpenNLP
Parsing text is simple using the ParserTool class. Its static parseLine method ac-
cepts three arguments and returns a Parser instance. These arguments are:
• A string containing the text to be parsed
• A Parser instance
• An integer specifying how many parses are to be returned
The Parser instance holds the elements of the parse. The parses are returned in order of
their probability. To create a Parser instance, we will use the ParserFactory class'
create method. This method uses a ParserModel instance that we will create using
the en-parser-chunking.bin file.
This process is shown here, where an input stream for the model file is created using a try-
with-resources block. The ParserModel instance is created followed by a Parser in-
stance:
String fileLocation = getModelDir() +
"/en-parser-chunking.bin";
try (InputStream modelInputStream =
new FileInputStream(fileLocation);) {
ParserModel model = new ParserModel(modelInputStream);
Parser parser = ParserFactory.create(model);
...
} catch (IOException ex) {
// Handle exceptions
}
We will use a simple sentence to demonstrate the parsing process. In the following code se-
quence, the parseLine method is invoked using a value of 3 for the third argument. This
will return the top three parses:
String sentence = "The cow jumped over the moon";
Parse parses[] = ParserTool.parseLine(sentence, parser, 3);
Next, the parses are displayed along with their probabilities, as shown here:
Search WWH ::




Custom Search