我正在使用LanguageTool和Eclipse。可以使用以下链接访问API:Click here。我能够从中获取文本输出,其中显示某些列具有拼写错误的单词但我无法获得输出,该输出是作为输入给出的拼写错误字符串的更正字符串版本。这是我的代码:
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
我该如何执行此操作?
答案 0 :(得分: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());
}
答案 1 :(得分:0)
使用match.getSuggestedReplacements()
之一并将原始输入字符串替换为match.getFromPos()
到match.getToPos()
的字符串。使用哪一个建议(如果有多个)无法自动确定,用户必须选择一个。