我想输出已经更正的文字而不是下面的格式。
JLanguageTool langTool = new JLanguageTool(new BritishEnglish());
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());
}
所以输出应该像&#34;带有错误的句子......&#34;
答案 0 :(得分:2)
当我需要为我正在开发的搜索引擎创建“你是不是意味着”时,我遇到了这个问题。以下代码似乎可以解决问题:
public String didYouMean(String query) {
JLanguageTool langTool = new JLanguageTool(new BritishEnglish());
List<RuleMatch> matches = null;
try {
matches = langTool.check(query);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String didYouMean = "";
int lastPos = 0;
for (RuleMatch match : matches) {
didYouMean += query.substring(lastPos, match.getFromPos());
didYouMean += match.getSuggestedReplacements().get(0);
lastPos = match.getToPos();
}
if (lastPos < query.length()) {
didYouMean += query.substring(lastPos, query.length());
}
return didYouMean;
}
通过迭代匹配,我能够将原始查询字符串(即带有错误的字符串)附加到新的String,但用LanguageTool中的第一个建议替换替换错误。
希望这有帮助!