简体   繁体   English

使用jOrtho拼写检查器

[英]Using jOrtho spell checker

How to use jOrtho spell checker? 如何使用jOrtho拼写检查器? I have downloaded the latest dictionary (XML file) from wiktionary. 我已经从wiktionary下载了最新的字典(XML文件)。 Now how to compile it and implement it in my program? 现在如何编译它并在我的程序中实现它?

I found the solution and these are the steps to add spell checking functionality. 我找到了解决方案,这些是添加拼写检查功能的步骤。 First download the jar and pre-compiled dictionary form here: http://sourceforge.net/projects/jortho/files/ 首先在此处下载jar和预编译的字典形式: http : //sourceforge.net/projects/jortho/files/

Following is the code snippet: 以下是代码段:

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

Here, messageWriter is JEditor pane. 在这里,messageWriter是JEditor窗格。 Refer to documentation explanation. 请参阅文档说明。 Put the dictionaries.cnf and dictionary_en.ortho files inside src/dictionary folder. 将dictionaries.cnf和dictionary_en.ortho文件放在src / dictionary文件夹中。

You can also manipulate the pop-up menu options. 您还可以操纵弹出菜单选项。 Here is an example what I have done: 这是我所做的一个示例:

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

Restricting the options to 10. See docs. 将选项限制为10。请参阅文档。

First, you have to download the library. 首先,您必须下载该库。 http://sourceforge.net/projects/jortho/files/JOrtho%20Library/0.5/ Their zip file should include one or more .jar files. http://sourceforge.net/projects/jortho/files/JOrtho%20Library/0.5/他们的zip文件应包含一个或多个.jar文件。 You will need to add these into your classpath. 您将需要将它们添加到您的类路径中。 The way you do this depends on how you do your development. 您执行此操作的方式取决于您的开发方式。 If you're using Netbeans, it's different than the way you would do it in Eclipse. 如果您使用的是Netbeans,则与在Eclipse中的使用方式有所不同。

If their zip file includes documentation for their API, you should be able to use that to add it to you Java program. 如果他们的zip文件包含其API的文档,则您应该能够使用该文件将其添加到Java程序中。 If it does not, you might need to look for an alternative. 如果不是,则可能需要寻找替代方法。 It looks like the links on their site are dead. 他们网站上的链接似乎已消失。 Which is usually a bad sign. 通常这是一个不好的迹象。

There are alternatives. 还有其他选择。 It didn't take me long to find this one http://jazzy.sourceforge.net/ for example. 例如,很快就找到了这个http://jazzy.sourceforge.net/ It looks like it's the one used by Lucene internally. 看起来这是Lucene内部使用的一种。 It also has a better license than jortho does. 它的许可证也比jortho更好。

Good luck. 祝好运。

for use in app whiout gui: 用于应用程序whit 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);
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM