use of org.languagetool.rules.CategoryId in project languagetool by languagetool-org.
the class RuleMatchesAsJsonSerializer method writeCategory.
private void writeCategory(JsonGenerator g, Category category) throws IOException {
g.writeObjectFieldStart("category");
CategoryId catId = category.getId();
if (catId != null) {
g.writeStringField("id", catId.toString());
g.writeStringField("name", category.getName());
}
g.writeEndObject();
}
use of org.languagetool.rules.CategoryId 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);
}
}
use of org.languagetool.rules.CategoryId in project languagetool by languagetool-org.
the class JLanguageToolTest method testEnableRulesCategories.
@Test
public void testEnableRulesCategories() throws Exception {
JLanguageTool lt = new JLanguageTool(new Demo());
List<String> ruleIds = getActiveRuleIds(lt);
assertTrue(ruleIds.contains("DEMO_RULE"));
assertFalse(ruleIds.contains("IN_OFF_CATEGORY"));
lt.disableCategory(new CategoryId("MISC"));
List<String> ruleIds2 = getActiveRuleIds(lt);
assertFalse(ruleIds2.contains("DEMO_RULE"));
assertFalse(ruleIds2.contains("IN_OFF_CATEGORY"));
lt.enableRuleCategory(new CategoryId("MISC"));
List<String> ruleIds3 = getActiveRuleIds(lt);
assertTrue(ruleIds3.contains("DEMO_RULE"));
assertFalse(ruleIds3.contains("IN_OFF_CATEGORY"));
lt.enableRuleCategory(new CategoryId("DEFAULT_OFF"));
List<String> ruleIds4 = getActiveRuleIds(lt);
assertTrue(ruleIds4.contains("DEMO_RULE"));
assertTrue(ruleIds4.contains("IN_OFF_CATEGORY"));
assertFalse(ruleIds4.contains("IN_OFF_CATEGORY_OFF_ITSELF"));
lt.enableRule("IN_OFF_CATEGORY_OFF_ITSELF");
List<String> ruleIds5 = getActiveRuleIds(lt);
assertTrue(ruleIds5.contains("IN_OFF_CATEGORY_OFF_ITSELF"));
}
use of org.languagetool.rules.CategoryId in project languagetool by languagetool-org.
the class TextChecker method getCategoryIds.
@NotNull
private List<CategoryId> getCategoryIds(String paramName, Map<String, String> parameters) {
List<String> stringIds = getCommaSeparatedStrings(paramName, parameters);
List<CategoryId> ids = new ArrayList<>();
for (String stringId : stringIds) {
ids.add(new CategoryId(stringId));
}
return ids;
}
use of org.languagetool.rules.CategoryId 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();
}
Aggregations