在java中停止单词和词干分析器

N00*_*mer 4 java nlp porter-stemmer stop-words

我正在考虑在我的相似性程序中设置一个停用词,然后是一个词干分析器(针对搬运工1或2取决于最容易实现的)

我想知道,因为我从文件中读取我的文本作为整行并将它们保存为长字符串,所以如果我有两个字符串ex.

String one = "I decided buy something from the shop.";
String two = "Nevertheless I decidedly bought something from a shop.";
Run Code Online (Sandbox Code Playgroud)

现在我得到了这些字符串

词干:我可以直接在它上面使用词干分析器算法,将它保存为字符串然后继续处理相似性,就像在程序中实现词干分析器之前一样,就像运行one.stem(); 之类的事情?

停止说:这是如何解决的?我只是用; one.replaceall("我",""); 或者是否有一些特定的方法用于此过程?我想继续使用字符串并获取字符串,然后在其上使用相似性算法来获得相似性.Wiki没有说太多.

希望你能帮助我!谢谢.

编辑:这是一个与学校相关的项目,我正在写一篇关于不同算法之间相似性的论文,所以我认为我不允许使用lucene或其他为我工作的库.另外,在开始使用像Lucene和co这样的库之前,我想尝试理解它是如何工作的.希望这不是太麻烦^^

Whi*_*g34 11

如果您出于学术原因未实现此功能,则应考虑使用Lucene库.在任何一种情况下,它都可能有助于参考.它具有标记化,停止词过滤,词干和相似性的类.这是一个使用Lucene 3.0删除停用词并阻止输入字符串的快速示例:

public static String removeStopWordsAndStem(String input) throws IOException {
    Set<String> stopWords = new HashSet<String>();
    stopWords.add("a");
    stopWords.add("I");
    stopWords.add("the");

    TokenStream tokenStream = new StandardTokenizer(
            Version.LUCENE_30, new StringReader(input));
    tokenStream = new StopFilter(true, tokenStream, stopWords);
    tokenStream = new PorterStemFilter(tokenStream);

    StringBuilder sb = new StringBuilder();
    TermAttribute termAttr = tokenStream.getAttribute(TermAttribute.class);
    while (tokenStream.incrementToken()) {
        if (sb.length() > 0) {
            sb.append(" ");
        }
        sb.append(termAttr.term());
    }
    return sb.toString();
}
Run Code Online (Sandbox Code Playgroud)

如果你的字符串使用如下:

public static void main(String[] args) throws IOException {
    String one = "I decided buy something from the shop.";
    String two = "Nevertheless I decidedly bought something from a shop.";
    System.out.println(removeStopWordsAndStem(one));
    System.out.println(removeStopWordsAndStem(two));
}
Run Code Online (Sandbox Code Playgroud)

产生此输出:

decid bui someth from shop
Nevertheless decidedli bought someth from shop
Run Code Online (Sandbox Code Playgroud)