LanguageTool最简范例代码
Posted 柳鲲鹏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LanguageTool最简范例代码相关的知识,希望对你有一定的参考价值。
说明中有这些代码。如果能搜索到这个博文也行:
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<RuleMatch> 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<String> 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<RuleMatch> 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();
}
}
}
以上是关于LanguageTool最简范例代码的主要内容,如果未能解决你的问题,请参考以下文章