I am looking for a class or method that takes a long string of many 100s of words and tokenizes, removes the stop words and stems for use in an IR system.
For example:
"The big fat cat, said 'your funniest guy i know' to the kangaroo..."
the tokenizer would remove the punctuation and return an ArrayList
of words
the stop word remover would remove words like "the", "to", etc
the stemmer would reduce each word the their 'root', for example 'funniest' would become funny
Many thanks in advance.
Stop word elimination and stemming are commonly used method in indexing. Stop words are high frequency words that have little semantic weight and are thus unlikely to help the retrieval process. Usual practice in IR is to drop them from index. Stemming conflates morphological variants of words in its root or stem.
The resulting stopwordsRegex will have the format “\\b(he|she|the|…) \\b\\s?”. In this regex, “\b” refers to a word boundary, to avoid replacing “he” in “heat” for example, while “\s?” refers to zero or one space, to delete the extra space after replacing a stopword.
To remove stop-words using Lucene you could either use their Default Stop Set using the method EnglishAnalyzer. getDefaultStopSet(); . Otherwise, you could create your own custom stop-words list.
Stemmer, implementing the Porter Stemming Algorithm The Stemmer class transforms a word into its root form. The input word can be provided a character at time (by calling add()), or at once by calling one of the various stem(something) methods.
AFAIK Lucene can do what you want. With StandardAnalyzer
and StopAnalyzer
you can to the stop word removal. In combination with the Lucene contrib-snowball
(which includes work from Snowball) project you can do the stemming too.
But for stemming also consider this answer to: Stemming algorithm that produces real words
These are standard requirements in Natural Language Processing so I would look in such toolkits. Since you require Java I'd start with OpenNLP: http://opennlp.sourceforge.net/
If you can look at other languages there is also NLTK (Python)
Note that "your funniest guy i know" is not standard syntax and this makes it harder to process than "You're the funniest guy I know". Not impossible, but much harder. I don't know of any system that would equate "your" to "you are".
I have dealt with the issue on a number of tasks I have worked with, so let me give a tokenizer suggestion. As I do not see it given directly as an answer, I often use edu.northwestern.at.utils.corpuslinguistics.tokenizer.*
as my family of tokenizers. I see a number of cases where I used the PennTreebankTokenizer
class. Here is how you use it:
WordTokenizer wordTokenizer = new PennTreebankTokenizer();
List<String> words = wordTokenizer.extractWords(text);
The link to this work is here. Just a disclaimer, I have no affiliation with Northwestern, the group, or the work they do. I am just someone who uses the code occasionally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With