Search in sources :

Example 1 with Category

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

the class PatternRuleTest method validateRuleIds.

private void validateRuleIds(Language lang, JLanguageTool languageTool) {
    List<Rule> allRules = languageTool.getAllRules();
    Set<String> ids = new HashSet<>();
    Set<Class> ruleClasses = new HashSet<>();
    Set<String> categoryIds = new HashSet<>();
    for (Rule rule : allRules) {
        assertIdUniqueness(ids, ruleClasses, lang, rule);
        if (rule.getId().equalsIgnoreCase("ID")) {
            System.err.println("WARNING: " + lang.getShortCodeWithCountryAndVariant() + " has a rule with id 'ID', this should probably be changed");
        }
        Category category = rule.getCategory();
        if (category != null && category.getId() != null) {
            String catId = category.getId().toString();
            if (!catId.matches("[A-Z0-9_-]+") && !categoryIds.contains(catId)) {
                System.err.println("WARNING: category id '" + catId + "' doesn't match expected regexp [A-Z0-9_-]+");
                categoryIds.add(catId);
            }
        }
    }
}
Also used : Category(org.languagetool.rules.Category) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) DisambiguationPatternRule(org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule) Rule(org.languagetool.rules.Rule) HashSet(java.util.HashSet)

Example 2 with Category

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

the class LanguageToolSupport method reloadConfig.

void reloadConfig() {
    //FIXME
    //if mother tongue changes then create new JLanguageTool instance
    boolean update = false;
    Set<String> disabledRules = config.getDisabledRuleIds();
    if (disabledRules == null) {
        disabledRules = Collections.emptySet();
    }
    Set<String> common = new HashSet<>(disabledRules);
    common.retainAll(languageTool.getDisabledRules());
    Set<String> toDisable = new HashSet<>(disabledRules);
    toDisable.removeAll(common);
    Set<String> toEnable = new HashSet<>(languageTool.getDisabledRules());
    toEnable.removeAll(common);
    for (String ruleId : toDisable) {
        languageTool.disableRule(ruleId);
        update = true;
    }
    for (String ruleId : toEnable) {
        languageTool.enableRule(ruleId);
        update = true;
    }
    Set<String> disabledCategoryNames = config.getDisabledCategoryNames();
    if (disabledCategoryNames == null) {
        disabledCategoryNames = Collections.emptySet();
    }
    Set<CategoryId> disabledCategories = new HashSet<>();
    Map<CategoryId, Category> langCategories = languageTool.getCategories();
    for (CategoryId id : langCategories.keySet()) {
        String categoryName = langCategories.get(id).getName();
        if (disabledCategoryNames.contains(categoryName)) {
            disabledCategories.add(id);
        }
    }
    Set<CategoryId> ltDisabledCategories = new HashSet<>();
    for (CategoryId id : langCategories.keySet()) {
        if (languageTool.isCategoryDisabled(id)) {
            ltDisabledCategories.add(id);
        }
    }
    Set<CategoryId> commonCat = new HashSet<>(disabledCategories);
    commonCat.retainAll(ltDisabledCategories);
    Set<CategoryId> toDisableCat = new HashSet<>(disabledCategories);
    toDisableCat.removeAll(commonCat);
    Set<CategoryId> toEnableCat = new HashSet<>(ltDisabledCategories);
    toEnableCat.removeAll(commonCat);
    for (CategoryId id : toDisableCat) {
        languageTool.disableCategory(id);
    }
    for (CategoryId id : toEnableCat) {
        languageTool.enableRuleCategory(id);
    }
    if (!toDisableCat.isEmpty() || !toEnableCat.isEmpty()) {
        // ugly hack to trigger reInitSpellCheckIgnoreWords()
        update = true;
    }
    Set<String> enabledRules = config.getEnabledRuleIds();
    if (enabledRules == null) {
        enabledRules = Collections.emptySet();
    }
    for (String ruleName : enabledRules) {
        languageTool.enableRule(ruleName);
        update = true;
    }
    if (update) {
        //FIXME
        //we could skip a full check if the user disabled but didn't enable rules
        checkImmediately(null);
        fireEvent(LanguageToolEvent.Type.RULE_ENABLED, null);
    }
}
Also used : CategoryId(org.languagetool.rules.CategoryId) Category(org.languagetool.rules.Category)

Example 3 with Category

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

the class RuleMatchAsXmlSerializer method ruleMatchesToXmlSnippet.

/**
   * Get the XML snippet (i.e. not a complete XML document) for the given rules.
   * @see #getXmlStart
   * @see #getXmlEnd()
   */
public String ruleMatchesToXmlSnippet(List<RuleMatch> ruleMatches, String text, int contextSize) {
    StringBuilder xml = new StringBuilder(CAPACITY);
    //
    // IMPORTANT: people rely on this format, don't change it!
    //
    ContextTools contextTools = new ContextTools();
    contextTools.setEscapeHtml(false);
    contextTools.setContextSize(contextSize);
    String startMarker = "__languagetool_start_marker";
    contextTools.setErrorMarkerStart(startMarker);
    contextTools.setErrorMarkerEnd("");
    for (RuleMatch match : ruleMatches) {
        String subId = "";
        if (match.getRule() instanceof AbstractPatternRule) {
            AbstractPatternRule pRule = (AbstractPatternRule) match.getRule();
            if (pRule.getSubId() != null) {
                subId = " subId=\"" + escapeXMLForAPIOutput(pRule.getSubId()) + "\" ";
            }
        }
        xml.append("<error fromy=\"").append(match.getLine()).append('"').append(" fromx=\"").append(match.getColumn() - 1).append('"').append(" toy=\"").append(match.getEndLine()).append('"').append(" tox=\"").append(match.getEndColumn() - 1).append('"').append(" ruleId=\"").append(match.getRule().getId()).append('"');
        xml.append(subId);
        String msg = match.getMessage().replaceAll("</?suggestion>", "'");
        xml.append(" msg=\"").append(escapeXMLForAPIOutput(msg)).append('"');
        if (!match.getShortMessage().isEmpty()) {
            xml.append(" shortmsg=\"").append(escapeXMLForAPIOutput(match.getShortMessage())).append('"');
        }
        xml.append(" replacements=\"").append(escapeXMLForAPIOutput(String.join("#", match.getSuggestedReplacements()))).append('"');
        String context = contextTools.getContext(match.getFromPos(), match.getToPos(), text);
        // get position of error in context and remove artificial marker again:
        int contextOffset = context.indexOf(startMarker);
        context = context.replaceFirst(startMarker, "");
        context = context.replaceAll("[\n\r]", " ");
        xml.append(" context=\"").append(escapeForXmlAttribute(context)).append('"').append(" contextoffset=\"").append(contextOffset).append('"').append(" offset=\"").append(match.getFromPos()).append('"').append(" errorlength=\"").append(match.getToPos() - match.getFromPos()).append('"');
        if (match.getRule().getUrl() != null) {
            xml.append(" url=\"").append(escapeXMLForAPIOutput(match.getRule().getUrl().toString())).append('"');
        }
        Category category = match.getRule().getCategory();
        if (category != null) {
            xml.append(" category=\"").append(escapeXMLForAPIOutput(category.getName())).append('"');
            CategoryId id = category.getId();
            if (id != null) {
                xml.append(" categoryid=\"").append(escapeXMLForAPIOutput(id.toString())).append('"');
            }
        }
        ITSIssueType type = match.getRule().getLocQualityIssueType();
        if (type != null) {
            xml.append(" locqualityissuetype=\"").append(escapeXMLForAPIOutput(type.toString())).append('"');
        }
        xml.append("/>\n");
    }
    return xml.toString();
}
Also used : CategoryId(org.languagetool.rules.CategoryId) RuleMatch(org.languagetool.rules.RuleMatch) Category(org.languagetool.rules.Category) ITSIssueType(org.languagetool.rules.ITSIssueType) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 4 with Category

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

the class MatchDatabaseTest method test.

@Test
public void test() throws SQLException, ClassNotFoundException {
    Language language = Languages.getLanguageForShortCode("de");
    MatchDatabase database = new MatchDatabase("jdbc:derby:atomFeedChecksDB;create=true", "user", "pass");
    database.dropTables();
    database.createTables();
    assertThat(database.getLatestDate(language), is(new Date(0)));
    assertThat(database.list().size(), is(0));
    assertThat(database.getCheckDates().size(), is(0));
    FakeRule rule1 = new FakeRule(1);
    rule1.setCategory(new Category(new CategoryId("TEST_ID"), "My Category"));
    RuleMatch ruleMatch = new RuleMatch(rule1, 5, 10, "my message");
    AtomFeedItem feedItem1 = new AtomFeedItem("//id1?diff=123", "title", "summary1", new Date(10000));
    WikipediaRuleMatch wikiRuleMatch1 = new WikipediaRuleMatch(language, ruleMatch, "my context", feedItem1);
    database.add(wikiRuleMatch1);
    assertThat(database.list().size(), is(1));
    assertThat(database.list().get(0).getRuleId(), is("ID_1"));
    assertThat(database.list().get(0).getRuleDescription(), is("A fake rule"));
    assertThat(database.list().get(0).getRuleMessage(), is("my message"));
    assertThat(database.list().get(0).getTitle(), is("title"));
    assertThat(database.list().get(0).getErrorContext(), is("my context"));
    assertThat(database.list().get(0).getDiffId(), is(123L));
    assertThat(database.list().get(0).getFixDiffId(), is(0L));
    assertThat(database.list().get(0).getEditDate(), is(new Date(10000)));
    assertThat(database.getLatestDate(language), is(new Date(0)));
    assertNull(database.list().get(0).getRuleSubId());
    assertNull(database.list().get(0).getFixDate());
    assertThat(database.getCheckDates().size(), is(0));
    // same ID, different character positions
    RuleMatch ruleMatch2 = new RuleMatch(new FakeRule(1), 9, 11, "my message");
    AtomFeedItem feedItem2 = new AtomFeedItem("//id2?diff=124", "title", "summary2", new Date(9000000000L));
    WikipediaRuleMatch wikiRuleMatch2 = new WikipediaRuleMatch(language, ruleMatch2, "my context", feedItem2);
    int affected = database.markedFixed(wikiRuleMatch2);
    assertThat(affected, is(1));
    assertThat(database.list().get(0).getFixDate(), is(new Date(9000000000L)));
    assertThat(database.list().get(0).getDiffId(), is(123L));
    assertThat(database.list().get(0).getFixDiffId(), is(124L));
    assertThat(database.getLatestDate(language), is(new Date(0)));
    assertThat(database.getCheckDates().size(), is(0));
}
Also used : CategoryId(org.languagetool.rules.CategoryId) Category(org.languagetool.rules.Category) RuleMatch(org.languagetool.rules.RuleMatch) Language(org.languagetool.Language) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Category (org.languagetool.rules.Category)4 CategoryId (org.languagetool.rules.CategoryId)3 RuleMatch (org.languagetool.rules.RuleMatch)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 Language (org.languagetool.Language)1 ITSIssueType (org.languagetool.rules.ITSIssueType)1 Rule (org.languagetool.rules.Rule)1 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)1 SpellingCheckRule (org.languagetool.rules.spelling.SpellingCheckRule)1 DisambiguationPatternRule (org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule)1