Search in sources :

Example 51 with Rule

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

the class LanguageToolSupport method disableRule.

void disableRule(String ruleId) {
    Rule rule = this.getRuleForId(ruleId);
    if (rule == null) {
        //System.err.println("No rule with id: <"+ruleId+">");
        return;
    }
    if (rule.isDefaultOff()) {
        config.getEnabledRuleIds().remove(ruleId);
    } else {
        config.getDisabledRuleIds().add(ruleId);
    }
    languageTool.disableRule(ruleId);
    updateHighlights(ruleId);
    fireEvent(LanguageToolEvent.Type.RULE_DISABLED, null);
}
Also used : Rule(org.languagetool.rules.Rule)

Example 52 with Rule

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

the class Main method showOptions.

private void showOptions() {
    JLanguageTool langTool = ltSupport.getLanguageTool();
    List<Rule> rules = langTool.getAllRules();
    ConfigurationDialog configDialog = getCurrentConfigDialog();
    // this blocks until OK/Cancel is clicked in the dialog
    configDialog.show(rules);
    Configuration config = ltSupport.getConfig();
    try {
        //save config - needed for the server
        config.saveConfiguration(langTool.getLanguage());
    } catch (IOException e) {
        Tools.showError(e);
    }
    ltSupport.reloadConfig();
    // Stop server, start new server if requested:
    stopServer();
    maybeStartServer();
}
Also used : JLanguageTool(org.languagetool.JLanguageTool) Rule(org.languagetool.rules.Rule)

Example 53 with Rule

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

the class ExampleSentenceProvider method initExampleSentences.

private void initExampleSentences(Language language) throws IOException {
    JLanguageTool lt = new JLanguageTool(language);
    List<Rule> rules = lt.getAllActiveRules();
    List<ExampleSentence> sentences = new ArrayList<>();
    for (Rule rule : rules) {
        if (rule instanceof AbstractPatternRule && !rule.isDefaultOff()) {
            List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
            for (IncorrectExample incorrectExample : incorrectExamples) {
                ExampleSentence sentence = new ExampleSentence(incorrectExample.getExample(), rule.getId());
                sentences.add(sentence);
            }
        }
    }
    languageToExamples.put(language, sentences);
}
Also used : JLanguageTool(org.languagetool.JLanguageTool) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) Rule(org.languagetool.rules.Rule) IncorrectExample(org.languagetool.rules.IncorrectExample) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 54 with Rule

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

the class PatternRuleTest method testBadSentences.

private void testBadSentences(JLanguageTool languageTool, JLanguageTool allRulesLanguageTool, Language lang, Map<String, AbstractPatternRule> complexRules, AbstractPatternRule rule) throws IOException {
    List<IncorrectExample> badSentences = rule.getIncorrectExamples();
    if (badSentences.size() == 0) {
        fail("No incorrect examples found for rule " + rule.getFullId());
    }
    // necessary for XML Pattern rules containing <or>
    List<AbstractPatternRule> rules = allRulesLanguageTool.getPatternRulesByIdAndSubId(rule.getId(), rule.getSubId());
    for (IncorrectExample origBadExample : badSentences) {
        // enable indentation use
        String origBadSentence = origBadExample.getExample().replaceAll("[\\n\\t]+", "");
        List<String> expectedCorrections = origBadExample.getCorrections();
        int expectedMatchStart = origBadSentence.indexOf("<marker>");
        int expectedMatchEnd = origBadSentence.indexOf("</marker>") - "<marker>".length();
        if (expectedMatchStart == -1 || expectedMatchEnd == -1) {
            fail(lang + ": No error position markup ('<marker>...</marker>') in bad example in rule " + rule.getFullId());
        }
        String badSentence = cleanXML(origBadSentence);
        assertTrue(badSentence.trim().length() > 0);
        // necessary for XML Pattern rules containing <or>
        List<RuleMatch> matches = new ArrayList<>();
        for (Rule auxRule : rules) {
            matches.addAll(getMatches(auxRule, badSentence, languageTool));
        }
        if (rule instanceof RegexPatternRule || rule instanceof PatternRule && !((PatternRule) rule).isWithComplexPhrase()) {
            if (matches.size() != 1) {
                AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(badSentence);
                StringBuilder sb = new StringBuilder("Analyzed token readings:");
                for (AnalyzedTokenReadings atr : analyzedSentence.getTokens()) {
                    sb.append(" ").append(atr);
                }
                String info = "";
                if (rule instanceof RegexPatternRule) {
                    info = "\nRegexp: " + ((RegexPatternRule) rule).getPattern().toString();
                }
                fail(lang + " rule " + rule.getFullId() + ":\n\"" + badSentence + "\"\n" + "Errors expected: 1\n" + "Errors found   : " + matches.size() + "\n" + "Message: " + rule.getMessage() + "\n" + sb + "\nMatches: " + matches + info);
            }
            assertEquals(lang + ": Incorrect match position markup (start) for rule " + rule.getFullId() + ", sentence: " + badSentence, expectedMatchStart, matches.get(0).getFromPos());
            assertEquals(lang + ": Incorrect match position markup (end) for rule " + rule.getFullId() + ", sentence: " + badSentence, expectedMatchEnd, matches.get(0).getToPos());
            // make sure suggestion is what we expect it to be
            assertSuggestions(badSentence, lang, expectedCorrections, rule, matches);
            // make sure the suggested correction doesn't produce an error:
            if (matches.get(0).getSuggestedReplacements().size() > 0) {
                int fromPos = matches.get(0).getFromPos();
                int toPos = matches.get(0).getToPos();
                for (String replacement : matches.get(0).getSuggestedReplacements()) {
                    String fixedSentence = badSentence.substring(0, fromPos) + replacement + badSentence.substring(toPos);
                    matches = getMatches(rule, fixedSentence, languageTool);
                    if (matches.size() > 0) {
                        fail("Incorrect input:\n" + "  " + badSentence + "\nCorrected sentence:\n" + "  " + fixedSentence + "\nBy Rule:\n" + "  " + rule.getFullId() + "\nThe correction triggered an error itself:\n" + "  " + matches.get(0) + "\n");
                    }
                }
            }
        } else {
            // for multiple rules created with complex phrases
            matches = getMatches(rule, badSentence, languageTool);
            if (matches.size() == 0 && !complexRules.containsKey(rule.getId() + badSentence)) {
                complexRules.put(rule.getId() + badSentence, rule);
            }
            if (matches.size() != 0) {
                complexRules.put(rule.getId() + badSentence, null);
                assertTrue(lang + ": Did expect one error in: \"" + badSentence + "\" (Rule: " + rule.getFullId() + "), got " + matches.size(), matches.size() == 1);
                assertEquals(lang + ": Incorrect match position markup (start) for rule " + rule.getFullId(), expectedMatchStart, matches.get(0).getFromPos());
                assertEquals(lang + ": Incorrect match position markup (end) for rule " + rule.getFullId(), expectedMatchEnd, matches.get(0).getToPos());
                assertSuggestions(badSentence, lang, expectedCorrections, rule, matches);
                assertSuggestionsDoNotCreateErrors(badSentence, languageTool, rule, matches);
            }
        }
    // check for overlapping rules
    /*matches = getMatches(rule, badSentence, languageTool);
      List<RuleMatch> matchesAllRules = allRulesLanguageTool.check(badSentence);
      for (RuleMatch match : matchesAllRules) {
        if (!match.getRule().getId().equals(rule.getId()) && !matches.isEmpty()
            && rangeIsOverlapping(matches.get(0).getFromPos(), matches.get(0).getToPos(), match.getFromPos(), match.getToPos()))
          System.err.println("WARN: " + lang.getShortCode() + ": '" + badSentence + "' in "
                  + rule.getId() + " also matched " + match.getRule().getId());
      }*/
    }
}
Also used : DisambiguationPatternRule(org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule) ArrayList(java.util.ArrayList) AnalyzedTokenReadings(org.languagetool.AnalyzedTokenReadings) RuleMatch(org.languagetool.rules.RuleMatch) AnalyzedSentence(org.languagetool.AnalyzedSentence) IncorrectExample(org.languagetool.rules.IncorrectExample) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) DisambiguationPatternRule(org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule) Rule(org.languagetool.rules.Rule)

Example 55 with Rule

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

the class ExampleSentenceCorrectionCreator method checkCorrections.

private void checkCorrections(Rule rule, IncorrectExample incorrectExample, List<String> xmlLines, JLanguageTool tool) throws IOException {
    List<String> corrections = incorrectExample.getCorrections();
    if (corrections.isEmpty()) {
        for (Rule r : tool.getAllActiveRules()) {
            tool.disableRule(r.getId());
        }
        tool.enableRule(rule.getId());
        String incorrectSentence = incorrectExample.getExample().replaceAll("</?marker>", "");
        List<RuleMatch> matches = tool.check(incorrectSentence);
        System.err.println("no corrections: " + rule.getId() + ", " + matches.size() + " matches");
        if (matches.size() == 0) {
            throw new RuntimeException("Got no rule match: " + incorrectSentence);
        }
        List<String> suggestedReplacements = matches.get(0).getSuggestedReplacements();
        String newAttribute = "correction=\"" + String.join("|", suggestedReplacements) + "\"";
        addAttribute(rule, newAttribute, xmlLines);
    }
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) Rule(org.languagetool.rules.Rule) PatternRule(org.languagetool.rules.patterns.PatternRule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

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