【问题标题】:Automatic application of suggestions in Java language tool在 Java 语言工具中自动应用建议
【发布时间】:2016-10-20 02:13:52
【问题描述】:

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

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

【问题讨论】:

    标签: java languagetool


    【解决方案1】:

    当我需要为我正在开发的搜索引擎创建“您的意思是”时,我遇到了这个问题。以下代码似乎可以解决问题:

    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 建议的第一个替换替换错误。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      相关资源
      最近更新 更多