使用jOrtho拼写检查器

时间:2013-03-10 21:43:21

标签: java spell-checking

如何使用jOrtho拼写检查程序?我从wiktionary下载了最新的字典(XML文件)。现在如何编译并在我的程序中实现它?

3 个答案:

答案 0 :(得分:3)

我找到了解决方案,这些是添加拼写检查功能的步骤。 首先在这里下载jar和预编译的字典表单:http://sourceforge.net/projects/jortho/files/

以下是代码段:

    SpellChecker.setUserDictionaryProvider(new FileUserDictionary());      
    SpellChecker.registerDictionaries(this.getClass().getResource("/dictionary"), "en");
    SpellChecker.register(messageWriter);

这里,messageWriter是JEditor窗格。 请参阅文档说明。将dictionaries.cnf和dictionary_en.ortho文件放在src / dictionary文件夹中。

您还可以操作弹出菜单选项。以下是我所做的一个例子:

    SpellCheckerOptions sco=new SpellCheckerOptions();
    sco.setCaseSensitive(true);
    sco.setSuggestionsLimitMenu(10);
    JPopupMenu popup = SpellChecker.createCheckerPopup(sco);
    messageWriter.addMouseListener(new PopupListener(popup));

将选项限制为10.请参阅文档。

答案 1 :(得分:2)

首先,您必须下载该库。 http://sourceforge.net/projects/jortho/files/JOrtho%20Library/0.5/他们的zip文件应包含一个或多个.jar文件。您需要将这些添加到类路径中。您这样做的方式取决于您的开发方式。如果您正在使用Netbeans,那么它与您在Eclipse中的方式不同。

如果他们的zip文件包含其API的文档,您应该可以使用它来将其添加到Java程序中。如果没有,您可能需要寻找替代方案。看起来他们网站上的链接已经死了。这通常是个坏兆头。

还有其他选择。我花了很长时间才找到这个http://jazzy.sourceforge.net/。看起来它是Lucene内部使用的那个。它还拥有比jortho更好的许可证。

祝你好运。

答案 2 :(得分:0)

用于应用程序whi gui:

public class Checker {

private static Map<String, Method> methods;

public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException, InvocationTargetException, IllegalAccessException {
    SpellChecker.registerDictionaries(Checker.class.getResource("/dictionary/"), "en");

    methods = new HashMap<>();

    setAccessibleMethod(LanguageBundle.class, "get", Locale.class);
    setAccessibleMethod(LanguageBundle.class,
            "existInDictionary",
            String.class,
            Checker.class.getClassLoader().loadClass("com.inet.jortho.Dictionary"),
            com.inet.jortho.SpellCheckerOptions.class,
            boolean.class
    );
    setAccessibleMethod(SpellChecker.class, "getCurrentDictionary");

    while (SpellChecker.getCurrentLocale() == null) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    Object dictionary = invokeMethod(SpellChecker.class, "getCurrentDictionary", null);

    LanguageBundle bundle = (LanguageBundle) invokeMethod(LanguageBundle.class, "get", null, SpellChecker.getCurrentLocale());

    Set<String> errors = new HashSet<>();
    StringTokenizer st = new StringTokenizer("A sentence with a error in the Hitchhiker's Guide tot he Galaxy");
    boolean newSentence = true;
    while (st.hasMoreTokens()) {
        String word = st.nextToken();
        boolean b = true;
        boolean nextNewSentence = false;
        if (word.length() > 1) {
            if ('.' == word.charAt(word.length() - 1)) {
                nextNewSentence = true;
                word = word.substring(0, word.length() - 1);
            }
            b = (Boolean) invokeMethod(LanguageBundle.class, "existInDictionary", bundle,
                    word,
                    dictionary,
                    SpellChecker.getOptions(),
                    newSentence);
        }
        if (!b)
            errors.add(word);
        newSentence = nextNewSentence;
    }
    System.out.println(StringUtils.join(errors, " , "));
}

private static void setAccessibleMethod(Class<?> cls, String name, Class<?>... parameterTypes) throws NoSuchMethodException {
    Method method = cls.getDeclaredMethod(name, parameterTypes);
    method.setAccessible(true);
    methods.put(cls.getName() + "." + name, method);
}

private static Object invokeMethod(Class<?> cls, String name, Object obj, Object... args) throws InvocationTargetException, IllegalAccessException {
    return methods.get(cls.getName() + "." + name).invoke(obj, args);
}

}