【问题标题】:stem words and create index without stop words using Lucene 4.0使用 Lucene 4.0 词干和创建没有停用词的索引
【发布时间】:2012-12-18 16:39:58
【问题描述】:

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

更新:我现在已经用一个文档创建了一个索引。该文档没有停用词并且是词干的。我如何计算 tf/idf 到这个文档 uisng lucenc? (我会在弄清楚如何进行计算后添加更多文档)

对 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    

【问题讨论】:

    标签: lucene stemming stop-words


    【解决方案1】:

    要过滤掉停用词,请使用StopAnalyzer。它将删除以下单词:

      "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(Iterable&lt;? extends IndexableField&gt; doc, Analyzer analyzer) 方法以及在搜索过程中,可以提供分析器。有关更多选项和详细信息,请参阅 Javadoc。

    对于词干提取,请查看this SO post

    很难提供更多建议,因为您没有解释到底是什么失败了。

    【讨论】:

    • 谢谢。这是我到目前为止尝试的代码,它只删除了停用词(如何添加我自己的停用词集?)尝试了您发布的链接,但 import porterStemmer 不可见(真的不知道是什么意思)我的构建路径中只有lucene 3.6 jar。
    • StopAnalyzer 有另一个构造函数,您可以在其中提供自己的停用词集。至于stemming,porterStemmer要单独下载,看看Lucene analyzersjar。
    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多