How to do it...

The necessary steps include the following:

  1. Add the following imports to the project:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import opennlp.tools.postag.MutableTagDictionary;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerFactory;
import opennlp.tools.postag.TagDictionary;
  1. Add the following try-with-resources block to the main method:
try (InputStream modelInputStream = new FileInputStream(
"en-pos-maxent.bin");) {
...
} catch (IOException e) {
// Handle exceptions
}
  1. Insert these statements into the try block to create a tag dictionary:
POSModel posModel = new POSModel(modelInputStream);
POSTaggerFactory posTaggerFactory = posModel.getFactory();
MutableTagDictionary mutableTagDictionary = (MutableTagDictionary) posTaggerFactory.getTagDictionary();
  1. Add the next sequence to display the existing tags for the word process:
String currentTags[] = mutableTagDictionary.getTags("process");
for (String tag : currentTags) {
System.out.print("/" + tag);
}
System.out.println();
  1. Execute the code. You will get the following output:
/VBP/NN/VB
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.105.227