Java Code Examples for opennlp.tools.doccat.DocumentCategorizerME#categorize()

The following examples show how to use opennlp.tools.doccat.DocumentCategorizerME#categorize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Chapter6.java    From Natural-Language-Processing-with-Java-Second-Edition with MIT License 6 votes vote down vote up
private static void usingOpenNLP() {
    try (InputStream modelIn = new FileInputStream(
            new File("en-animal.model"));) {
        DoccatModel model = new DoccatModel(modelIn);
        DocumentCategorizerME categorizer = new DocumentCategorizerME(model);
        double[] outcomes = categorizer.categorize(inputText);
        for (int i = 0; i < categorizer.getNumberOfCategories(); i++) {
            String category = categorizer.getCategory(i);
            System.out.println(category + " - " + outcomes[i]);
        }
        System.out.println(categorizer.getBestCategory(outcomes));
        System.out.println(categorizer.getAllResults(outcomes));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
 
Example 2
Source File: OpenNlpDoccatRecommender.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException
{
    DoccatModel model = aContext.get(KEY_MODEL).orElseThrow(() -> 
            new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));
    
    DocumentCategorizerME finder = new DocumentCategorizerME(model);

    Type sentenceType = getType(aCas, Sentence.class);
    Type predictedType = getPredictedType(aCas);
    Type tokenType = getType(aCas, Token.class);
    Feature scoreFeature = getScoreFeature(aCas);
    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);

    int predictionCount = 0;
    for (AnnotationFS sentence : select(aCas, sentenceType)) {
        if (predictionCount >= traits.getPredictionLimit()) {
            break;
        }
        predictionCount++;
        
        List<AnnotationFS> tokenAnnotations = selectCovered(tokenType, sentence);
        String[] tokens = tokenAnnotations.stream()
            .map(AnnotationFS::getCoveredText)
            .toArray(String[]::new);

        double[] outcome = finder.categorize(tokens);
        String label = finder.getBestCategory(outcome);
        
        AnnotationFS annotation = aCas.createAnnotation(predictedType, sentence.getBegin(),
                sentence.getEnd());
        annotation.setStringValue(predictedFeature, label);
        annotation.setDoubleValue(scoreFeature, NumberUtils.max(outcome));
        annotation.setBooleanValue(isPredictionFeature, true);
        aCas.addFsToIndexes(annotation);
    }
}