如何在字符串中获得建议的句子作为LanguageTool中的输出? [英] How do I get the Suggested Sentence in a string as output in LanguageTool?
本文介绍了如何在字符串中获得建议的句子作为LanguageTool中的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将LanguageTool与Eclipse一起使用.可以使用以下链接访问该API:单击此处.我能够从中获得文本输出,该输出表明某些列的单词拼写错误,但是我无法获得输出,它是作为输入给出的拼写错误的字符串的校正后的字符串版本.这是我的代码:
I am using LanguageTool along with Eclipse. The API can be accessed using the link: Click here. 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());
}
获得的输出是:
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
我如何执行此操作?
推荐答案
使用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中的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文