How do I get the Suggested Sentence in a string as output in LanguageTool?

不想你离开。 提交于 2019-12-25 09:05:08

问题


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());
}

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?


回答1:


Example with using getFromPos(), getToPos() methods:

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());
}



回答2:


Use one of match.getSuggestedReplacements() and replace the original input string with that from match.getFromPos() to 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.



来源:https://stackoverflow.com/questions/38675624/how-do-i-get-the-suggested-sentence-in-a-string-as-output-in-languagetool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!