X-Git-Url: https://git.wpitchoune.net/gitweb/?p=pnews.git;a=blobdiff_plain;f=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2FNER.java;h=f8238c1d7ce51869727304c65b9336fb5b08ca13;hp=2868239d90d1ef5f330777b73dd550ed46e5b0db;hb=56c07f5de3319eb61182b7100855801644538e6f;hpb=47bc9d3dc6b3cf77957f11bceea1bcda492f8818 diff --git a/war/src/main/java/pnews/NER.java b/war/src/main/java/pnews/NER.java index 2868239..f8238c1 100644 --- a/war/src/main/java/pnews/NER.java +++ b/war/src/main/java/pnews/NER.java @@ -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 classifier; List> out; String cat, w; + List 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 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 {