Search in sources :

Example 41 with RuleMatch

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

the class PatternRuleTest method testErrorTriggeringSentences.

private void testErrorTriggeringSentences(JLanguageTool languageTool, Language lang, AbstractPatternRule rule) throws IOException {
    for (ErrorTriggeringExample example : rule.getErrorTriggeringExamples()) {
        String sentence = cleanXML(example.getExample());
        List<RuleMatch> matches = getMatches(rule, sentence, languageTool);
        if (matches.size() == 0) {
            fail(lang + ": " + rule.getFullId() + ": Example sentence marked with 'triggers_error' didn't actually trigger an error: '" + sentence + "'");
        }
    }
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) ErrorTriggeringExample(org.languagetool.rules.ErrorTriggeringExample)

Example 42 with RuleMatch

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

the class PatternRuleTest method getMatches.

private List<RuleMatch> getMatches(Rule rule, String sentence, JLanguageTool languageTool) throws IOException {
    AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(sentence);
    RuleMatch[] matches = rule.match(analyzedSentence);
    if (CHECK_WITH_SENTENCE_SPLITTING) {
        // "real check" with sentence splitting:
        for (Rule r : languageTool.getAllActiveRules()) {
            languageTool.disableRule(r.getId());
        }
        languageTool.enableRule(rule.getId());
        List<RuleMatch> realMatches = languageTool.check(sentence);
        List<String> realMatchRuleIds = new ArrayList<>();
        for (RuleMatch realMatch : realMatches) {
            realMatchRuleIds.add(realMatch.getRule().getId());
        }
        for (RuleMatch match : matches) {
            String ruleId = match.getRule().getId();
            if (!match.getRule().isDefaultOff() && !realMatchRuleIds.contains(ruleId)) {
                System.err.println("WARNING: " + languageTool.getLanguage().getName() + ": missing rule match " + ruleId + " when splitting sentences for test sentence '" + sentence + "'");
            }
        }
    }
    return Arrays.asList(matches);
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence) RuleMatch(org.languagetool.rules.RuleMatch) ArrayList(java.util.ArrayList) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) DisambiguationPatternRule(org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule) Rule(org.languagetool.rules.Rule)

Example 43 with RuleMatch

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

the class RegexPatternRuleTest method testMatchWithMark.

@Test
public void testMatchWithMark() throws IOException {
    RegexPatternRule rule = new RegexPatternRule("ID", "desc", "msg: <suggestion>a suggestion \\1</suggestion>", "<suggestion>another suggestion \\2</suggestion>", TestTools.getDemoLanguage(), Pattern.compile("(fo.) (bar)"), 1);
    JLanguageTool lt = new JLanguageTool(TestTools.getDemoLanguage());
    RuleMatch[] matches2 = rule.match(lt.getAnalyzedSentence("This is foo bar"));
    assertThat(matches2.length, is(1));
    assertThat(matches2[0].getFromPos(), is(8));
    assertThat(matches2[0].getToPos(), is(11));
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) JLanguageTool(org.languagetool.JLanguageTool) Test(org.junit.Test)

Example 44 with RuleMatch

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

the class RegexPatternRuleTest method testMatch.

@Test
public void testMatch() throws IOException {
    RegexPatternRule rule = new RegexPatternRule("ID", "desc", "msg: <suggestion>a suggestion \\1</suggestion>", "<suggestion>another suggestion \\2</suggestion>", TestTools.getDemoLanguage(), Pattern.compile("(fo.) (bar)"), 0);
    JLanguageTool lt = new JLanguageTool(TestTools.getDemoLanguage());
    RuleMatch[] matches1 = rule.match(lt.getAnalyzedSentence("This is a test"));
    assertThat(matches1.length, is(0));
    RuleMatch[] matches2 = rule.match(lt.getAnalyzedSentence("This is foo bar"));
    assertThat(matches2.length, is(1));
    assertThat(matches2[0].getFromPos(), is(8));
    assertThat(matches2[0].getToPos(), is(15));
    RuleMatch[] matches3 = rule.match(lt.getAnalyzedSentence("This is foo bar and fou bar"));
    assertThat(matches3.length, is(2));
    assertThat(matches3[0].getFromPos(), is(8));
    assertThat(matches3[0].getToPos(), is(15));
    assertThat(matches3[0].getMessage(), is("msg: <suggestion>a suggestion foo</suggestion>"));
    assertThat(matches3[0].getSuggestedReplacements().toString(), is("[a suggestion foo, another suggestion bar]"));
    assertThat(matches3[1].getFromPos(), is(20));
    assertThat(matches3[1].getToPos(), is(27));
    assertThat(matches3[1].getMessage(), is("msg: <suggestion>a suggestion fou</suggestion>"));
    assertThat(matches3[1].getSuggestedReplacements().toString(), is("[a suggestion fou, another suggestion bar]"));
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) JLanguageTool(org.languagetool.JLanguageTool) Test(org.junit.Test)

Example 45 with RuleMatch

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

the class DifferentPunctuationRuleTest method testRule.

@Test
public void testRule() throws IOException {
    DifferentPunctuationRule rule = new DifferentPunctuationRule();
    RuleMatch[] matches;
    JLanguageTool srcLangTool = new JLanguageTool(TestTools.getDemoLanguage());
    JLanguageTool trgLangTool = new JLanguageTool(new FakeLanguage());
    rule.setSourceLanguage(TestTools.getDemoLanguage());
    // correct sentences:
    matches = rule.match(srcLangTool.getAnalyzedSentence("This is a test sentence!"), trgLangTool.getAnalyzedSentence("C'est la vie!"));
    assertEquals(0, matches.length);
    matches = rule.match(srcLangTool.getAnalyzedSentence("one sentence"), trgLangTool.getAnalyzedSentence("jedno zdanie"));
    assertEquals(0, matches.length);
    // incorrect sentences:
    matches = rule.match(srcLangTool.getAnalyzedSentence("This this is a test sentence."), trgLangTool.getAnalyzedSentence("This this is a test sentence!"));
    assertEquals(1, matches.length);
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) FakeLanguage(org.languagetool.FakeLanguage) JLanguageTool(org.languagetool.JLanguageTool) Test(org.junit.Test)

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