说明中有这些代码。如果能搜索到这个博文也行:
package taishan.languagetool;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.languagetool.JLanguageTool;
import org.languagetool.language.BritishEnglish;
import org.languagetool.language.Chinese;
import org.languagetool.rules.Rule;
import org.languagetool.rules.RuleMatch;
import org.languagetool.rules.spelling.SpellingCheckRule;
public class LanguageToolTest
{
private static void testEnglish() throws IOException
{
JLanguageTool langTool = new JLanguageTool(new BritishEnglish());
// comment in to use statistical ngram data:
//langTool.activateLanguageModelRules(new File("/data/google-ngram-data"));
List matches = langTool.check("A sentence with a error in the Hitchhiker's Guide tot he Galaxy");
for (RuleMatch match : matches) {
System.out.println("Potential error at characters " +
match.getFromPos() + "-" + match.getToPos() + ": " +
match.getMessage());
System.out.println("Suggested correction(s): " +
match.getSuggestedReplacements());
}
for (Rule rule : langTool.getAllActiveRules()) {
if (rule instanceof SpellingCheckRule) {
List wordsToIgnore = Arrays.asList("specialword", "myotherword");
((SpellingCheckRule)rule).addIgnoreTokens(wordsToIgnore);
}
}
for (Rule rule : langTool.getAllActiveRules()) {
if (rule instanceof SpellingCheckRule) {
((SpellingCheckRule)rule).acceptPhrases(Arrays.asList("foo bar", "producct namez"));
}
}
}
private static void testChinese() throws IOException
{
JLanguageTool langTool = new JLanguageTool(new Chinese());
List matches = langTool.check("泰山OFFICE要与WORD媲美!");
for (RuleMatch match : matches) {
System.out.println("可能拼写错误 " +
match.getFromPos() + "-" + match.getToPos() + ": " +
match.getMessage());
System.out.println("建议修正(s): " +
match.getSuggestedReplacements());
}
}
public static void main(String[] args)
{
try
{
long startTime = System.currentTimeMillis();
testEnglish();
System.out.println("testEnglish() cost="+(System.currentTimeMillis()-startTime));
testChinese();
System.out.println("testChinese() cost="+(System.currentTimeMillis()-startTime));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}