Search in sources :

Example 21 with Rule

use of org.languagetool.rules.Rule 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;
}
Also used : JLanguageTool(org.languagetool.JLanguageTool) PatternRule(org.languagetool.rules.patterns.PatternRule) Rule(org.languagetool.rules.Rule)

Example 22 with Rule

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

the class AtomFeedChecker method disableSpellingRules.

private void disableSpellingRules(JLanguageTool langTool) {
    for (Rule rule : langTool.getAllActiveRules()) {
        if (rule.isDictionaryBasedSpellingRule()) {
            langTool.disableRule(rule.getId());
            System.out.println("Disabled spelling rule: " + rule.getId());
        }
    }
}
Also used : Rule(org.languagetool.rules.Rule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 23 with Rule

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

the class AtomFeedChecker method activateCategory.

private void activateCategory(String categoryName, JLanguageTool langTool) {
    for (Rule rule : langTool.getAllRules()) {
        if (rule.getCategory().getName().equals(categoryName)) {
            System.out.println("Activating " + rule.getId() + " in category " + categoryName);
            langTool.enableRule(rule.getId());
        }
    }
}
Also used : Rule(org.languagetool.rules.Rule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 24 with Rule

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

the class WikipediaQuickCheck method main.

/*public static void mainTest(String[] args) throws IOException {
      TextFilter filter = new SwebleWikipediaTextFilter();
      String plainText = filter.filter("hallo\n* eins\n* zwei");
      System.out.println(plainText);
  }*/
public static void main(String[] args) throws IOException, PageNotFoundException {
    if (args.length != 1) {
        System.out.println("Usage: " + WikipediaQuickCheck.class.getName() + " <url>");
        System.exit(1);
    }
    WikipediaQuickCheck check = new WikipediaQuickCheck();
    // URL examples:
    //String urlString = "http://de.wikipedia.org/wiki/Angela_Merkel";
    //String urlString = "https://de.wikipedia.org/wiki/Benutzer_Diskussion:Dnaber";
    //String urlString = "https://secure.wikimedia.org/wikipedia/de/wiki/Gütersloh";
    String urlString = args[0];
    MarkupAwareWikipediaResult result = check.checkPage(new URL(urlString), new ErrorMarker("***", "***"));
    int errorCount = 0;
    for (AppliedRuleMatch match : result.getAppliedRuleMatches()) {
        RuleMatchApplication matchApplication = match.getRuleMatchApplications().get(0);
        RuleMatch ruleMatch = match.getRuleMatch();
        Rule rule = ruleMatch.getRule();
        System.out.println("");
        String message = ruleMatch.getMessage().replace("<suggestion>", "'").replace("</suggestion>", "'");
        errorCount++;
        System.out.print(errorCount + ". " + message);
        if (rule instanceof AbstractPatternRule) {
            System.out.println(" (" + ((AbstractPatternRule) rule).getFullId() + ")");
        } else {
            System.out.println(" (" + rule.getId() + ")");
        }
        System.out.println("    ..." + matchApplication.getOriginalErrorContext(50).replace("\n", "\\n") + "...");
    }
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) Rule(org.languagetool.rules.Rule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) URL(java.net.URL) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 25 with Rule

use of org.languagetool.rules.Rule 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");
}
Also used : PatternRule(org.languagetool.rules.patterns.PatternRule) JLanguageTool(org.languagetool.JLanguageTool) IOException(java.io.IOException) English(org.languagetool.language.English) RuleMatch(org.languagetool.rules.RuleMatch) Language(org.languagetool.Language) PatternRule(org.languagetool.rules.patterns.PatternRule) Rule(org.languagetool.rules.Rule) Ignore(org.junit.Ignore)

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