Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4579466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:38:46+00:00 2026-05-21T20:38:46+00:00

I want to POStag an English sentence and do some processing. I would like

  • 0

I want to POStag an English sentence and do some processing. I would like to use openNLP. I have it installed

When I execute the command

I:\Workshop\Programming\nlp\opennlp-tools-1.5.0-bin\opennlp-tools-1.5.0>java -jar opennlp-tools-1.5.0.jar POSTagger models\en-pos-maxent.bin < Text.txt

It gives output POSTagging the input in Text.txt

    Loading POS Tagger model ... done (4.009s)
My_PRP$ name_NN is_VBZ Shabab_NNP i_FW am_VBP 22_CD years_NNS old._.


Average: 66.7 sent/s
Total: 1 sent
Runtime: 0.015s

I hope it installed properly?

Now how do i do this POStagging from inside a java application? I have added the openNLPtools, jwnl, maxent jar to the project but how do i invoke the POStagging?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:38:46+00:00Added an answer on May 21, 2026 at 8:38 pm

    Here’s some (old) sample code I threw together, with modernized code to follow:

    package opennlp;
    
    import opennlp.tools.cmdline.PerformanceMonitor;
    import opennlp.tools.cmdline.postag.POSModelLoader;
    import opennlp.tools.postag.POSModel;
    import opennlp.tools.postag.POSSample;
    import opennlp.tools.postag.POSTaggerME;
    import opennlp.tools.tokenize.WhitespaceTokenizer;
    import opennlp.tools.util.ObjectStream;
    import opennlp.tools.util.PlainTextByLineStream;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.StringReader;
    
    public class OpenNlpTest {
    public static void main(String[] args) throws IOException {
        POSModel model = new POSModelLoader().load(new File("en-pos-maxent.bin"));
        PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent");
        POSTaggerME tagger = new POSTaggerME(model);
    
        String input = "Can anyone help me dig through OpenNLP's horrible documentation?";
        ObjectStream<String> lineStream =
                new PlainTextByLineStream(new StringReader(input));
    
        perfMon.start();
        String line;
        while ((line = lineStream.read()) != null) {
    
            String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE.tokenize(line);
            String[] tags = tagger.tag(whitespaceTokenizerLine);
    
            POSSample sample = new POSSample(whitespaceTokenizerLine, tags);
            System.out.println(sample.toString());
    
            perfMon.incrementCounter();
        }
        perfMon.stopAndPrintFinalResult();
    }
    }
    

    The output is:

    Loading POS Tagger model ... done (2.045s)
    Can_MD anyone_NN help_VB me_PRP dig_VB through_IN OpenNLP's_NNP horrible_JJ documentation?_NN
    
    Average: 76.9 sent/s 
    Total: 1 sent
    Runtime: 0.013s
    

    This is basically working from the POSTaggerTool class included as part of OpenNLP. The sample.getTags() is a String array that has the tag types themselves.

    This requires direct file access to the training data, which is really, really lame.

    An updated codebase for this is a little different (and probably more useful.)

    First, a Maven POM:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.javachannel</groupId>
        <artifactId>opennlp-example</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.apache.opennlp</groupId>
                <artifactId>opennlp-tools</artifactId>
                <version>1.6.0</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>[6.8.21,)</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    And here’s the code, written as a test, therefore located in ./src/test/java/org/javachannel/opennlp/example:

    package org.javachannel.opennlp.example;
    
    import opennlp.tools.cmdline.PerformanceMonitor;
    import opennlp.tools.postag.POSModel;
    import opennlp.tools.postag.POSSample;
    import opennlp.tools.postag.POSTaggerME;
    import opennlp.tools.tokenize.WhitespaceTokenizer;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.nio.channels.Channels;
    import java.nio.channels.ReadableByteChannel;
    import java.util.stream.Stream;
    
    public class POSTest {
        private void download(String url, File destination) throws IOException {
            URL website = new URL(url);
            ReadableByteChannel rbc = Channels.newChannel(website.openStream());
            FileOutputStream fos = new FileOutputStream(destination);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        }
    
        @DataProvider
        Object[][] getCorpusData() {
            return new Object[][][]{{{
                    "Can anyone help me dig through OpenNLP's horrible documentation?"
            }}};
        }
    
        @Test(dataProvider = "getCorpusData")
        public void showPOS(Object[] input) throws IOException {
            File modelFile = new File("en-pos-maxent.bin");
            if (!modelFile.exists()) {
                System.out.println("Downloading model.");
                download("http://opennlp.sourceforge.net/models-1.5/en-pos-maxent.bin", modelFile);
            }
            POSModel model = new POSModel(modelFile);
            PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent");
            POSTaggerME tagger = new POSTaggerME(model);
    
            perfMon.start();
            Stream.of(input).map(line -> {
                String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE.tokenize(line.toString());
                String[] tags = tagger.tag(whitespaceTokenizerLine);
    
                POSSample sample = new POSSample(whitespaceTokenizerLine, tags);
    
                perfMon.incrementCounter();
                return sample.toString();
            }).forEach(System.out::println);
            perfMon.stopAndPrintFinalResult();
        }
    }
    

    This code doesn’t actually test anything – it’s a smoke test, if anything – but it should serve as a starting point. Another (potentially) nice thing is that it downloads a model for you if you don’t have it downloaded already.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Want to run javascript function from parent window in child window Example I have
want to know why String behaves like value type while using ==. String s1
want to have a Hyperlink-Button in a gridView in which I can display a
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
I want to display place details from this json: I have problem with parse
I'm using a form where I want to have the required field missing error
I have the following tree: 1-[HAS]->2-[HASSUBPAGE]->3-[HASSUBPAGE]->4 Now I want to traverse this tree starting
After watching RailsCasts #273 I want to use the Geocoder gem. I've seen this:
I have a xml which I want it to be extracted using OpenXML within
I want to add space to word something like this CountryName RegionName ZipPostalCode to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.