import edu.stanford.nlp.ie.crf.CRFClassifier;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.Triple;
+import pnews.servlet.Config;
/** https://stanfordnlp.github.io/CoreNLP/api.html */
public class NER {
private static final Logger LOG = Logger.getLogger(CLASS_NAME);
private static final CRFClassifier<CoreMap> classifier = CRFClassifier.getDefaultClassifier();
- public static List<String> classify(String str, List<String> entities) throws ClassCastException, ClassNotFoundException, IOException {
+ public static List<String> classify(String str, List<String> entities, Config config) throws ClassCastException, ClassNotFoundException, IOException {
List<Triple<String, Integer, Integer>> triples;
String w;
LOG.entering(CLASS_NAME, FUNCTION_NAME, str);
- OpenNLP.classify(str, entities);
+ OpenNLP.classify(str, entities, config);
synchronized (classifier) {
triples = classifier.classifyToCharacterOffsets(str);
for (Triple<String, Integer, Integer> t: triples) {
w = str.substring(t.second, t.third);
- if (!entities.contains(w))
- entities.add(w);
+ if (!config.isBlacklistedEntity(w) && !entities.contains(w))
+ entities.add(config.getEntityAlias(w));
}
- entities.remove("CNET");
- entities.remove("Read More");
- entities.remove("New");
- entities.remove("App");
- entities.remove("Digital Trends");
-
LOG.exiting(CLASS_NAME, FUNCTION_NAME, entities);
return entities;
public static void main(String[] args) throws Exception {
List<String> lst;
- lst = classify("I live in Washington and New York in United States.", new ArrayList<>());
+ lst = classify("I live in Washington and New York in United States.", new ArrayList<>(), new Config());
for (String str: lst)
System.out.println(str);
}