3 import java.io.IOException;
4 import java.util.ArrayList;
6 import java.util.logging.Logger;
8 import edu.stanford.nlp.ie.crf.CRFClassifier;
9 import edu.stanford.nlp.ling.CoreAnnotations.AnswerAnnotation;
10 import edu.stanford.nlp.ling.CoreLabel;
12 /** https://stanfordnlp.github.io/CoreNLP/api.html */
14 private static final String CLASS_NAME = NER.class.getName();
15 private static final Logger LOG = Logger.getLogger(CLASS_NAME);
17 public static List<String> classify(String str, List<String> entities) throws ClassCastException, ClassNotFoundException, IOException {
18 CRFClassifier<CoreLabel> classifier;
19 List<List<CoreLabel>> out;
21 final String FUNCTION_NAME = "classify";
23 LOG.entering(CLASS_NAME, FUNCTION_NAME, str);
25 OpenNLP.classify(str, entities);
27 classifier = CRFClassifier.getDefaultClassifier();
28 out = classifier.classify(str);
30 for (List<CoreLabel> labels: out)
31 for (CoreLabel l: labels) {
32 cat = l.getString(AnswerAnnotation.class);
34 if (!cat.equals("O") && !entities.contains(w))
38 entities.remove("CNET");
39 entities.remove("Read More");
40 entities.remove("New");
41 entities.remove("App");
43 LOG.exiting(CLASS_NAME, FUNCTION_NAME, entities);
48 public static void main(String[] args) throws Exception {
49 classify("I live in Washington.", new ArrayList<>());