2

How to use jOrtho spell checker? I have downloaded the latest dictionary (XML file) from wiktionary. Now how to compile it and implement it in my program?

4

3 回答 3

3

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/

Following is the code snippet:

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

Here, messageWriter is JEditor pane. Refer to documentation explanation. Put the dictionaries.cnf and dictionary_en.ortho files inside src/dictionary folder.

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.

于 2013-04-19T11:28:27.063 回答
2

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. 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.

If their zip file includes documentation for their API, you should be able to use that to add it to you Java program. 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. It looks like it's the one used by Lucene internally. It also has a better license than jortho does.

Good luck.

于 2013-03-10T21:57:27.667 回答
0

for use in app whiout 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);
}

}
于 2019-04-19T16:10:17.377 回答