如何在 LanguageTool 中获取字符串中的建议句子作为输出?
How do I get the Suggested Sentence in a string as output in LanguageTool?
我正在使用 LanguageTool 和 Eclipse。 API 可以使用 link 访问: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
我该如何执行此操作?
使用 match.getSuggestedReplacements()
之一并将原始输入字符串替换为 match.getFromPos()
到 match.getToPos()
中的字符串。无法自动确定使用哪一个建议(如果有多个建议),用户必须select一个。
使用 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());
}
我正在使用 LanguageTool 和 Eclipse。 API 可以使用 link 访问: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
我该如何执行此操作?
使用 match.getSuggestedReplacements()
之一并将原始输入字符串替换为 match.getFromPos()
到 match.getToPos()
中的字符串。无法自动确定使用哪一个建议(如果有多个建议),用户必须select一个。
使用 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());
}