Java Reference
In-Depth Information
Processing multiple entity types
We can also handle multiple entity types at the same time. This involves creating in-
stances of the NameFinderME class based on each model within a loop and applying the
model against each sentence, keeping track of the entities as they are found.
We will illustrate this process with the following example. It requires rewriting the previ-
ous try block to create the InputStream instance within the block, as shown here:
try {
InputStream tokenStream = new FileInputStream(
new File(getModelDir(), "en-token.bin"));
TokenizerModel tokenModel = new
TokenizerModel(tokenStream);
Tokenizer tokenizer = new TokenizerME(tokenModel);
...
} catch (Exception ex) {
// Handle exceptions
}
Within the try block, we will define a string array to hold the names of the model files.
As shown here, we will use models for people, locations, and organizations:
String modelNames[] = {"en-ner-person.bin",
"en-ner-location.bin", "en-ner-organization.bin"};
An ArrayList instance is created to hold the entities as they are discovered:
ArrayList<String> list = new ArrayList();
A for-each statement is used to load one model at a time and then to create an instance of
the NameFinderME class:
for(String name : modelNames) {
TokenNameFinderModel entityModel = new
TokenNameFinderModel(
new FileInputStream(new File(getModelDir(), name)));
NameFinderME nameFinder = new NameFinderME(entityModel);
Search WWH ::




Custom Search