【发布时间】: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