Finding the stem of a word is easy to do. We will illustrate this process using OpenNLP’s PorterStemmer class.
Identifying the stem of a word
Getting ready
To prepare, we need to do the following:
- Create a new Maven project
- Add the following dependency to the POM file:
<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.9.0</version>
</dependency>
How to do it...
Let's go through the following steps:
- Add the following import statement to the program:
import opennlp.tools.stemmer.PorterStemmer;
- Then, add the following code to the main method:
String wordList[] =
{ "draft", "drafted", "drafting", "drafts",
"drafty", "draftsman" };
PorterStemmer porterStemmer = new PorterStemmer();
for (String word : wordList) {
String stem = porterStemmer.stem(word);
System.out.println("The stem of " + word + " is " + stem);
}
- Execute the program. The output should be as follows:
The stem of drafted is draft
The stem of drafting is draft
The stem of drafts is draft
The stem of drafty is drafti
The stem of draftsman is draftsman
How it works...
We start by creating an array of strings that will hold words that we will use with the stemmer:
String wordList[] =
{ "draft", "drafted", "drafting", "drafts", "drafty", "draftsman" };
The OpenNLP PorterStemmer class supports finding the stem of a word. It has a single default constructor that is used to create an instance of the class, as shown in the following code. This is the only constructor available for this class:
PorterStemmer porterStemmer = new PorterStemmer();
The remainder of the code iterates over the array and invokes the stem method against each word in the array, as shown in the following code:
for (String word : wordList) {
String stem = porterStemmer.stem(word);
System.out.println("The stem of " + word + " is " + stem);
}
See also
- The OpenNLP API can be found at https://opennlp.apache.org/docs/1.9.0/apidocs/opennlp-tools/index.html
- The process of lemmatization is discussed in the Determining the lexical meaning of a word recipe
- An comparison of stemming versus lemmatization can be found at https://blog.bitext.com/what-is-the-difference-between-stemming-and-lemmatization/