use of org.languagetool.rules.patterns.PatternRule in project languagetool by languagetool-org.
the class RuleMatchAsXmlSerializerTest method testRuleMatchesToXMLWithCategory.
@Test
public void testRuleMatchesToXMLWithCategory() throws IOException {
List<RuleMatch> matches = new ArrayList<>();
String text = "This is a test sentence.";
List<PatternToken> patternTokens = Collections.emptyList();
Rule patternRule = new PatternRule("MY_ID", LANG, patternTokens, "my description", "my message", "short message");
patternRule.setCategory(new Category(new CategoryId("TEST_ID"), "MyCategory"));
RuleMatch match = new RuleMatch(patternRule, 8, 10, "myMessage");
match.setColumn(99);
match.setEndColumn(100);
match.setLine(44);
match.setEndLine(45);
matches.add(match);
String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, LANG, LANG);
assertTrue(xml.contains(">\n" + "<error fromy=\"44\" fromx=\"98\" toy=\"45\" tox=\"99\" ruleId=\"MY_ID\" msg=\"myMessage\" " + "replacements=\"\" context=\"...s is a test ...\" contextoffset=\"8\" offset=\"8\" errorlength=\"2\" category=\"MyCategory\" " + "categoryid=\"TEST_ID\" locqualityissuetype=\"uncategorized\"/>\n" + "</matches>\n"));
patternRule.setCategory(new Category(new CategoryId("CAT_ID"), "MyCategory"));
RuleMatch match2 = new RuleMatch(patternRule, 8, 10, "myMessage");
String xml2 = SERIALIZER.ruleMatchesToXml(Collections.singletonList(match2), text, 5, LANG, LANG);
assertTrue(xml2.contains("category=\"MyCategory\""));
assertTrue(xml2.contains("categoryid=\"CAT_ID\""));
}
use of org.languagetool.rules.patterns.PatternRule in project languagetool by languagetool-org.
the class JLanguageToolTest method testOverlapFilter.
@Test
public void testOverlapFilter() throws IOException {
Category category = new Category(new CategoryId("TEST_ID"), "test category");
List<PatternToken> elements1 = Arrays.asList(new PatternToken("one", true, false, false));
PatternRule rule1 = new PatternRule("id1", new English(), elements1, "desc1", "msg1", "shortMsg1");
rule1.setSubId("1");
rule1.setCategory(category);
List<PatternToken> elements2 = Arrays.asList(new PatternToken("one", true, false, false), new PatternToken("two", true, false, false));
PatternRule rule2 = new PatternRule("id1", new English(), elements2, "desc2", "msg2", "shortMsg2");
rule2.setSubId("2");
rule2.setCategory(category);
JLanguageTool tool = new JLanguageTool(new English());
tool.addRule(rule1);
tool.addRule(rule2);
List<RuleMatch> ruleMatches1 = tool.check("And one two three.");
assertEquals("one overlapping rule must be filtered out", 1, ruleMatches1.size());
assertEquals("msg1", ruleMatches1.get(0).getMessage());
String sentence = "And one two three.";
AnalyzedSentence analyzedSentence = tool.getAnalyzedSentence(sentence);
List<Rule> bothRules = new ArrayList<>(Arrays.asList(rule1, rule2));
List<RuleMatch> ruleMatches2 = tool.checkAnalyzedSentence(ParagraphHandling.NORMAL, bothRules, analyzedSentence);
assertEquals("one overlapping rule must be filtered out", 1, ruleMatches2.size());
assertEquals("msg1", ruleMatches2.get(0).getMessage());
}
use of org.languagetool.rules.patterns.PatternRule in project languagetool by languagetool-org.
the class Searcher method getLanguageToolWithOneRule.
private JLanguageTool getLanguageToolWithOneRule(Language lang, PatternRule patternRule) {
JLanguageTool langTool = new JLanguageTool(lang);
for (Rule rule : langTool.getAllActiveRules()) {
if (!rule.getId().equals(patternRule.getId())) {
langTool.disableRule(rule.getId());
}
}
langTool.addRule(patternRule);
// rule might be off by default
langTool.enableRule(patternRule.getId());
return langTool;
}
use of org.languagetool.rules.patterns.PatternRule in project languagetool by languagetool-org.
the class IndexerSearcherTest method testAllRules.
@Ignore("ignored as long as it doesn't work 100%")
public void testAllRules() throws Exception {
long startTime = System.currentTimeMillis();
// comment in to test with external index:
//directory = new SimpleFSDirectory(new File("/media/external-disk/corpus/languagetool/fast-rule-evaluation-de/"));
//errorSearcher = new Searcher(directory);
// TODO: make this work for all languages
Language language = new English();
//Language language = new French();
//Language language = new Spanish();
//Language language = new Polish();
//Language language = new German();
JLanguageTool lt = new JLanguageTool(language);
System.out.println("Creating index for " + language + "...");
int ruleCount = createIndex(lt);
System.out.println("Index created with " + ruleCount + " rules");
int ruleCounter = 0;
int ruleProblems = 0;
int exceptionCount = 0;
List<Rule> rules = lt.getAllActiveRules();
for (Rule rule : rules) {
if (rule instanceof PatternRule && !rule.isDefaultOff()) {
PatternRule patternRule = (PatternRule) rule;
try {
ruleCounter++;
SearcherResult searcherResult = errorSearcher.findRuleMatchesOnIndex(patternRule, language);
List<MatchingSentence> matchingSentences = searcherResult.getMatchingSentences();
boolean foundExpectedMatch = false;
for (MatchingSentence matchingSentence : matchingSentences) {
List<RuleMatch> ruleMatches = matchingSentence.getRuleMatches();
List<String> ruleMatchIds = getRuleMatchIds(ruleMatches);
if (ruleMatchIds.contains(patternRule.getFullId())) {
// TODO: there can be more than one expected match, can't it?
foundExpectedMatch = true;
break;
}
}
if (!foundExpectedMatch) {
System.out.println("Error: No match found for " + patternRule);
System.out.println("Query : " + searcherResult.getRelaxedQuery().toString(FIELD_NAME_LOWERCASE));
System.out.println("Default field: " + FIELD_NAME_LOWERCASE);
System.out.println("Lucene Hits: " + searcherResult.getLuceneMatchCount());
System.out.println("Matches : " + matchingSentences);
System.out.println("Examples : " + rule.getIncorrectExamples());
System.out.println();
ruleProblems++;
} else {
//long time = System.currentTimeMillis() - startTime;
//System.out.println("Tested " + matchingSentences.size() + " sentences in " + time + "ms for rule " + patternRule);
}
} catch (UnsupportedPatternRuleException e) {
System.out.println("UnsupportedPatternRuleException searching for rule " + patternRule.getFullId() + ": " + e.getMessage());
ruleProblems++;
} catch (Exception e) {
System.out.println("Exception searching for rule " + patternRule.getFullId() + ": " + e.getMessage());
e.printStackTrace(System.out);
exceptionCount++;
}
}
}
System.out.println(language + ": problems: " + ruleProblems + ", total rules: " + ruleCounter);
System.out.println(language + ": exceptions: " + exceptionCount + " (including timeouts)");
System.out.println("Total time: " + (System.currentTimeMillis() - startTime) + "ms");
}
use of org.languagetool.rules.patterns.PatternRule in project languagetool by languagetool-org.
the class IndexerSearcherTest method getRuleMatchIds.
private List<String> getRuleMatchIds(List<RuleMatch> ruleMatches) {
List<String> ids = new ArrayList<>();
for (RuleMatch ruleMatch : ruleMatches) {
if (ruleMatch.getRule() instanceof PatternRule) {
PatternRule patternRule = (PatternRule) ruleMatch.getRule();
ids.add(patternRule.getFullId());
}
}
return ids;
}
Aggregations