Search in sources :

Example 61 with Rule

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

the class PatternRuleLoaderTest method testGetRules.

@Test
public void testGetRules() throws Exception {
    PatternRuleLoader prg = new PatternRuleLoader();
    String name = "/xx/grammar.xml";
    List<AbstractPatternRule> rules = prg.getRules(JLanguageTool.getDataBroker().getFromRulesDirAsStream(name), name);
    assertTrue(rules.size() >= 30);
    Rule demoRule1 = getRuleById("DEMO_RULE", rules);
    assertEquals("http://fake-server.org/foo-bar-error-explained", demoRule1.getUrl().toString());
    assertEquals("[This is <marker>fuu bah</marker>.]", demoRule1.getCorrectExamples().toString());
    List<IncorrectExample> incorrectExamples = demoRule1.getIncorrectExamples();
    assertEquals(1, incorrectExamples.size());
    assertEquals("This is <marker>foo bar</marker>.", incorrectExamples.get(0).getExample());
    Rule demoRule2 = getRuleById("API_OUTPUT_TEST_RULE", rules);
    assertNull(demoRule2.getUrl());
    assertEquals(ITSIssueType.Uncategorized, demoRule1.getLocQualityIssueType());
    assertEquals("tag inheritance failed", ITSIssueType.Addition, getRuleById("TEST_GO", rules).getLocQualityIssueType());
    assertEquals("tag inheritance overwrite failed", ITSIssueType.Uncategorized, getRuleById("TEST_PHRASES1", rules).getLocQualityIssueType());
    assertEquals("tag inheritance overwrite failed", ITSIssueType.Characters, getRuleById("test_include", rules).getLocQualityIssueType());
    List<Rule> groupRules1 = getRulesById("test_spacebefore", rules);
    assertEquals("tag inheritance form category failed", ITSIssueType.Addition, groupRules1.get(0).getLocQualityIssueType());
    assertEquals("tag inheritance overwrite failed", ITSIssueType.Duplication, groupRules1.get(1).getLocQualityIssueType());
    List<Rule> groupRules2 = getRulesById("test_unification_with_negation", rules);
    assertEquals("tag inheritance from rulegroup failed", ITSIssueType.Grammar, groupRules2.get(0).getLocQualityIssueType());
    Set<String> categories = getCategoryNames(rules);
    assertEquals(4, categories.size());
    assertTrue(categories.contains("misc"));
    assertTrue(categories.contains("otherCategory"));
    assertTrue(categories.contains("Test tokens with min and max attributes"));
    assertTrue(categories.contains("A category that's off by default"));
    PatternRule demoRuleWithChunk = (PatternRule) getRuleById("DEMO_CHUNK_RULE", rules);
    List<PatternToken> patternTokens = demoRuleWithChunk.getPatternTokens();
    assertEquals(2, patternTokens.size());
    assertEquals(null, patternTokens.get(1).getPOStag());
    assertEquals(new ChunkTag("B-NP-singular"), patternTokens.get(1).getChunkTag());
    List<Rule> orRules = getRulesById("GROUP_WITH_URL", rules);
    assertEquals(3, orRules.size());
    assertEquals("http://fake-server.org/rule-group-url", orRules.get(0).getUrl().toString());
    assertEquals("http://fake-server.org/rule-group-url-overwrite", orRules.get(1).getUrl().toString());
    assertEquals("http://fake-server.org/rule-group-url", orRules.get(2).getUrl().toString());
    assertEquals("short message on rule group", ((PatternRule) orRules.get(0)).getShortMessage());
    assertEquals("overwriting short message", ((PatternRule) orRules.get(1)).getShortMessage());
    assertEquals("short message on rule group", ((PatternRule) orRules.get(2)).getShortMessage());
    // make sure URLs don't leak to the next rule:
    List<Rule> orRules2 = getRulesById("OR_GROUPS", rules);
    for (Rule rule : orRules2) {
        assertNull("http://fake-server.org/rule-group-url", rule.getUrl());
    }
    Rule nextRule = getRuleById("DEMO_CHUNK_RULE", rules);
    assertNull("http://fake-server.org/rule-group-url", nextRule.getUrl());
}
Also used : ChunkTag(org.languagetool.chunking.ChunkTag) Rule(org.languagetool.rules.Rule) IncorrectExample(org.languagetool.rules.IncorrectExample) Test(org.junit.Test)

Example 62 with Rule

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

the class PatternRuleTest method testCorrectSentences.

private void testCorrectSentences(JLanguageTool languageTool, JLanguageTool allRulesLanguageTool, Language lang, AbstractPatternRule rule) throws IOException {
    List<CorrectExample> goodSentences = rule.getCorrectExamples();
    // necessary for XML Pattern rules containing <or>
    List<AbstractPatternRule> rules = allRulesLanguageTool.getPatternRulesByIdAndSubId(rule.getId(), rule.getSubId());
    for (CorrectExample goodSentenceObj : goodSentences) {
        // enable indentation use
        String goodSentence = goodSentenceObj.getExample().replaceAll("[\\n\\t]+", "");
        goodSentence = cleanXML(goodSentence);
        assertTrue(lang + ": Empty correct example in rule " + rule.getFullId(), goodSentence.trim().length() > 0);
        boolean isMatched = false;
        // necessary for XML Pattern rules containing <or>
        for (Rule auxRule : rules) {
            isMatched = isMatched || match(auxRule, goodSentence, languageTool);
        }
        if (isMatched) {
            AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(goodSentence);
            StringBuilder sb = new StringBuilder("Analyzed token readings:");
            for (AnalyzedTokenReadings atr : analyzedSentence.getTokens()) {
                sb.append(" ").append(atr);
            }
            fail(lang + ": Did not expect error in:\n" + "  " + goodSentence + "\n" + "  " + sb + "\n" + "Matching Rule: " + rule.getFullId());
        }
    // avoid matches with all the *other* rules:
    /*
      List<RuleMatch> matches = allRulesLanguageTool.check(goodSentence);
      for (RuleMatch match : matches) {
        System.err.println("WARN: " + lang.getShortCode() + ": '" + goodSentence + "' did not match "
                + rule.getId() + " but matched " + match.getRule().getId());
      }
      */
    }
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence) CorrectExample(org.languagetool.rules.CorrectExample) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) DisambiguationPatternRule(org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule) Rule(org.languagetool.rules.Rule) AnalyzedTokenReadings(org.languagetool.AnalyzedTokenReadings)

Example 63 with Rule

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

the class SpellingCheckRuleTest method testIgnorePhrases.

@Test
public void testIgnorePhrases() throws IOException {
    JLanguageTool lt = new JLanguageTool(new GermanyGerman());
    assertThat(lt.check("Ein Test mit Auriensis Fantasiewortus").size(), is(2));
    for (Rule rule : lt.getAllActiveRules()) {
        if (rule instanceof SpellingCheckRule) {
            ((SpellingCheckRule) rule).acceptPhrases(Arrays.asList("Auriensis Fantasiewortus", "fudeldu laberwort"));
        } else {
            lt.disableRule(rule.getId());
        }
    }
    assertThat(lt.check("Ein Test mit Auriensis Fantasiewortus").size(), is(0));
    // the words on their own are not ignored
    assertThat(lt.check("Ein Test mit Auriensis und Fantasiewortus").size(), is(2));
    assertThat(lt.check("fudeldu laberwort").size(), is(0));
    // Uppercase at sentence start is okay
    assertThat(lt.check("Fudeldu laberwort").size(), is(0));
    // Different case somewhere other than at sentence start is not okay
    assertThat(lt.check("Fudeldu Laberwort").size(), is(2));
}
Also used : SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) JLanguageTool(org.languagetool.JLanguageTool) GermanSpellerRule(org.languagetool.rules.de.GermanSpellerRule) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) Rule(org.languagetool.rules.Rule) GermanyGerman(org.languagetool.language.GermanyGerman) Test(org.junit.Test)

Example 64 with Rule

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

the class AustralianEnglish method getRelevantRules.

@Override
public List<Rule> getRelevantRules(ResourceBundle messages) throws IOException {
    List<Rule> rules = new ArrayList<>();
    rules.addAll(super.getRelevantRules(messages));
    rules.add(new MorfologikAustralianSpellerRule(messages, this));
    return rules;
}
Also used : MorfologikAustralianSpellerRule(org.languagetool.rules.en.MorfologikAustralianSpellerRule) ArrayList(java.util.ArrayList) Rule(org.languagetool.rules.Rule) MorfologikAustralianSpellerRule(org.languagetool.rules.en.MorfologikAustralianSpellerRule)

Example 65 with Rule

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

the class BritishEnglish method getRelevantRules.

@Override
public List<Rule> getRelevantRules(ResourceBundle messages) throws IOException {
    List<Rule> rules = new ArrayList<>();
    rules.addAll(super.getRelevantRules(messages));
    rules.add(new BritishReplaceRule(messages));
    rules.add(new MorfologikBritishSpellerRule(messages, this));
    return rules;
}
Also used : BritishReplaceRule(org.languagetool.rules.en.BritishReplaceRule) ArrayList(java.util.ArrayList) MorfologikBritishSpellerRule(org.languagetool.rules.en.MorfologikBritishSpellerRule) MorfologikBritishSpellerRule(org.languagetool.rules.en.MorfologikBritishSpellerRule) Rule(org.languagetool.rules.Rule) BritishReplaceRule(org.languagetool.rules.en.BritishReplaceRule)

Aggregations

Rule (org.languagetool.rules.Rule)67 JLanguageTool (org.languagetool.JLanguageTool)16 PatternRule (org.languagetool.rules.patterns.PatternRule)15 ArrayList (java.util.ArrayList)14 RuleMatch (org.languagetool.rules.RuleMatch)14 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)12 Test (org.junit.Test)11 Language (org.languagetool.Language)11 IncorrectExample (org.languagetool.rules.IncorrectExample)8 English (org.languagetool.language.English)7 SpellingCheckRule (org.languagetool.rules.spelling.SpellingCheckRule)7 File (java.io.File)6 PatternToken (org.languagetool.rules.patterns.PatternToken)6 AnalyzedSentence (org.languagetool.AnalyzedSentence)5 AmericanEnglish (org.languagetool.language.AmericanEnglish)5 BitextRule (org.languagetool.rules.bitext.BitextRule)5 GermanSpellerRule (org.languagetool.rules.de.GermanSpellerRule)4 DisambiguationPatternRule (org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule)4 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3