【问题标题】:Spell checker using lucene使用 lucene 的拼写检查器
【发布时间】:2013-03-15 10:29:26
【问题描述】:

我正在尝试使用 lucene 拼写检查器编写拼写校正器。我想给它一个带有博客文本内容的文本文件。问题是它只有在我的字典文件中每行给出一个句子/单词时才有效。此外,建议 API 会返回结果,而不会对出现次数给予任何权重。以下是源码

   public class SpellCorrector {

        SpellChecker spellChecker = null;

        public SpellCorrector() {
                try {
                        File file = new File("/home/ubuntu/spellCheckIndex");
                        Directory directory = FSDirectory.open(file);

                        spellChecker = new SpellChecker(directory);

                        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
                        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
                        spellChecker.indexDictionary(
                                        new PlainTextDictionary(new File("/home/ubuntu/main.dictionary")), config, true);
                                                                        //Should I format this file with one sentence/word per line?

                } catch (IOException e) {

                }

        }

        public String correct(String query) {
                if (spellChecker != null) {
                        try {
                                String[] suggestions = spellChecker.suggestSimilar(query, 5);  
                                 // This returns the suggestion not based on occurence but based on when it occured

                                if (suggestions != null) {
                                        if (suggestions.length != 0) {
                                                return suggestions[0];
                                        }
                                }
                        } catch (IOException e) {
                                return null;
                        }
                }
                return null;
        }
}

我需要做一些改变吗?

【问题讨论】:

    标签: solr lucene spell-checking autocorrect


    【解决方案1】:

    关于您的第一个问题,听起来像是预期的、记录在案的字典格式,在 PlainTextDictionary API 中。如果您想传入任意文本,您可能需要对其进行索引并改用LuceneDictionary,或者可能使用HighFrequencyDictionary,具体取决于您的需要。

    拼写检查器会根据单词之间的相似性(基于Levenstein Distance)建议替换,然后再考虑其他问题。如果您希望它只推荐更受欢迎的术语作为建议,您应该将SuggestMode 传递给SpellChecker.suggestSimilar。这可确保建议的匹配项至少与它们打算替换的词一样强大、受欢迎。

    如果您必须覆盖 Lucene 决定最佳匹配的方式,您可以使用 SpellChecker.setComparator 执行此操作,在 SuggestWords 上创建您自己的比较器。由于 SuggestWord 向您公开了freq,因此应该很容易按受欢迎程度排列找到的匹配项。

    【讨论】:

      猜你喜欢
      • 2011-12-27
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多