0

我将 LanguageTool 与 Eclipse 一起使用。可以使用以下链接访问 API:单击此处。我能够从中获取文本输出,显示某些列的单词拼写错误,但我无法获得作为输入给出的拼写错误字符串的更正字符串版本的输出。这是我的代码:

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 line " +
      match.getLine() + ", column " +
      match.getColumn() + ": " + match.getMessage());
  System.out.println("Suggested correction: " +
      match.getSuggestedReplacements());
}

得到的输出是:

Potential error at line 0, column 17: Use <suggestion>an</suggestion> instead of 'a' if the following word starts with a vowel sound, e.g. 'an article', 'an hour'
Suggested correction: [an]
Potential error at line 0, column 32: Possible spelling mistake found
Suggested correction: [Hitch-hiker]
Potential error at line 0, column 51: Did you mean <suggestion>to the</suggestion>?
Suggested correction: [to the]

我希望输出是输入字符串的更正版本:

A sentence with an error in the Hitchhiker's Guide to the Galaxy

我该怎么做?

4

2 回答 2

1

使用getFromPos(),getToPos()方法的示例:

private static final String TEST_SENTENCE = "A sentence with a error in the Hitchhiker's Guide tot he Galaxy";

public static void main(String[] args) throws Exception {

    StringBuffer correctSentence = new StringBuffer(TEST_SENTENCE);

    JLanguageTool langTool = new JLanguageTool(new BritishEnglish());
    List<RuleMatch> matches = langTool.check(TEST_SENTENCE);

    int offset = 0;
    for (RuleMatch match : matches) {

        correctSentence.replace(match.getFromPos() - offset, match.getToPos() - offset, match.getSuggestedReplacements().get(0));
        offset += (match.getToPos() - match.getFromPos() - match.getSuggestedReplacements().get(0).length());

    }

    System.out.println(correctSentence.toString());
}
于 2016-07-30T16:13:13.360 回答
0

使用其中之一match.getSuggestedReplacements()并将原始输入字符串替换为 from match.getFromPos()to match.getToPos()。不能自动确定要使用哪一个建议(如果有多个),用户必须选择一个。

于 2016-07-30T16:00:01.363 回答