2868239d90d1ef5f330777b73dd550ed46e5b0db
[pnews.git] / war / src / main / java / pnews / NER.java
1 package pnews;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import edu.stanford.nlp.ie.crf.CRFClassifier;
7 import edu.stanford.nlp.ling.CoreAnnotations.AnswerAnnotation;
8 import edu.stanford.nlp.ling.CoreLabel;
9
10 /** https://stanfordnlp.github.io/CoreNLP/api.html */
11 public class NER {
12         public static void classify(String str) throws ClassCastException, ClassNotFoundException, IOException {
13                 CRFClassifier<CoreLabel> classifier;
14                 List<List<CoreLabel>> out;
15                 String cat, w;
16                 
17                 classifier = CRFClassifier.getDefaultClassifier();
18                 out = classifier.classify(str);
19                 
20                 for (List<CoreLabel> labels: out)
21                         for (CoreLabel l: labels) {
22                                 cat = l.getString(AnswerAnnotation.class);
23                                 w = l.word();
24                                 System.out.println(cat + " " + w);
25                         }
26         }
27         
28         public static void main(String[] args) throws Exception {
29                 classify("I live in Washington.");
30         }
31 }