X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2FNER.java;h=ac34c0812faf0f24f71392b497ac6fc86ea5d608;hb=c6f5722d9572158f0d561819f080f8ccf4e3c7d9;hp=2868239d90d1ef5f330777b73dd550ed46e5b0db;hpb=9dd396afa60532248fc053ef6063f7b602d58f36;p=pnews.git diff --git a/war/src/main/java/pnews/NER.java b/war/src/main/java/pnews/NER.java index 2868239..ac34c08 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,10 +11,18 @@ 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 List classify(String str, List entities) throws ClassCastException, ClassNotFoundException, IOException { CRFClassifier classifier; List> 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); @@ -21,11 +31,21 @@ public class NER { 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