2016-07-30 55 views
0

我正在使用LanguageTool和Eclipse。 API可以通過鏈接訪問:Click here。我能夠從中得到文本輸出,其中顯示某些列拼寫錯誤的單詞,但我無法獲得作爲輸入提供拼寫錯誤字符串的正確字符串版本的輸出。這裏是我的代碼:如何在LanguageTool中輸出字符串中的建議句子?

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] 

我想輸出是輸入字符串作爲修正版本:

A sentence with an error in the Hitchhiker's Guide to the Galaxy 

我如何執行此操作?

回答

1

實施例使用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()); 
} 
+0

非常感謝。 – Ravichandra

0

使用match.getSuggestedReplacements()之一,並將原始輸入字符串替換爲match.getFromPos()match.getToPos()。使用哪個建議(如果有多個建議)不能自動確定,用戶必須選擇一個建議。

+0

嘿,感謝的信息。我會嘗試。所以,沒有任何直接的聲明來執行它,對嗎?通過字符串操作本身? – Ravichandra

+0

@Ravichandra不,直接決定執行此任務存在。看看我的答案。 –

+0

非常感謝! :) – Ravichandra

相關問題