Skip to content

Instantly share code, notes, and snippets.

@zawhtutwin
Last active November 25, 2016 17:29
Show Gist options
  • Save zawhtutwin/13f0c13bf99c93c4af957890d74488b5 to your computer and use it in GitHub Desktop.
Save zawhtutwin/13f0c13bf99c93c4af957890d74488b5 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.Span;
public class NameFinderTest {
static String sentence = "Mya Mya is Shan. Mg Mg is Burmese. Hla Hla is Kachin.";
//opennlp TokenNameFinderTrainer -model mm-ner-log.bin -lang en -data cities.txt -encoding UTF-8
public static void main(String[] args) {
InputStream modelIn = null;
try {
InputStream is = new FileInputStream("E:\\opennlp\\bin\\en-token.bin");
TokenizerModel tmodel = new TokenizerModel(is);
Tokenizer tokenizer = new TokenizerME(tmodel);
//Tokenizer tokenizer = SimpleTokenizer.INSTANCE;
TokenNameFinderModel model = new TokenNameFinderModel(new File("E:\\opennlp\\bin\\mm-ner-log.bin"));
NameFinderME nameFinder = new NameFinderME(model);
String tokens[] = tokenizer.tokenize(sentence);
Span nameSpans[] = nameFinder.find(tokens);
double[] spanProbs = nameFinder.probs(nameSpans);
for( int i = 0; i<nameSpans.length; i++) {
System.out.println("Span: "+nameSpans[i].toString());
try{
System.out.println(tokens[nameSpans[i].getEnd()+1]);
}catch(java.lang.ArrayIndexOutOfBoundsException ex){
System.out.println(tokens[nameSpans[i].getEnd()]);
}
System.out.println("Probability is: "+spanProbs[i]);
System.out.println();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try { if (modelIn != null) modelIn.close(); } catch (IOException e){};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment