Example usage for edu.stanford.nlp.ie.crf CRFClassifier classify

List of usage examples for edu.stanford.nlp.ie.crf CRFClassifier classify

Introduction

In this page you can find the example usage for edu.stanford.nlp.ie.crf CRFClassifier classify.

Prototype

@Override
    public List<IN> classify(List<IN> document) 

Source Link

Usage

From source file:NERServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("usage: java NERServer modelpath");
        System.exit(1);/*from  ww  w .  ja va  2  s.c o  m*/
    }

    CRFClassifier crf = CRFClassifier.getClassifier(args[0]);
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in), 1);

    for (;;) {
        String ln = input.readLine();
        if (ln == null) {
            break;
        }

        List<List<CoreLabel>> out = crf.classify(ln);
        for (List<CoreLabel> sentence : out) {
            for (CoreLabel word : sentence) {
                String label = word.get(CoreAnnotations.AnswerAnnotation.class);
                System.out.print(word.word() + '/' + label + ' ');
            }
        }
        System.out.print('\n');
    }
}

From source file:com.epictodo.controller.nlp.SentenceAnalysis.java

License:Open Source License

/**
 * This method identify and extract NER entities such as Name, Person, Date, Time, Organization, Location
 *
 * @param _sentence//from  w  ww.  ja  va 2  s .  co m
 * @return _results
 */
public LinkedHashMap<String, LinkedHashSet<String>> nerEntitiesExtractor(String _sentence) {
    LinkedHashMap<String, LinkedHashSet<String>> _results = new <String, LinkedHashSet<String>>LinkedHashMap();
    CRFClassifier<CoreLabel> _classifier = load_engine.CLASSIFIER; //CRFClassifier.getClassifierNoExceptions(CLASSIFIER_MODEL);
    List<List<CoreLabel>> _classify = _classifier.classify(_sentence);

    for (List<CoreLabel> _tokens : _classify) {
        for (CoreLabel _token : _tokens) {
            String _word = _token.word();
            String _category = _token.get(CoreAnnotations.AnswerAnnotation.class);

            if (!"O".equals(_category)) {
                if (_results.containsKey(_category)) {
                    // Key already exists, insert to LinkedHashMap
                    _results.get(_category).add(_word);
                } else {
                    LinkedHashSet<String> _temp = new LinkedHashSet<>();
                    _temp.add(_word);
                    _results.put(_category, _temp);
                }
            }
        }
    }

    return _results;
}