![](/img/trans.png)
[英]Suggestions for an embeddable scripting language for iPhone and Java app?
[英]Automatic application of suggestions in Java language tool
我想輸出一個已經更正的文本,而不是下面的格式。
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 characters " +
match.getFromPos() + "-" + match.getToPos() + ": " +
match.getMessage());
System.out.println("Suggested correction(s): " +
match.getSuggestedReplacements());
}
因此輸出應類似於“有錯誤的句子……”
當我需要為我正在開發的搜索引擎創建一個“您的意思是”時,我遇到了這個問題。 以下代碼似乎可以解決問題:
public String didYouMean(String query) {
JLanguageTool langTool = new JLanguageTool(new BritishEnglish());
List<RuleMatch> matches = null;
try {
matches = langTool.check(query);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String didYouMean = "";
int lastPos = 0;
for (RuleMatch match : matches) {
didYouMean += query.substring(lastPos, match.getFromPos());
didYouMean += match.getSuggestedReplacements().get(0);
lastPos = match.getToPos();
}
if (lastPos < query.length()) {
didYouMean += query.substring(lastPos, query.length());
}
return didYouMean;
}
通過遍歷匹配項,我能夠將原始查詢字符串(即帶有錯誤的字符串)追加到新的String上,但是用LanguageTool中建議的第一個替換字符串替換了錯誤。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.