简体   繁体   English

如何在LanguageTool中以字符串形式获取建议的句子?

[英]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.

相关问题 如何删除字符串中的空格(句子) - How do I remove spaces in a String (sentence) 如何从句子中取出中缀? - How do I get the Infix out of a sentence? 在 LanguageTool 中,如何创建字典并将其用于拼写检查? - In LanguageTool, how do you create a dictionary and use it for spell checking? 如何返回给定输入句子(字符串)的单词数组? - How do I return an array of words given an input sentence (string)? 使用LanguageTool检测无意义和/或语法不正确的句子 - Detecting meaningless and/or grammatically incorrect sentence with LanguageTool 如何解析句子并获取句子的标签和单词的输出? - How to parse a sentence and get output of tags and words for a sentence? 我如何每个段落获得一个句子(该句子是随机抽取的) - how do i get one sentence per paragraph (the sentence taken randomly) 我如何获得 output 之类的 output 字符串? - How I get the output like output string? 为什么我得到 null 作为字符串 output? - Why do I get null as the String output? 当我输入单个单词字符串而不是句子中的单个字符串时,如何停止 do while 循环? 不知道怎么说 - How do I stop a do while loop, when I input a single word string, instead of that single string in a sentence sentence? not sure how to word it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM