Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Stanford NLP POS Tagger With Maven

DZone's Guide to

Stanford NLP POS Tagger With Maven

The Stanford NLP POS Tagger is used to mark up text to be processed by natural language processing and NLP. Read on to learn how to use it!

· AI Zone
Free Resource

Last time, we talked about the Apache Open NLP POS Tagger. This post is about using the Stanford NLP to tag any part of speech.We will be setting up a Maven-based project to get started with Stanford NLP. In the example below, we will be using MaxentTagger to tag any POS by using Stanford NLP. Here are some different POS tags with their corresponding meaning:

POS Tags

Following is the Maven dependencies to get started with Stanford NLP:

<dependency>
  <groupId>edu.stanford.nlp</groupId>
  <artifactId>stanford-corenlp</artifactId>
  <version>3.8.0</version>
</dependency>

To initialize MaxentTagger, we need to invoke the constructor with the location of parameter file with a trained tagger as english-left3words-distsim.tagger. For example:

MaxentTagger maxentTagger = new MaxentTagger("english-left3words-distsim.tagger");

Once the Maxent tagger is initialized, we can call predefined method tagString() provided inside this class to tag any sentence. This method basically returns a tagged string. Following is a sample code to use tagString():

String tag = maxentTagger.tagString(text);
   String[] eachTag = tag.split("\\s+");
   System.out.println("Word      " + "Stanford tag");
   System.out.println("----------------------------------");
   for(int i = 0; i< eachTag.length; i++) {
     System.out.println(eachTag[i].split("_")[0] +"           "+ eachTag[i].split("_")[1]);
}

If you try to execute the above implementation for any text and if you have several test classes, you can combine them into a test suite. We can expect result following result:

standford-tagging-output

That's it!

Topics:
nlp ,maven ,ai ,tutorial

Published at DZone with permission of Dhiraj Ray. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}