Search in sources :

Example 16 with RuleMatch

use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.

the class RussianSimpleReplaceRuleTest method testRule.

@Test
public void testRule() throws IOException {
    RussianSimpleReplaceRule rule = new RussianSimpleReplaceRule(TestTools.getMessages("ru"));
    RuleMatch[] matches;
    JLanguageTool langTool = new JLanguageTool(new Russian());
    // correct sentences:
    matches = rule.match(langTool.getAnalyzedSentence("Рост кораллов тут самый быстрый,"));
    assertEquals(0, matches.length);
    matches = rule.match(langTool.getAnalyzedSentence("Книга была порвана."));
    assertEquals(0, matches.length);
    // incorrect sentences:
    matches = rule.match(langTool.getAnalyzedSentence("Книга была порвата."));
    assertEquals(1, matches.length);
    assertEquals(1, matches[0].getSuggestedReplacements().size());
    assertEquals("порвана", matches[0].getSuggestedReplacements().get(0));
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) Russian(org.languagetool.language.Russian) JLanguageTool(org.languagetool.JLanguageTool) Test(org.junit.Test)

Example 17 with RuleMatch

use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.

the class DifferentPunctuationRule method match.

@Override
public RuleMatch[] match(AnalyzedSentence sourceText, AnalyzedSentence targetText) throws IOException {
    AnalyzedTokenReadings[] translationTokens = targetText.getTokens();
    AnalyzedTokenReadings[] sourceTokens = sourceText.getTokens();
    AnalyzedTokenReadings lastTransTokenObj = translationTokens[translationTokens.length - 1];
    String lastTransToken = lastTransTokenObj.getToken();
    if ((".".equals(lastTransToken) || "?".equals(lastTransToken) || "!".equals(lastTransToken)) && !lastTransToken.equals(sourceTokens[sourceTokens.length - 1].getToken())) {
        int endPos = lastTransTokenObj.getEndPos();
        return new RuleMatch[] { new RuleMatch(this, 1, endPos, getMessage()) };
    }
    return new RuleMatch[0];
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) AnalyzedTokenReadings(org.languagetool.AnalyzedTokenReadings)

Example 18 with RuleMatch

use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.

the class PatternRuleMatcher method match.

@Override
public RuleMatch[] match(AnalyzedSentence sentence) throws IOException {
    List<RuleMatch> ruleMatches = new ArrayList<>();
    AnalyzedTokenReadings[] tokens = sentence.getTokensWithoutWhitespace();
    List<Integer> tokenPositions = new ArrayList<>(tokens.length + 1);
    int patternSize = patternTokenMatchers.size();
    int limit = Math.max(0, tokens.length - patternSize + 1);
    PatternTokenMatcher pTokenMatcher = null;
    int i = 0;
    int minOccurCorrection = getMinOccurrenceCorrection();
    while (i < limit + minOccurCorrection && !(rule.isSentStart() && i > 0)) {
        int skipShiftTotal = 0;
        boolean allElementsMatch = false;
        int firstMatchToken = -1;
        int lastMatchToken = -1;
        int firstMarkerMatchToken = -1;
        int lastMarkerMatchToken = -1;
        int prevSkipNext = 0;
        if (rule.isTestUnification()) {
            unifier.reset();
        }
        tokenPositions.clear();
        int minOccurSkip = 0;
        for (int k = 0; k < patternSize; k++) {
            PatternTokenMatcher prevTokenMatcher = pTokenMatcher;
            pTokenMatcher = patternTokenMatchers.get(k);
            pTokenMatcher.resolveReference(firstMatchToken, tokens, rule.getLanguage());
            int nextPos = i + k + skipShiftTotal - minOccurSkip;
            prevMatched = false;
            if (prevSkipNext + nextPos >= tokens.length || prevSkipNext < 0) {
                // SENT_END?
                prevSkipNext = tokens.length - (nextPos + 1);
            }
            int maxTok = Math.min(nextPos + prevSkipNext, tokens.length - (patternSize - k) + minOccurCorrection);
            for (int m = nextPos; m <= maxTok; m++) {
                allElementsMatch = !tokens[m].isImmunized() && testAllReadings(tokens, pTokenMatcher, prevTokenMatcher, m, firstMatchToken, prevSkipNext);
                if (pTokenMatcher.getPatternToken().getMinOccurrence() == 0) {
                    boolean foundNext = false;
                    for (int k2 = k + 1; k2 < patternSize; k2++) {
                        PatternTokenMatcher nextElement = patternTokenMatchers.get(k2);
                        boolean nextElementMatch = !tokens[m].isImmunized() && testAllReadings(tokens, nextElement, pTokenMatcher, m, firstMatchToken, prevSkipNext);
                        if (nextElementMatch) {
                            // this element doesn't match, but it's optional so accept this and continue
                            allElementsMatch = true;
                            minOccurSkip++;
                            tokenPositions.add(0);
                            foundNext = true;
                            break;
                        } else if (nextElement.getPatternToken().getMinOccurrence() > 0) {
                            break;
                        }
                    }
                    if (foundNext) {
                        break;
                    }
                }
                if (allElementsMatch) {
                    int skipForMax = skipMaxTokens(tokens, pTokenMatcher, firstMatchToken, prevSkipNext, prevTokenMatcher, m, patternSize - k - 1);
                    lastMatchToken = m + skipForMax;
                    int skipShift = lastMatchToken - nextPos;
                    tokenPositions.add(skipShift + 1);
                    prevSkipNext = translateElementNo(pTokenMatcher.getPatternToken().getSkipNext());
                    skipShiftTotal += skipShift;
                    if (firstMatchToken == -1) {
                        firstMatchToken = lastMatchToken - skipForMax;
                    }
                    if (firstMarkerMatchToken == -1 && pTokenMatcher.getPatternToken().isInsideMarker()) {
                        firstMarkerMatchToken = lastMatchToken - skipForMax;
                    }
                    if (pTokenMatcher.getPatternToken().isInsideMarker()) {
                        lastMarkerMatchToken = lastMatchToken;
                    }
                    break;
                }
            }
            if (!allElementsMatch) {
                break;
            }
        }
        if (allElementsMatch && tokenPositions.size() == patternSize) {
            RuleMatch ruleMatch = createRuleMatch(tokenPositions, tokens, firstMatchToken, lastMatchToken, firstMarkerMatchToken, lastMarkerMatchToken);
            if (ruleMatch != null) {
                ruleMatches.add(ruleMatch);
            }
        }
        i++;
    }
    RuleMatchFilter maxFilter = new RuleWithMaxFilter();
    List<RuleMatch> filteredMatches = maxFilter.filter(ruleMatches);
    return filteredMatches.toArray(new RuleMatch[filteredMatches.size()]);
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) ArrayList(java.util.ArrayList) RuleWithMaxFilter(org.languagetool.rules.RuleWithMaxFilter) RuleMatchFilter(org.languagetool.rules.RuleMatchFilter) AnalyzedTokenReadings(org.languagetool.AnalyzedTokenReadings)

Example 19 with RuleMatch

use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.

the class RegexPatternRule method match.

@Override
public RuleMatch[] match(AnalyzedSentence sentenceObj) throws IOException {
    String sentence = sentenceObj.getText();
    Matcher matcher = pattern.matcher(sentence);
    int startPos = 0;
    List<RuleMatch> matches = new ArrayList<>();
    while (matcher.find(startPos)) {
        String msg = replaceBackRefs(matcher, message);
        boolean sentenceStart = matcher.start(0) == 0;
        List<String> suggestions = extractSuggestions(matcher, msg);
        List<String> matchSuggestions = getMatchSuggestions(sentence, matcher);
        msg = replaceMatchElements(msg, matchSuggestions);
        int markStart = matcher.start(markGroup);
        int markEnd = matcher.end(markGroup);
        RuleMatch ruleMatch = new RuleMatch(this, markStart, markEnd, msg, null, sentenceStart, null);
        List<String> allSuggestions = new ArrayList<>();
        if (matchSuggestions.size() > 0) {
            allSuggestions.addAll(matchSuggestions);
        } else {
            allSuggestions.addAll(suggestions);
            List<String> extendedSuggestions = extractSuggestions(matcher, getSuggestionsOutMsg());
            allSuggestions.addAll(extendedSuggestions);
        }
        ruleMatch.setSuggestedReplacements(allSuggestions);
        matches.add(ruleMatch);
        startPos = matcher.end();
    }
    return matches.toArray(new RuleMatch[matches.size()]);
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 20 with RuleMatch

use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.

the class HunspellRule method match.

@Override
public RuleMatch[] match(AnalyzedSentence sentence) throws IOException {
    List<RuleMatch> ruleMatches = new ArrayList<>();
    if (needsInit) {
        init();
    }
    if (hunspellDict == null) {
        // some languages might not have a dictionary, be silent about it
        return toRuleMatchArray(ruleMatches);
    }
    String[] tokens = tokenizeText(getSentenceTextWithoutUrlsAndImmunizedTokens(sentence));
    // starting with the first token to skip the zero-length START_SENT
    int len = sentence.getTokens()[1].getStartPos();
    for (int i = 0; i < tokens.length; i++) {
        String word = tokens[i];
        if (ignoreWord(Arrays.asList(tokens), i) || ignoreWord(word)) {
            len += word.length() + 1;
            continue;
        }
        if (isMisspelled(word)) {
            RuleMatch ruleMatch = new RuleMatch(this, len, len + word.length(), messages.getString("spelling"), messages.getString("desc_spelling_short"));
            List<String> suggestions = getSuggestions(word);
            suggestions.addAll(0, getAdditionalTopSuggestions(suggestions, word));
            suggestions.addAll(getAdditionalSuggestions(suggestions, word));
            if (!suggestions.isEmpty()) {
                filterSuggestions(suggestions);
                ruleMatch.setSuggestedReplacements(suggestions);
            }
            ruleMatches.add(ruleMatch);
        }
        len += word.length() + 1;
    }
    return toRuleMatchArray(ruleMatches);
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) 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