how to detect dates with openNLP

2.8k Views Asked by At

I want to detect dates on a sentence by using openNLP. It's easy to use id there is a existing model other than training by myslft. Can anyone please guide me how to do it in java.

1

There are 1 best solutions below

0
On
  • First download Date name finder model from here (English version- en tag)
  • Also download Tokenizer model. Because input for Name Finder Model should be tokenized array
  • Add them into project folder or somewhere else

Load both model

InputStream modelIn1 = new FileInputStream("en-ner-date.bin");
InputStream modelIn2 = new FileInputStream("en-token.bin");

Create Tokenizer model and NamefinderModel

try {
  TokenizerModel model1 = new TokenizerModel(modelIn2);
  TokenNameFinderModel model2 = new TokenNameFinderModel(modelIn1);
}
catch (IOException e) {
  e.printStackTrace();
}
finally {
  if (modelIn1 != null) {
    try {
      modelIn1.close();
    }
    catch (IOException e) {
    }
  }
  if (modelIn2 != null) {
    try {
      modelIn2.close();
    }
    catch (IOException e) {
    }
  }
}

Then Load TokenizerME and NameFinderME

Tokenizer tokenizer = new TokenizerME(model1);
NameFinderME nameFinder = new NameFinderME(model2);

Then get tokens from tokenizer model

String tokens[] = tokenizer.tokenize("An input sample sentence.");

Then send this token array as a parameter to namefinder model

Span nameSpans[] = nameFinder.find(tokens);

This will convert it to array of predicted dates as string

String[] array=Span.spansToStrings(nameSpans,tokens);