1

我想输出一个已经更正的文本而不是下面的格式。

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());
}

所以输出应该像“A sentence with An error......”

4

1 回答 1

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;
}

通过遍历匹配,我能够将原始查询字符串(即带有错误的字符串)附加到一个新字符串,但是用 LanguageTool 中的第一个建议替换替换错误。

希望这可以帮助!

于 2016-11-15T12:19:50.517 回答