package cn.edu.bjut.textmining.chapter2; import cn.edu.bjut.textmining.util.FileUtil; import org.tartarus.snowball.ext.PorterStemmer; import java.nio.file.Path; import java.util.List; /** 使用 Lucene analyzers-common 中附带的 Snowball PorterStemmer。 */ public class PorterStemmerDemo { public static void main(String[] args) { try { Path sampleFile = FileUtil.path("data", "chapter2", "stemming-samples.txt"); List words = FileUtil.readNonEmptyUtf8Lines(sampleFile); for (String word : words) { System.out.println(word + " -> " + stem(word)); } } catch (Exception e) { e.printStackTrace(); } } public static String stem(String word) { PorterStemmer stemmer = new PorterStemmer(); stemmer.setCurrent(word); stemmer.stem(); return stemmer.getCurrent(); } }