From 9dd396afa60532248fc053ef6063f7b602d58f36 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Sat, 21 Oct 2017 02:31:43 +0200 Subject: [PATCH] added NER base class --- war/pom.xml | 11 +++++++++++ war/src/main/java/pnews/NER.java | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 war/src/main/java/pnews/NER.java diff --git a/war/pom.xml b/war/pom.xml index d038cd3..76e68c6 100644 --- a/war/pom.xml +++ b/war/pom.xml @@ -34,5 +34,16 @@ gson 2.8.2 + + edu.stanford.nlp + stanford-corenlp + 3.8.0 + + + edu.stanford.nlp + stanford-corenlp + 3.8.0 + models + diff --git a/war/src/main/java/pnews/NER.java b/war/src/main/java/pnews/NER.java new file mode 100644 index 0000000..2868239 --- /dev/null +++ b/war/src/main/java/pnews/NER.java @@ -0,0 +1,31 @@ +package pnews; + +import java.io.IOException; +import java.util.List; + +import edu.stanford.nlp.ie.crf.CRFClassifier; +import edu.stanford.nlp.ling.CoreAnnotations.AnswerAnnotation; +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 { + CRFClassifier classifier; + List> out; + String cat, w; + + classifier = CRFClassifier.getDefaultClassifier(); + out = classifier.classify(str); + + for (List labels: out) + for (CoreLabel l: labels) { + cat = l.getString(AnswerAnnotation.class); + w = l.word(); + System.out.println(cat + " " + w); + } + } + + public static void main(String[] args) throws Exception { + classify("I live in Washington."); + } +} \ No newline at end of file -- 2.7.4