Search in sources :

Example 21 with RuleMatch

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;
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch)

Example 22 with RuleMatch

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();
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch)

Example 23 with RuleMatch

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;
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence) RuleMatch(org.languagetool.rules.RuleMatch) BitextRule(org.languagetool.rules.bitext.BitextRule) BitextPatternRule(org.languagetool.rules.patterns.bitext.BitextPatternRule) Rule(org.languagetool.rules.Rule) BitextRule(org.languagetool.rules.bitext.BitextRule)

Example 24 with RuleMatch

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;
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch)

Example 25 with RuleMatch

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()]);
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) PatternRule(org.languagetool.rules.patterns.PatternRule) ArrayList(java.util.ArrayList)

Aggregations

RuleMatch (org.languagetool.rules.RuleMatch)144 Test (org.junit.Test)64 JLanguageTool (org.languagetool.JLanguageTool)54 ArrayList (java.util.ArrayList)30 AnalyzedTokenReadings (org.languagetool.AnalyzedTokenReadings)14 Rule (org.languagetool.rules.Rule)14 Language (org.languagetool.Language)10 PatternRule (org.languagetool.rules.patterns.PatternRule)10 AnalyzedSentence (org.languagetool.AnalyzedSentence)8 Ukrainian (org.languagetool.language.Ukrainian)8 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)8 Matcher (java.util.regex.Matcher)7 English (org.languagetool.language.English)7 IOException (java.io.IOException)6 Catalan (org.languagetool.language.Catalan)6 Polish (org.languagetool.language.Polish)6 GermanyGerman (org.languagetool.language.GermanyGerman)5 AnnotatedText (org.languagetool.markup.AnnotatedText)5 PatternToken (org.languagetool.rules.patterns.PatternToken)5 AnalyzedToken (org.languagetool.AnalyzedToken)4