use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class MorfologikSpellerRule method getRuleMatches.
protected List<RuleMatch> getRuleMatches(String word, int startPos) throws IOException {
List<RuleMatch> ruleMatches = new ArrayList<>();
if (isMisspelled(speller1, word) || isProhibited(word)) {
RuleMatch ruleMatch = new RuleMatch(this, startPos, startPos + word.length(), messages.getString("spelling"), messages.getString("desc_spelling_short"));
List<String> suggestions = speller1.getSuggestions(word);
if (suggestions.size() == 0 && word.length() >= 5) {
// speller1 uses a maximum edit distance of 1, it won't find suggestion for "garentee", "greatful" etc.
suggestions.addAll(speller2.getSuggestions(word));
}
suggestions.addAll(0, getAdditionalTopSuggestions(suggestions, word));
suggestions.addAll(getAdditionalSuggestions(suggestions, word));
if (!suggestions.isEmpty()) {
filterSuggestions(suggestions);
ruleMatch.setSuggestedReplacements(orderSuggestions(suggestions, word));
}
ruleMatches.add(ruleMatch);
}
return ruleMatches;
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class Tools method correctTextFromMatches.
/**
* @since 2.3
*/
public static String correctTextFromMatches(String contents, List<RuleMatch> matches) {
StringBuilder sb = new StringBuilder(contents);
List<String> errors = new ArrayList<>();
for (RuleMatch rm : matches) {
List<String> replacements = rm.getSuggestedReplacements();
if (!replacements.isEmpty()) {
errors.add(sb.substring(rm.getFromPos(), rm.getToPos()));
}
}
int offset = 0;
int counter = 0;
for (RuleMatch rm : matches) {
List<String> replacements = rm.getSuggestedReplacements();
if (!replacements.isEmpty()) {
//make sure the error hasn't been already corrected:
if (rm.getFromPos() - offset >= 0 && rm.getToPos() - offset >= rm.getFromPos() - offset && errors.get(counter).equals(sb.substring(rm.getFromPos() - offset, rm.getToPos() - offset))) {
sb.replace(rm.getFromPos() - offset, rm.getToPos() - offset, replacements.get(0));
offset += rm.getToPos() - rm.getFromPos() - replacements.get(0).length();
}
counter++;
}
}
return sb.toString();
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class Tools method checkBitext.
/**
* Checks the bilingual input (bitext).
*
* @param src Source text.
* @param trg Target text.
* @param srcLt Source JLanguageTool (used to analyze the text).
* @param trgLt Target JLanguageTool (used to analyze the text).
* @param bRules Bilingual rules used in addition to target standard rules.
* @return The list of rule matches on the bitext.
* @since 1.0.1
*/
public static List<RuleMatch> checkBitext(String src, String trg, JLanguageTool srcLt, JLanguageTool trgLt, List<BitextRule> bRules) throws IOException {
AnalyzedSentence srcText = srcLt.getAnalyzedSentence(src);
AnalyzedSentence trgText = trgLt.getAnalyzedSentence(trg);
List<Rule> nonBitextRules = trgLt.getAllRules();
for (Rule rule : nonBitextRules) {
rule.reset();
}
List<RuleMatch> ruleMatches = trgLt.checkAnalyzedSentence(JLanguageTool.ParagraphHandling.NORMAL, nonBitextRules, trgText);
for (BitextRule bRule : bRules) {
RuleMatch[] curMatch = bRule.match(srcText, trgText);
if (curMatch != null && curMatch.length > 0) {
// adjust positions for bitext rules
for (RuleMatch match : curMatch) {
if (match.getColumn() < 0) {
match.setColumn(1);
}
if (match.getEndColumn() < 0) {
// we count from 0
match.setEndColumn(trg.length() + 1);
}
if (match.getLine() < 0) {
match.setLine(1);
}
if (match.getEndLine() < 0) {
match.setEndLine(1);
}
ruleMatches.add(match);
}
}
}
return ruleMatches;
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class MultiThreadedJLanguageToolTest method getRuleMatchIds.
private List<String> getRuleMatchIds(JLanguageTool langTool) throws IOException {
String input = "A small toast. No error here. Foo go bar. First goes last there, please!";
List<RuleMatch> matches = langTool.check(input);
List<String> ruleMatchIds = new ArrayList<>();
for (RuleMatch match : matches) {
ruleMatchIds.add(match.getRule().getId());
}
return ruleMatchIds;
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class DashRule method match.
@Override
public RuleMatch[] match(AnalyzedSentence sentence) throws IOException {
List<RuleMatch> matches = new ArrayList<>();
for (PatternRule dashRule : dashRules) {
for (RuleMatch ruleMatch : dashRule.match(sentence)) {
RuleMatch rm = new RuleMatch(this, ruleMatch.getFromPos(), ruleMatch.getToPos(), ruleMatch.getMessage(), ruleMatch.getShortMessage(), false, "");
matches.add(rm);
}
}
return matches.toArray(new RuleMatch[matches.size()]);
}
Aggregations