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 String[] classify(String str) throws ClassCastException, ClassNotFoundException, IOException {
CRFClassifier<CoreLabel> classifier;
List<List<CoreLabel>> out;
String cat, w;
+ List<String> entities;
+ final String FUNCTION_NAME = "classify";
+
+ LOG.entering(CLASS_NAME, FUNCTION_NAME, str);
classifier = CRFClassifier.getDefaultClassifier();
out = classifier.classify(str);
+ entities = new ArrayList<>();
for (List<CoreLabel> labels: out)
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);
}
+
+ LOG.exiting(CLASS_NAME, FUNCTION_NAME, entities);
+
+ return entities.toArray(new String[0]);
}
public static void main(String[] args) throws Exception {