find hot named entities using stanford ner
[pnews.git] / war / src / main / java / pnews / NER.java
index 2868239..f8238c1 100644 (file)
@@ -1,7 +1,9 @@
 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;
@@ -9,20 +11,33 @@ import edu.stanford.nlp.ling.CoreLabel;
 
 /** 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 {