2016-07-30 3 views
0

私はEclipseと一緒にLanguageToolを使用しています。 APIは、Click hereのリンクを使用してアクセスできます。私は、特定の列にスペルミスがあることを示すテキスト出力を得ることができますが、入力として与えられたスペルミスの修正された文字列バージョンである出力を得ることができません。ここに私のコードは次のとおりです。LanguageToolの出力として文字列にSuggested Sentenceを出力するにはどうすればよいですか?

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

得られる出力は次のとおりです。

私はどのように
A sentence with an error in the Hitchhiker's Guide to the Galaxy 

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] 

私のように、出力は入力文字列の修正バージョンになりたいですこれを行う?

答えて

1

例:情報について

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

ありがとうございます。 – Ravichandra

0

match.getSuggestedReplacements()のいずれかを使用し、元の入力文字列をmatch.getFromPos()match.getToPos()の文字列で置き換えます。使用する提案(複数ある場合)のどれを自動的に決定できないかは、ユーザーが選択する必要があります。 getFromPos()getToPos()方法を使用して

+0

ねえ、ありがとう。私はそれを試みます。それで、それを実行する直接的な声明はありません、そうですか?文字列操作自体を通して? – Ravichandra

+0

@Ravichandraいいえ、このタスクを実行するための直接の決定が存在します。私の答えを見てください。 –

+0

ありがとう! :) – Ravichandra

関連する問題