use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class DifferentLengthRuleTest method testRule.
@Test
public void testRule() throws IOException {
DifferentLengthRule rule = new DifferentLengthRule();
RuleMatch[] matches;
JLanguageTool trgLangTool = new JLanguageTool(TestTools.getDemoLanguage());
JLanguageTool srcLangTool = new JLanguageTool(new FakeLanguage());
rule.setSourceLanguage(TestTools.getDemoLanguage());
// correct sentences:
matches = rule.match(srcLangTool.getAnalyzedSentence("This is a test sentence."), trgLangTool.getAnalyzedSentence("To zdanie testowe."));
assertEquals(0, matches.length);
matches = rule.match(srcLangTool.getAnalyzedSentence("Click this button."), trgLangTool.getAnalyzedSentence("Kliknij ten przycisk."));
assertEquals(0, matches.length);
// incorrect sentences:
matches = rule.match(srcLangTool.getAnalyzedSentence("Open a file, and check if it is corrupt."), trgLangTool.getAnalyzedSentence("Otwórz plik."));
assertEquals(1, matches.length);
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class PatternRuleMatcherTest method testZeroMinOccurrencesWithSuggestion.
@Test
public void testZeroMinOccurrencesWithSuggestion() throws Exception {
PatternToken patternTokenB = makeElement("b");
patternTokenB.setMinOccurrence(0);
// regex: a b? c
List<PatternToken> patternTokens = Arrays.asList(makeElement("a"), patternTokenB, makeElement("c"));
PatternRule rule = new PatternRule("", new Demo(), patternTokens, "my description", "<suggestion>\\1 \\2 \\3</suggestion>", "short message");
PatternRuleMatcher matcher = new PatternRuleMatcher(rule, false);
// we need to add this line to trigger proper replacement but I am not sure why :(
rule.addSuggestionMatch(new Match(null, null, false, null, null, CaseConversion.NONE, false, false, IncludeRange.NONE));
RuleMatch[] matches = getMatches("a b c", matcher);
assertEquals(Arrays.asList("a b c"), matches[0].getSuggestedReplacements());
RuleMatch[] matches2 = getMatches("a c", matcher);
assertEquals(Arrays.asList("a c"), matches2[0].getSuggestedReplacements());
}
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 + "'");
}
}
}
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);
}
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));
}
Aggregations