使用Lucene 4.0为单词添加词干并创建不带停止词的索引

使用Lucene 4.0为单词添加词干并创建不带停止词的索引,lucene,stemming,stop-words,Lucene,Stemming,Stop Words,我有以下问题:有几个文本文档需要解析和创建索引,但没有停止词和词干。我可以手动完成,但我从一位同事那里听说Lucene可以自动完成。 我在网上搜索了很多我尝试过的例子,但是每个例子都使用了不同版本的lucene和不同的方法,没有一个例子是完整的。 在这个过程结束时,我需要计算集合中每个术语的tf/idf 更新:我现在已经创建了一个包含一个文档的索引。该文档没有停止词,并且有词干。如何使用lucenc计算此文档的tf/idf?(我将在计算出如何进行计算后添加更多文档) 对lucene的任何帮助都将

我有以下问题:有几个文本文档需要解析和创建索引,但没有停止词和词干。我可以手动完成,但我从一位同事那里听说Lucene可以自动完成。 我在网上搜索了很多我尝试过的例子,但是每个例子都使用了不同版本的lucene和不同的方法,没有一个例子是完整的。 在这个过程结束时,我需要计算集合中每个术语的tf/idf

更新:我现在已经创建了一个包含一个文档的索引。该文档没有停止词,并且有词干。如何使用lucenc计算此文档的tf/idf?(我将在计算出如何进行计算后添加更多文档)

对lucene的任何帮助都将不胜感激。 谢谢

import java.io.*;
    import java.util.HashSet;
    import org.apache.lucene.analysis.*;
    import org.apache.lucene.analysis.tokenattributes.*;
    import org.apache.lucene.analysis.standard.*;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.*;
    import org.apache.lucene.analysis.snowball.*;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.index.IndexWriter;


public class Stemmer
{
    static HashSet<String> stopWordsList = null;

    public static String Stem(String text, String language) throws IOException
    {
        parse p = new parse();
        stopWordsList = p.readStopWordsFile();
        StringBuffer result = new StringBuffer();
        if (text!=null && text.trim().length()>0)
        {
            StringReader tReader = new StringReader(text);
            // Analyzer analyzer = new StopAnalyzer(Version.LUCENE_36,stopWordsList);
            @SuppressWarnings("deprecation")
            Analyzer analyzer = new SnowballAnalyzer(Version.LUCENE_35,"English",stopWordsList);
            // disk index storage
            Directory directory = FSDirectory.open(new File("d:/index")); 

            @SuppressWarnings("deprecation")
            IndexWriter writer = new IndexWriter(directory, analyzer, true, new IndexWriter.MaxFieldLength(25000));

            TokenStream tStream = analyzer.tokenStream("contents", tReader);
            @SuppressWarnings("deprecation")
            TermAttribute term = tStream.addAttribute(TermAttribute.class);

            try {
                while (tStream.incrementToken())
                    {
                        result.append(term.term());
                        result.append(" ");
                    }

                Document doc = new Document();
                String title = "DocID";
                // adding title field
                doc.add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED)); 
                String content = result.toString();
                // adding content field
                doc.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
                // writing new document to the index
                writer.addDocument(doc);  
                writer.close();
                System.out.println("Reult is: " + result);  
            } 
            catch (IOException ioe)
                {
                    System.out.println("Error: "+ioe.getMessage());
                }
        }

        // If, for some reason, the stemming did not happen, return the original text
        if (result.length()==0)
            result.append(text);
        return result.toString().trim();

    } //end stem

    public static void main (String[] args) throws IOException
        {
            Stemmer.Stem("Michele Bachmann amenities pressed her allegations that the former head of her Iowa presidential bid was bribed by the campaign of rival Ron Paul to endorse him, even as one of her own aides denied the charge.", "English");
        }
}//end class    
import java.io.*;
导入java.util.HashSet;
导入org.apache.lucene.analysis.*;
导入org.apache.lucene.analysis.tokenattributes.*;
导入org.apache.lucene.analysis.standard.*;
导入org.apache.lucene.store.Directory;
导入org.apache.lucene.store.FSDirectory;
导入org.apache.lucene.util.*;
导入org.apache.lucene.analysis.snowball.*;
导入org.apache.lucene.document.document;
导入org.apache.lucene.document.Field;
导入org.apache.lucene.index.IndexWriter;
公共类词干分析器
{
静态HashSet stopWordsList=null;
公共静态字符串干(字符串文本、字符串语言)引发IOException
{
parse p=newparse();
stopWordsList=p.readStopWordsFile();
StringBuffer结果=新的StringBuffer();
如果(text!=null&&text.trim().length()>0)
{
StringReader踏板=新StringReader(文本);
//Analyzer Analyzer=新的StopAnalyzer(版本.LUCENE_36,stopWordsList);
@抑制警告(“弃用”)
Analyzer Analyzer=新型雪球分析仪(LUCENE_35版,“英语”,stopWordsList);
//磁盘索引存储
Directory=FSDirectory.open(新文件(“d:/index”);
@抑制警告(“弃用”)
IndexWriter writer=new IndexWriter(目录,分析器,true,new IndexWriter.MaxFieldLength(25000));
TokenStream tStream=analyzer.TokenStream(“内容”,tReader);
@抑制警告(“弃用”)
termtattribute term=tStream.addAttribute(termtattribute.class);
试一试{
while(tStream.incrementToken())
{
result.append(term.term());
结果。追加(“”);
}
单据单据=新单据();
字符串title=“DocID”;
//添加标题字段
新增单据(新增字段(“标题”,标题,Field.Store.YES,Field.Index.ANALYZED));
字符串内容=result.toString();
//添加内容字段
新增单据(新增字段(“内容”,内容,Field.Store.YES,Field.Index.ANALYZED));
//将新文档写入索引
writer.addDocument(doc);
writer.close();
System.out.println(“Reult is:+result”);
} 
捕获(ioe异常ioe)
{
System.out.println(“错误:+ioe.getMessage());
}
}
//如果由于某种原因,没有进行词干分析,请返回原始文本
if(result.length()==0)
结果。追加(文本);
返回结果.toString().trim();
}//端杆
公共静态void main(字符串[]args)引发IOException
{
Stemmer.Stem(“米歇尔·巴赫曼(Michele Bachmann)对她的指控进行了追问,称她的前爱荷华州总统竞选负责人被对手罗恩·保罗(Ron Paul)的竞选活动贿赂以支持他,尽管她自己的一名助手否认了这一指控。”,“英语”);
}
}//末级

要过滤掉停止词,请使用。它将删除以下文字:

  "a", "an", "and", "are", "as", "at", "be", "but", "by",
  "for", "if", "in", "into", "is", "it",
  "no", "not", "of", "on", "or", "such",
  "that", "the", "their", "then", "there", "these",
  "they", "this", "to", "was", "will", "with"

如果您使用
addDocument(Iterablemindas谢谢。这是我迄今为止尝试的代码,它只删除了停止词(如何添加我自己的停止词集?)尝试了您发布的链接,但导入porterStemmer不可见(不知道它的真正含义)我的构建路径中只有lucene 3.6 jar。StopAnalyzer有另一个构造函数,您可以在其中提供自己的stopwords集。至于词干分析,porterStemmer必须单独下载,请查看jar。