2012-01-22 60 views
1

我想在文件“fdictionary.txt”中搜索包含逐行写入的单词列表(230,000字)的查询。任何建议为什么这段代码不起作用? 拼写检查部分正在工作,给了我建议的列表(我将列表的长度限制为1)。我想要做的就是搜索那个fdictionary,如果这个单词已经在那里,不要叫拼写检查。我的搜索功能不起作用。它不会给我错误!这里是我已经实现:如何使用lucene搜索文件

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; 
} 
} 
+0

我相信你的问题已经回答。接受其中一个答案 – naresh

回答

1

你在哪里索引fdictionary00.txt的内容?

只有当您有索引时,才能使用IndexSearcher进行搜索。如果您对lucene不熟悉,则可能需要查看一些快速教程。 (如http://lucenetutorial.com/lucene-in-5-minutes.html

+0

这里:spell.indexDictionary(new PlainTextDictionary(dictionary),config,true); – Marcus

+0

您需要为搜索数据编制索引。检查我给出的链接 – naresh

0

您从未建立过索引。

您需要设置索引...

Directory directory = FSDirectory.open(indexDir); 
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_20); 
IndexWriter writer = new IndexWriter(directory,analyzer,true,IndexWriter.MaxFieldLength.UNLIMITED); 

然后,您需要创建一个文件,每个术语添加到文档作为分析的领域..

Document doc = new Document(); 
doc.Add(new Field("name", word , Field.Store.YES, Field.Index.ANALYZED)); 

然后添加文件索引

writer.AddDocument(doc); 

writer.Optimize(); 

现在构建索引并关闭索引编写器。

writer.Commit(); 
writer.Close(); 
0

你可以做一个服务提供您SpellChecker实例并使用spellChecker.exist(word)

请注意,SpellChecker将不会索引2个字符或更少的字。为了解决这个问题,您可以在创建它之后将它们添加到索引(将它们添加到SpellChecker.F_WORD字段中)。

如果您想要添加到您的实时索引并使其可用于exist(word)那么您需要将它们添加到SpellChecker.F_WORD字段。当然,因为你没有添加到所有其他领域,如克/开始/结束等,那么你的单词不会出现作为其他拼写错误的单词的建议。

在这种情况下,您必须将文字添加到文件中,因此当您重新创建索引时,它将作为建议提供。如果该项目使公众/受保护而不是私人项目成为SpellChecker.createDocument(...)将是非常好的,因为此方法通过添加单词来完成所有任务。

毕竟这需要拨打spellChecker.setSpellIndex(directory)