use of org.languagetool.rules.patterns.PatternRule in project languagetool by languagetool-org.
the class IndexerSearcherTest method createIndex.
private int createIndex(JLanguageTool lt) throws IOException {
int ruleCount = 0;
try (Indexer indexer = new Indexer(directory, lt.getLanguage())) {
List<Rule> rules = lt.getAllActiveRules();
for (Rule rule : rules) {
if (rule instanceof PatternRule && !rule.isDefaultOff()) {
PatternRule patternRule = (PatternRule) rule;
List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
Document doc = new Document();
FieldType idType = new FieldType();
idType.setStored(true);
idType.setTokenized(false);
doc.add(new Field("ruleId", patternRule.getFullId(), idType));
for (IncorrectExample incorrectExample : incorrectExamples) {
String example = incorrectExample.getExample().replaceAll("</?marker>", "");
FieldType fieldType = new FieldType();
fieldType.setStored(true);
fieldType.setTokenized(true);
fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
doc.add(new Field(FIELD_NAME, example, fieldType));
// no lowercase here, it would lowercase the input to the LT analysis, leading to wrong POS tags:
doc.add(new Field(FIELD_NAME_LOWERCASE, example, fieldType));
}
indexer.add(doc);
ruleCount++;
}
}
}
errorSearcher = new Searcher(directory);
return ruleCount;
}
use of org.languagetool.rules.patterns.PatternRule in project languagetool by languagetool-org.
the class IndexerSearcherTest method testNegatedMatchAtSentenceStart.
public void testNegatedMatchAtSentenceStart() throws Exception {
createIndex("How to move?");
PatternToken negatedPatternToken = new PatternToken("Negated", false, false, false);
negatedPatternToken.setNegation(true);
List<PatternToken> patternTokens = Arrays.asList(negatedPatternToken, new PatternToken("How", false, false, false));
Searcher errorSearcher = new Searcher(directory);
PatternRule rule1 = new PatternRule("RULE1", new English(), patternTokens, "desc", "msg", "shortMsg");
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.patterns.PatternRule in project languagetool by languagetool-org.
the class IndexerSearcherTest method testForDebugging.
@Ignore("manual debugging only")
public void testForDebugging() throws Exception {
// Note that the second sentence ends with "lid" instead of "lids" (the inflated one)
//createIndex("I thin so");
useRealIndex();
German language = new German();
PatternRule rule = getFirstRule("I_THIN", language);
SearcherResult searcherResult = errorSearcher.findRuleMatchesOnIndex(rule, language);
System.out.println("Matches: " + searcherResult.getMatchingSentences());
}
use of org.languagetool.rules.patterns.PatternRule in project languagetool by languagetool-org.
the class IndexerSearcherTest method testWithRegexRule.
public void testWithRegexRule() 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("forth|back", false, true, 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.patterns.PatternRule in project languagetool by languagetool-org.
the class IndexerSearcherTest method testApostropheElement.
public void testApostropheElement() throws Exception {
createIndex("Daily Bleed's Anarchist Encyclopedia");
List<PatternToken> elements1 = Arrays.asList(new PatternToken("Bleed", false, false, false), new PatternToken("'", false, false, false), new PatternToken("s", false, false, false));
PatternRule rule1 = new PatternRule("RULE1", new English(), elements1, "desc", "msg", "shortMsg");
List<PatternToken> elements2 = Arrays.asList(new PatternToken("Bleed", false, false, false), new PatternToken("'", false, false, false), new PatternToken("x", false, false, false));
PatternRule rule2 = new PatternRule("RULE", new English(), elements2, "desc", "msg", "shortMsg");
SearcherResult searcherResult1 = errorSearcher.findRuleMatchesOnIndex(rule1, new English());
assertEquals(1, searcherResult1.getMatchingSentences().size());
List<RuleMatch> ruleMatches = searcherResult1.getMatchingSentences().get(0).getRuleMatches();
assertEquals(1, ruleMatches.size());
Rule rule = ruleMatches.get(0).getRule();
assertEquals("RULE1", rule.getId());
SearcherResult searcherResult2 = errorSearcher.findRuleMatchesOnIndex(rule2, new English());
assertEquals(0, searcherResult2.getMatchingSentences().size());
}
Aggregations