【问题标题】:Trying to implement Google's "did you mean" feature in java尝试在 Java 中实现 Google 的“你的意思是”功能
【发布时间】:2012-11-02 20:31:31
【问题描述】:

我正在尝试在 java 中实现 google 的“您的意思是”功能。 我在互联网上发现了一些代码说它可以正常工作,但在尝试运行它时却给我一个错误。我认为它与目录创建有关,这是我不完全理解的代码的唯一部分。

这是代码,你能帮我看看有什么问题吗? 提前致谢!

             public static void main(String[] args) throws Exception {
             File dir = new File("C:/Users/Lala");
             Directory directory = FSDirectory.open(dir);

             SpellChecker spellChecker = new SpellChecker(directory);

             spellChecker.indexDictionary(
             new PlainTextDictionary(new File("fulldictionary00.txt")));
             String wordForSuggestions = "hwllo";
             int suggestionsNumber = 5;
             String[] suggestions = spellChecker.
                 suggestSimilar(wordForSuggestions, suggestionsNumber);
             if (suggestions!=null && suggestions.length>0) {
                 for (String word : suggestions) {
                     System.out.println("Did you mean:" + word);
                 }
             }
             else {
                 System.out.println("No suggestions found for word:"+wordForSuggestions);
             }

         }

文件fulldictionary00.txt是一个格式正确的纯文本文件。

我得到的错误是在第 18 行:

SpellChecker spellChecker = new SpellChecker(directory);

所以它与目录创建有关。我正在粘贴我得到的错误,以防万一你看到它时有任何想法。

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/document/Fieldable at did_you_mean.main(did_you_mean.java:18) Caused by:     
 java.lang.ClassNotFoundException: org.apache.lucene.document.Fieldable 

【问题讨论】:

    标签: java lucene


    【解决方案1】:

    编辑

    根据 OPs 的评论,错误是 Lucene 的 JAR 文件似乎不在类路径中...

    原始答案,不知道错误(保留原样可能有用)

    您必须将内容添加到指定的文件中...没有它就无法工作。稍微想一想:程序应该如何知道哪些单词是正确的,哪些不是?

    对于纯文本字典文件,您应该使用 PlainTextDictionary

    由文本文件表示的字典。

    允许的格式:每行 1 个字:
    word1
    word2
    词3

    This page 在 Lucene 索引的上下文中稍微解释一下:

    导入:将单词添加到字典中 我们可以添加来自 Lucene 索引(更准确地说来自一组 Lucene 字段)的单词,以及来自带有单词列表的文本文件。

    示例:我们可以添加我的索引的给定 Lucene 字段的所有关键字。

    SpellChecker spell= new SpellChecker(dictionaryDirectory);
    spell.indexDictionary(new LuceneDictionary(my_luceneReader,my_fieldname));
    

    【讨论】:

    • 首先感谢您的回答! fulldictionary00.txt 是您刚才提到的格式的字典,抱歉之前没有说。我得到的错误在第 18 行(SpellChecker spellChecker = new SpellChecker(directory);) ,所以它与目录创建有关。我粘贴我得到的错误,以防万一你看到它时有任何想法。 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/document/Fieldable at did_you_mean.main(did_you_mean.java:18) Caused by: java.lang.ClassNotFoundException: org.apache.lucene.document.Fieldable
    • @missrg:你如何运行你的应用程序? Lucene JAR 文件是否包含在运行时配置中?
    • 是的,我想我已经包含了该程序所需的一切。自己看一下:import java.io.File;导入 org.apache.lucene.search.spell.PlainTextDictionary;导入 org.apache.lucene.search.spell.SpellChecker;导入 org.apache.lucene.store.Directory;导入 org.apache.lucene.store.FSDirectory;
    • @missrg 你如何启动程序?
    • 整个程序就是你在 main 中看到的(加上包含),没有更多的代码行。我用运行按钮从eclipse运行它..
    【解决方案2】:

    以防万一其他人有同样的问题,我找到了解决方法!

    首先,问题似乎出在我下载的 lucene 4.0.0 版本上,因为一个 jar 文件的一个类正在调用另一个已在此版本中重命名的 jar 文件中的类。

    为了解决这个问题,我刚刚下载了一个旧版本 (3.6.1),它需要对现有代码进行一些更改。在这个版本中, spellChecker.IndexDictionary() 函数需要 3 个参数:

    spellChecker.indexDictionary(new PlainTextDictionary(new File("fulldictionary00.txt")),config,false);

    config 是一个 IndexWriterConfig 对象。

    我希望这能帮助遇到同样问题的人! @ppeterka 无论如何感谢您的帮助!

    【讨论】:

      【解决方案3】:

      嗯,在 lucene 4.0.0 中,spellchecker 放在一个名为 lucene-suggest-4.0.0.jar 的包中,而不是 lucene-spellchecker-XXX.jar

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-25
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 2018-10-16
        相关资源
        最近更新 更多