Search in sources :

Example 1 with POSModel

use of opennlp.tools.postag.POSModel 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 POSModel

use of opennlp.tools.postag.POSModel 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 POSModel

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

the class OpenNLPTest method testLoadEnPOS.

@Test
public void testLoadEnPOS() throws IOException {
    POSModel model = openNLP.getPartOfSpeechModel("en");
    Assert.assertNotNull(model);
    POSTagger posTagger = openNLP.getPartOfSpeechTagger("en");
    Assert.assertNotNull(posTagger);
}
Also used : POSModel(opennlp.tools.postag.POSModel) POSTagger(opennlp.tools.postag.POSTagger) Test(org.junit.Test)

Example 4 with POSModel

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

the class OpenNLPTest method testLoadModelByName.

@Test
public void testLoadModelByName() throws IOException {
    TokenizerModel tokenModel = openNLP.getModel(TokenizerModel.class, "en-token.bin", null);
    Assert.assertNotNull(tokenModel);
    SentenceModel sentModel = openNLP.getModel(SentenceModel.class, "en-sent.bin", null);
    Assert.assertNotNull(sentModel);
    POSModel posModel = openNLP.getModel(POSModel.class, "en-pos-maxent.bin", null);
    Assert.assertNotNull(posModel);
    ChunkerModel chunkModel = openNLP.getModel(ChunkerModel.class, "en-chunker.bin", null);
    Assert.assertNotNull(chunkModel);
    TokenNameFinderModel nerModel = openNLP.getModel(TokenNameFinderModel.class, "en-ner-person.bin", null);
    Assert.assertNotNull(nerModel);
    // unavailable model
    tokenModel = openNLP.getModel(TokenizerModel.class, "ru-token.bin", null);
    Assert.assertNull(tokenModel);
}
Also used : TokenNameFinderModel(opennlp.tools.namefind.TokenNameFinderModel) ChunkerModel(opennlp.tools.chunker.ChunkerModel) SentenceModel(opennlp.tools.sentdetect.SentenceModel) POSModel(opennlp.tools.postag.POSModel) TokenizerModel(opennlp.tools.tokenize.TokenizerModel) Test(org.junit.Test)

Example 5 with POSModel

use of opennlp.tools.postag.POSModel 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)

Aggregations

POSModel (opennlp.tools.postag.POSModel)9 POSTaggerME (opennlp.tools.postag.POSTaggerME)5 Test (org.junit.Test)3 File (java.io.File)2 IOException (java.io.IOException)2 Scanner (java.util.Scanner)2 PerformanceMonitor (opennlp.tools.cmdline.PerformanceMonitor)2 POSModelLoader (opennlp.tools.cmdline.postag.POSModelLoader)2 POSTagger (opennlp.tools.postag.POSTagger)2 ChunkerModel (opennlp.tools.chunker.ChunkerModel)1 TokenNameFinderModel (opennlp.tools.namefind.TokenNameFinderModel)1 SentenceModel (opennlp.tools.sentdetect.SentenceModel)1 TokenizerModel (opennlp.tools.tokenize.TokenizerModel)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