[英]How do I get the Suggested Sentence in a string as output in LanguageTool?
I am using LanguageTool along with Eclipse. 我正在将LanguageTool与Eclipse一起使用。 The API can be accessed using the link: Click here .
可以使用链接访问API: 单击此处 。 I am able to get the text output from it which shows that certain columns have misspelled words but I am not able to get the output which is the corrected string version of the misspelled string given as the input.
我能够从中获得文本输出,该输出表明某些列的单词拼写错误,但是我无法获得输出,它是作为输入给出的拼写错误的字符串的校正后的字符串版本。 Here is my code:
这是我的代码:
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());
}
The output obtained is: 获得的输出为:
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]
I would like the output to be the corrected version of the input string as: 我希望输出是输入字符串的正确版本,如下所示:
A sentence with an error in the Hitchhiker's Guide to the Galaxy
How do I perform this? 我该如何执行呢?
Example with using getFromPos()
, getToPos()
methods: 使用
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());
}
Use one of match.getSuggestedReplacements()
and replace the original input string with that from match.getFromPos()
to match.getToPos()
. 使用的一个
match.getSuggestedReplacements()
并从与替换原来的输入字符串match.getFromPos()
来match.getToPos()
Which one of the suggestions to use (if there's more than one) cannot be determined automatically, the user has to select one. 不能自动确定要使用哪个建议(如果有多个),用户必须选择一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.