Example usage for opennlp.tools.doccat DocumentCategorizerME DocumentCategorizerME

List of usage examples for opennlp.tools.doccat DocumentCategorizerME DocumentCategorizerME

Introduction

In this page you can find the example usage for opennlp.tools.doccat DocumentCategorizerME DocumentCategorizerME.

Prototype

public DocumentCategorizerME(DoccatModel model) 

Source Link

Document

Initializes the current instance with a doccat model.

Usage

From source file:io.learningbox.controller.APIController.java

@RequestMapping(value = "/categorize/{area}", method = RequestMethod.POST)
public SortedMap<Double, Set<String>> categorize(@PathVariable final String area, @RequestBody String input)
        throws IOException {
    List<LearningSet> l = repository.findByArea(area);
    final Iterator<LearningSet> sets = l.iterator();

    ObjectStream<DocumentSample> stream = new ObjectStream<DocumentSample>() {

        @Override/*w  w w .  j a va 2  s . c  om*/
        public DocumentSample read() throws IOException {
            if (sets.hasNext()) {
                LearningSet s = sets.next();

                return new DocumentSample(s.getCategory(), s.getText());
            }
            return null;
        }

        @Override
        public void reset() throws IOException, UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

        @Override
        public void close() throws IOException {
            //Do nothing
        }
    };

    TrainingParameters trainingParameters = TrainingParameters.defaultParams();
    trainingParameters.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(1000));
    trainingParameters.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(1));

    DoccatModel model = DocumentCategorizerME.train("en", stream, trainingParameters, new DoccatFactory());
    DocumentCategorizerME myCategorizer = new DocumentCategorizerME(model);
    return myCategorizer.sortedScoreMap(input);
}

From source file:org.wso2.uima.collectionProccesingEngine.analysisEngines.TrafficLevelAnalyser.java

@Override
public void initialize(UimaContext ctx) throws ResourceInitializationException {
    super.initialize(ctx);
    InputStream docStream = null;
    try {/*from  w  ww . j av a  2s .  c om*/

        docStream = getContext().getResourceAsStream("DoccatModel");
        DoccatModel doccatModel = new DoccatModel(docStream);
        documentCategorizer = new DocumentCategorizerME(doccatModel);
        docStream.close();

    } catch (Exception e) {
        logger.error("Error occurs when initializing resources");
        throw new ResourceInitializationException(e);
    } finally {
        IOUtils.closeQuietly(docStream);
        logger.info(TrafficLevelAnalyser.class.getSimpleName() + " Analysis Engine initialized successfully");
    }
}