Search in sources :

Example 21 with PatternRule

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;
}
Also used : Field(org.apache.lucene.document.Field) PatternRule(org.languagetool.rules.patterns.PatternRule) PatternRule(org.languagetool.rules.patterns.PatternRule) Rule(org.languagetool.rules.Rule) IncorrectExample(org.languagetool.rules.IncorrectExample) Document(org.apache.lucene.document.Document) FieldType(org.apache.lucene.document.FieldType)

Example 22 with PatternRule

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());
}
Also used : English(org.languagetool.language.English) PatternToken(org.languagetool.rules.patterns.PatternToken) RuleMatch(org.languagetool.rules.RuleMatch) PatternRule(org.languagetool.rules.patterns.PatternRule) PatternRule(org.languagetool.rules.patterns.PatternRule) Rule(org.languagetool.rules.Rule)

Example 23 with PatternRule

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());
}
Also used : PatternRule(org.languagetool.rules.patterns.PatternRule) German(org.languagetool.language.German) Ignore(org.junit.Ignore)

Example 24 with PatternRule

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());
}
Also used : English(org.languagetool.language.English) PatternToken(org.languagetool.rules.patterns.PatternToken) RuleMatch(org.languagetool.rules.RuleMatch) PatternRule(org.languagetool.rules.patterns.PatternRule) PatternRule(org.languagetool.rules.patterns.PatternRule) Rule(org.languagetool.rules.Rule)

Example 25 with PatternRule

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());
}
Also used : English(org.languagetool.language.English) PatternToken(org.languagetool.rules.patterns.PatternToken) RuleMatch(org.languagetool.rules.RuleMatch) PatternRule(org.languagetool.rules.patterns.PatternRule) PatternRule(org.languagetool.rules.patterns.PatternRule) Rule(org.languagetool.rules.Rule)

Aggregations

PatternRule (org.languagetool.rules.patterns.PatternRule)28 PatternToken (org.languagetool.rules.patterns.PatternToken)17 Rule (org.languagetool.rules.Rule)13 ArrayList (java.util.ArrayList)12 RuleMatch (org.languagetool.rules.RuleMatch)9 Test (org.junit.Test)8 English (org.languagetool.language.English)8 JLanguageTool (org.languagetool.JLanguageTool)6 File (java.io.File)4 FileReader (java.io.FileReader)3 IncorrectExample (org.languagetool.rules.IncorrectExample)3 Ignore (org.junit.Ignore)2 Language (org.languagetool.Language)2 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1