Java Reference
In-Depth Information
• Add a new tag dictionary to a new tagger factory
As with the previous example, we will use a try-with-resources block to open our input
streams for the POS model and then create our model and tagger factory, as shown here:
try (InputStream modelIn = new FileInputStream(
new File(getModelDir(), "en-pos-maxent.bin"));) {
POSModel model = new POSModel(modelIn);
POSTaggerFactory posTaggerFactory = model.getFactory();
…
} catch (IOException e) {
//Handle exceptions
}
Obtaining the tag dictionary for a tagger
We used the
POSModel
class'
getFactory
method to get a
POSTaggerFactory
in-
stance. We will use its
getTagDictionary
method to obtain its
TagDictionary
instance. This is illustrated here:
MutableTagDictionary tagDictionary =
(MutableTagDictionary)posTaggerFactory.getTagDictionary();
The
MutableTagDictionary
interface extends the
TagDictionary
interface. The
TagDictionary
interface possesses a
getTags
method, and the
MutableTagDic-
tionary
interface adds a
put
method that allows tags to be added to the dictionary.
These interfaces are implemented by the
POSDictionary
class.
Determining a word's tags
To obtain the tags for a given word, use the
getTags
method. This returns an array of
tags represented by strings. The tags are then displayed as shown here:
String tags[] = tagDictionary.getTags("force");
for (String tag : tags) {
System.out.print("/" + tag);
}
System.out.println();
The output is as follows: