use of org.languagetool.rules.RuleMatch 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.RuleMatch 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;
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class IndexerSearcherTest method testWithNewRule.
public void testWithNewRule() throws Exception {
createIndex("How to move back and fourth from linux to xmb?");
List<PatternToken> patternTokens = Arrays.asList(new PatternToken("move", false, false, false), new PatternToken("back", false, false, false));
PatternRule rule1 = new PatternRule("RULE1", new English(), patternTokens, "desc", "msg", "shortMsg");
Searcher errorSearcher = new Searcher(directory);
SearcherResult searcherResult = errorSearcher.findRuleMatchesOnIndex(rule1, new English());
assertEquals(1, searcherResult.getCheckedSentences());
assertEquals(1, searcherResult.getMatchingSentences().size());
List<RuleMatch> ruleMatches = searcherResult.getMatchingSentences().get(0).getRuleMatches();
assertEquals(1, ruleMatches.size());
Rule rule = ruleMatches.get(0).getRule();
assertEquals("RULE1", rule.getId());
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class IndexerSearcherTest method testWithException.
public void testWithException() throws Exception {
createIndex("How to move back and fourth from linux to xmb?");
PatternToken exceptionElem = new PatternToken("forth|back", false, true, false);
exceptionElem.setStringPosException("exception", false, false, false, false, false, "POS", false, false, null);
List<PatternToken> patternTokens = Arrays.asList(new PatternToken("move", false, false, false), exceptionElem);
PatternRule rule1 = new PatternRule("RULE1", new English(), patternTokens, "desc", "msg", "shortMsg");
Searcher errorSearcher = new Searcher(directory);
SearcherResult searcherResult = errorSearcher.findRuleMatchesOnIndex(rule1, new English());
assertEquals(1, searcherResult.getCheckedSentences());
assertEquals(1, searcherResult.getMatchingSentences().size());
List<RuleMatch> ruleMatches = searcherResult.getMatchingSentences().get(0).getRuleMatches();
assertEquals(1, ruleMatches.size());
Rule rule = ruleMatches.get(0).getRule();
assertEquals("RULE1", rule.getId());
}
use of org.languagetool.rules.RuleMatch in project languagetool by languagetool-org.
the class MultiThreadedJLanguageTool method performCheck.
@Override
protected List<RuleMatch> performCheck(List<AnalyzedSentence> analyzedSentences, List<String> sentences, List<Rule> allRules, ParagraphHandling paraMode, AnnotatedText annotatedText, RuleMatchListener listener) throws IOException {
int charCount = 0;
int lineCount = 0;
int columnCount = 1;
List<RuleMatch> ruleMatches = new ArrayList<>();
ExecutorService executorService = getExecutorService();
try {
List<Callable<List<RuleMatch>>> callables = createTextCheckCallables(paraMode, annotatedText, analyzedSentences, sentences, allRules, charCount, lineCount, columnCount, listener);
List<Future<List<RuleMatch>>> futures = executorService.invokeAll(callables);
for (Future<List<RuleMatch>> future : futures) {
ruleMatches.addAll(future.get());
}
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
return ruleMatches;
}
Aggregations