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());
}
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());
}
*/
}
}
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));
}
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;
}
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;
}
Aggregations