我想在文件“fdictionary.txt”中搜索查询,该文件包含逐行写入的单词列表(230,000 个单词)。有什么建议为什么这段代码不起作用? 拼写检查部分正在运行,并为我提供了建议列表(我将列表的长度限制为 1)。我想做的是搜索该词典,如果该词已经在其中,则不要调用拼写检查。我的搜索功能不起作用。它不会给我错误!这是我已经实现的:
public class SpellCorrection {
public static File indexDir = new File("/../idxDir");
public static void main(String[] args) throws IOException, FileNotFoundException, CorruptIndexException, ParseException {
Directory directory = FSDirectory.open(indexDir);
SpellChecker spell = new SpellChecker(directory);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_20, null);
File dictionary = new File("/../fdictionary00.txt");
spell.indexDictionary(new PlainTextDictionary(dictionary), config, true);
String query = "red"; //kne, console
String correctedQuery = query; //kne, console
if (!search(directory, query)) {
String[] suggestions = spell.suggestSimilar(query, 1);
if (suggestions != null) {correctedQuery=suggestions[0];}
}
System.out.println("The Query was: "+query);
System.out.println("The Corrected Query is: "+correctedQuery);
}
public static boolean search(Directory directory, String queryTerm) throws FileNotFoundException, CorruptIndexException, IOException, ParseException {
boolean isIn = false;
IndexReader indexReader = IndexReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_20);
Term term = new Term(queryTerm);
Query termQuery = new TermQuery(term);
TopDocs hits = indexSearcher.search(termQuery, 100);
System.out.println(hits.totalHits);
if (hits.totalHits > 0) {
isIn = true;
}
return isIn;
}
}
最佳答案
您在哪里为 fdictionary00.txt 的内容建立索引?
只有当您有index.html时,您才可以使用IndexSearcher
进行搜索。如果您是 lucene 新手,您可能需要查看一些快速教程。 (如http://lucenetutorial.com/lucene-in-5-minutes.html)
关于java - 如何使用lucene搜索文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958871/