Search in sources :

Example 1 with POSTaggerME

use of opennlp.tools.postag.POSTaggerME in project deeplearning4j by deeplearning4j.

the class PoStagger method initialize.

/**
     * Initializes the current instance with the given context.
     * 
     * Note: Do all initialization in this method, do not use the constructor.
     */
@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);
    this.context = context;
    this.logger = context.getLogger();
    if (this.logger.isLoggable(Level.INFO)) {
        this.logger.log(Level.INFO, "Initializing the OpenNLP " + "Part of Speech annotator.");
    }
    POSModel model;
    try {
        POSModelResource modelResource = (POSModelResource) context.getResourceObject(UimaUtil.MODEL_PARAMETER);
        model = modelResource.getModel();
    } catch (ResourceAccessException e) {
        throw new ResourceInitializationException(e);
    }
    Integer beamSize = AnnotatorUtil.getOptionalIntegerParameter(context, UimaUtil.BEAM_SIZE_PARAMETER);
    if (beamSize == null)
        beamSize = POSTaggerME.DEFAULT_BEAM_SIZE;
    this.posTagger = new POSTaggerME(model, beamSize, 0);
}
Also used : POSModelResource(opennlp.uima.postag.POSModelResource) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) POSModel(opennlp.tools.postag.POSModel) POSTaggerME(opennlp.tools.postag.POSTaggerME) ResourceAccessException(org.apache.uima.resource.ResourceAccessException)

Example 2 with POSTaggerME

use of opennlp.tools.postag.POSTaggerME in project textdb by TextDB.

the class POSTagexample method main.

public static void main(String[] args) throws IOException {
    POSModel model = new POSModelLoader().load(new File("./src/main/java/edu/uci/ics/textdb/sandbox/OpenNLPexample/en-pos-maxent.bin"));
    PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent");
    POSTaggerME tagger = new POSTaggerME(model);
    String dataFile = "./src/main/resources/abstract_100.txt";
    Scanner scan = new Scanner(new File(dataFile));
    int counter = 0;
    perfMon.start();
    while (scan.hasNextLine()) {
        String input = scan.nextLine();
        String[] sentence = Tokenize(input);
        String[] tags = tagger.tag(sentence);
        perfMon.incrementCounter();
        for (int i = 0; i < sentence.length; i++) {
            String word = sentence[i];
            String pos = tags[i];
            //filter out useless results
            if (!word.equals(pos) && !pos.equals("``") && !pos.equals("''")) {
                counter++;
                System.out.println("word: " + sentence[i] + " pos: " + tags[i]);
            }
        }
    }
    System.out.println("Total Number of Results: " + counter);
    perfMon.stopAndPrintFinalResult();
    scan.close();
}
Also used : Scanner(java.util.Scanner) POSModel(opennlp.tools.postag.POSModel) POSTaggerME(opennlp.tools.postag.POSTaggerME) PerformanceMonitor(opennlp.tools.cmdline.PerformanceMonitor) POSModelLoader(opennlp.tools.cmdline.postag.POSModelLoader) File(java.io.File)

Example 3 with POSTaggerME

use of opennlp.tools.postag.POSTaggerME in project stanbol by apache.

the class OpenNlpPosTaggingEngine method getPOSTagger.

private POSTagger getPOSTagger(String language) {
    String modelName = languageConfig.getParameter(language, MODEL_NAME_PARAM);
    try {
        POSModel model;
        if (modelName == null) {
            //use the default
            model = openNLP.getPartOfSpeechModel(language);
        } else {
            model = openNLP.getModel(POSModel.class, modelName, null);
        }
        if (model != null) {
            log.debug("POS Tagger Model {} for lanugage '{}' version: {}", new Object[] { model.getClass().getSimpleName(), model.getLanguage(), model.getVersion() != null ? model.getVersion() : "undefined" });
            return new POSTaggerME(model);
        }
    } catch (Exception e) {
        log.warn("Unable to load POS model for language '" + language + "'!", e);
    }
    log.debug("POS tagging Model for Language '{}' not available.", language);
    return null;
}
Also used : POSModel(opennlp.tools.postag.POSModel) POSTaggerME(opennlp.tools.postag.POSTaggerME) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 4 with POSTaggerME

use of opennlp.tools.postag.POSTaggerME in project stanbol by apache.

the class TextAnalyzer method getPosTagger.

protected final POSTaggerME getPosTagger() {
    if (!config.enablePosTagger) {
        return null;
    }
    if (posTagger == null && !posTaggerNotAvailable) {
        try {
            POSModel posModel = openNLP.getPartOfSpeechModel(language);
            if (posModel != null) {
                posTagger = new POSTaggerME(posModel);
            } else {
                log.debug("No POS Model for language '{}'", language);
                posTaggerNotAvailable = true;
            }
        } catch (IOException e) {
            log.info("Unable to load POS Model for language '" + language + "'", e);
            posTaggerNotAvailable = true;
        }
    }
    return posTagger;
}
Also used : POSModel(opennlp.tools.postag.POSModel) POSTaggerME(opennlp.tools.postag.POSTaggerME) IOException(java.io.IOException)

Aggregations

POSModel (opennlp.tools.postag.POSModel)4 POSTaggerME (opennlp.tools.postag.POSTaggerME)4 File (java.io.File)1 IOException (java.io.IOException)1 Scanner (java.util.Scanner)1 PerformanceMonitor (opennlp.tools.cmdline.PerformanceMonitor)1 POSModelLoader (opennlp.tools.cmdline.postag.POSModelLoader)1 POSModelResource (opennlp.uima.postag.POSModelResource)1 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)1 ResourceAccessException (org.apache.uima.resource.ResourceAccessException)1 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1