The necessary steps include the following:
- 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;
- 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
}
- 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();
- 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();
- Execute the code. You will get the following output:
/VBP/NN/VB