package pnews;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.List;
+import java.util.logging.Logger;
import edu.stanford.nlp.ie.crf.CRFClassifier;
import edu.stanford.nlp.ling.CoreAnnotations.AnswerAnnotation;
/** https://stanfordnlp.github.io/CoreNLP/api.html */
public class NER {
- public static void classify(String str) throws ClassCastException, ClassNotFoundException, IOException {
+ private static final String CLASS_NAME = NER.class.getName();
+ private static final Logger LOG = Logger.getLogger(CLASS_NAME);
+
+ public static List<String> classify(String str, List<String> entities) throws ClassCastException, ClassNotFoundException, IOException {
CRFClassifier<CoreLabel> classifier;
List<List<CoreLabel>> out;
String cat, w;
+ final String FUNCTION_NAME = "classify";
+
+ LOG.entering(CLASS_NAME, FUNCTION_NAME, str);
+
+ OpenNLP.classify(str, entities);
classifier = CRFClassifier.getDefaultClassifier();
out = classifier.classify(str);
for (CoreLabel l: labels) {
cat = l.getString(AnswerAnnotation.class);
w = l.word();
- System.out.println(cat + " " + w);
+ if (!cat.equals("O") && !entities.contains(w))
+ entities.add(w);
}
+
+ entities.remove("CNET");
+ entities.remove("Read More");
+ entities.remove("New");
+ entities.remove("App");
+
+ LOG.exiting(CLASS_NAME, FUNCTION_NAME, entities);
+
+ return entities;
}
public static void main(String[] args) throws Exception {
- classify("I live in Washington.");
+ classify("I live in Washington.", new ArrayList<>());
}
}
\ No newline at end of file