Search in sources :

Example 41 with Rule

use of org.languagetool.rules.Rule in project languagetool by languagetool-org.

the class Searcher method getRuleById.

List<PatternRule> getRuleById(String ruleId, Language language) throws IOException {
    List<PatternRule> rules = new ArrayList<>();
    JLanguageTool langTool = new JLanguageTool(language);
    for (Rule rule : langTool.getAllRules()) {
        if (rule.getId().equals(ruleId) && rule instanceof PatternRule) {
            rules.add((PatternRule) rule);
        }
    }
    if (rules.size() > 0) {
        return rules;
    } else {
        throw new PatternRuleNotFoundException(ruleId, language);
    }
}
Also used : PatternRule(org.languagetool.rules.patterns.PatternRule) JLanguageTool(org.languagetool.JLanguageTool) ArrayList(java.util.ArrayList) PatternRule(org.languagetool.rules.patterns.PatternRule) Rule(org.languagetool.rules.Rule)

Example 42 with Rule

use of org.languagetool.rules.Rule 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 43 with Rule

use of org.languagetool.rules.Rule 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 44 with Rule

use of org.languagetool.rules.Rule 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 45 with Rule

use of org.languagetool.rules.Rule 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

Rule (org.languagetool.rules.Rule)67 JLanguageTool (org.languagetool.JLanguageTool)16 PatternRule (org.languagetool.rules.patterns.PatternRule)15 ArrayList (java.util.ArrayList)14 RuleMatch (org.languagetool.rules.RuleMatch)14 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)12 Test (org.junit.Test)11 Language (org.languagetool.Language)11 IncorrectExample (org.languagetool.rules.IncorrectExample)8 English (org.languagetool.language.English)7 SpellingCheckRule (org.languagetool.rules.spelling.SpellingCheckRule)7 File (java.io.File)6 PatternToken (org.languagetool.rules.patterns.PatternToken)6 AnalyzedSentence (org.languagetool.AnalyzedSentence)5 AmericanEnglish (org.languagetool.language.AmericanEnglish)5 BitextRule (org.languagetool.rules.bitext.BitextRule)5 GermanSpellerRule (org.languagetool.rules.de.GermanSpellerRule)4 DisambiguationPatternRule (org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule)4 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3