Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prio field to rule #10437

Merged
merged 2 commits into from Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -839,9 +839,12 @@ protected int getPriorityForId(String id) {
public int getRulePriority(Rule rule) {
int categoryPriority = this.getPriorityForId(rule.getCategory().getId().toString());
int rulePriority = this.getPriorityForId(rule.getId());
int rulePriorityFromRule = rule.getPriority();
// if there is a priority defined for rule it takes precedence over category priority
if (rulePriority != 0) {
return rulePriority;
} else if ( rulePriorityFromRule != 0) {
return rulePriorityFromRule;
} else {
return categoryPriority;
}
Expand Down
Expand Up @@ -74,6 +74,7 @@ public abstract class Rule {
private boolean officeDefaultOff = false;
private int minPrevMatches = 0; // minimum number of previous matches to show the rule
private int distanceTokens = -1; // distance (number of tokens) between matches to consider a repetition
private int priority = 0;

public Rule() {
this(null);
Expand Down Expand Up @@ -620,4 +621,12 @@ public boolean isGoalSpecific() {
public void setGoalSpecific(boolean goalSpecific) {
isGoalSpecific = goalSpecific;
}

public int getPriority() {
return priority;
}

public void setPriority(int priority) {
this.priority = priority;
}
}
Expand Up @@ -82,7 +82,7 @@ public AbstractPatternRule(String id, String description, Language language, Lis

public AbstractPatternRule(String id, String description, Language language, List<PatternToken> patternTokens, boolean getUnified) {
this.id = Objects.requireNonNull(id, "id cannot be null");
this.description = Objects.requireNonNull(description, "description ('name' in XML) cannot be null");
this.description = Objects.requireNonNull(description, "description ('name' in XML) cannot be null ruleID: " + id);
this.language = Objects.requireNonNull(language, "language cannot be null");
this.getUnified = getUnified;
if (patternTokens != null) {
Expand Down
Expand Up @@ -20,7 +20,6 @@

import org.apache.commons.lang3.ObjectUtils;
import org.languagetool.*;
import org.languagetool.broker.ResourceDataBroker;
import org.languagetool.rules.*;
import org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule;
import org.xml.sax.Attributes;
Expand Down Expand Up @@ -140,6 +139,10 @@ public void startElement(String namespaceURI, String lName,
categoryToneTags.addAll(Arrays.asList(attrs.getValue("tone_tags").split(" ")));
}
isGoalSpecificCategoryAttribute = attrs.getValue(GOAL_SPECIFIC);
String prioCategoryAttributeValue = attrs.getValue(PRIO);
if (prioCategoryAttributeValue != null) {
prioCategoryAttribute = Integer.parseInt(prioCategoryAttributeValue);
}
break;
case "rules":
String languageStr = attrs.getValue("lang");
Expand Down Expand Up @@ -281,6 +284,10 @@ public void startElement(String namespaceURI, String lName,
} else {
isGoalSpecific = false;
}
String prioRuleAttributeValue = attrs.getValue(PRIO);
if (prioRuleAttributeValue != null) {
prioRuleAttribute = Integer.parseInt(prioRuleAttributeValue);
}
break;
case PATTERN:
startPattern(attrs);
Expand Down Expand Up @@ -425,6 +432,10 @@ public void startElement(String namespaceURI, String lName,
ruleGroupDistanceTokens = Integer.parseInt(distanceTokensStr2);
}
antipatternForRuleGroupsExamples = new ArrayList<>();
String prioRuleGroupAttributeValue = attrs.getValue(PRIO);
if (prioRuleGroupAttributeValue != null) {
prioRuleGroupAttribute = Integer.parseInt(prioRuleGroupAttributeValue);
}
break;
case MATCH:
setMatchElement(attrs, inSuggestion && (isSuggestionSuppressMisspelled || isRuleSuppressMisspelled));
Expand Down Expand Up @@ -481,6 +492,7 @@ public void endElement(String namespaceURI, String sName,
isGoalSpecific = false;
premiumCategoryAttribute = null;
isGoalSpecificCategoryAttribute = null;
prioCategoryAttribute = 0;
break;
case "regexp":
inRegex = false;
Expand Down Expand Up @@ -526,6 +538,7 @@ public void endElement(String namespaceURI, String sName,
ruleToneTags.clear();
isPremiumRule = false;
isGoalSpecific = false;
prioRuleAttribute = 0;
break;
case EXCEPTION:
finalizeExceptions();
Expand Down Expand Up @@ -675,6 +688,7 @@ public void endElement(String namespaceURI, String sName,
premiumRuleGroupAttribute = null;
isGoalSpecificRuleGroupAttribute = null;
antipatternForRuleGroupsExamples.clear();
prioRuleGroupAttribute = 0;
break;
case MARKER:
if (inCorrectExample) {
Expand Down Expand Up @@ -910,6 +924,17 @@ protected void prepareRule(AbstractPatternRule rule) {
} else if (categoryIssueType != null) {
rule.setLocQualityIssueType(ITSIssueType.getIssueType(categoryIssueType));
}
int prio = 0;
if (prioCategoryAttribute != 0) {
prio = prioCategoryAttribute;
}
if (prioRuleGroupAttribute != 0) {
prio = prioRuleGroupAttribute;
}
if (prioRuleAttribute != 0) {
prio = prioRuleAttribute;
}
rule.setPriority(prio);
}

private final Map<String, URL> internedUrls = new HashMap<>();
Expand Down
Expand Up @@ -103,6 +103,7 @@ enum RegexpMode {
protected static final String TABNAME = "tab";
protected static final String MINPREVMATCHES = "min_prev_matches";
protected static final String DISTANCETOKENS = "distance_tokens";
protected static final String PRIO = "prio";

protected List<AbstractPatternRule> rules = new ArrayList<>();
protected Language language;
Expand Down Expand Up @@ -166,6 +167,9 @@ enum RegexpMode {
protected String isGoalSpecificCategoryAttribute;
protected String isGoalSpecificRuleGroupAttribute;
protected boolean isGoalSpecific;
protected int prioCategoryAttribute;
protected int prioRuleGroupAttribute;
protected int prioRuleAttribute;

protected boolean tokenLevelCaseSensitive;
protected boolean tokenLevelCaseSet;
Expand Down
Expand Up @@ -77,6 +77,7 @@
<xs:attribute ref="tags" />
<xs:attribute ref="tone_tags" />
<xs:attribute name="is_goal_specific" type="xs:boolean"/>
<xs:attribute name="prio" type="xs:integer" default="0"/>
</xs:complexType>
</xs:element>

Expand Down Expand Up @@ -123,6 +124,7 @@
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="prio" type="xs:integer" default="0"/>
</xs:complexType>
</xs:element>

Expand Down Expand Up @@ -250,6 +252,7 @@
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="prio" type="xs:integer" default="0"/>
</xs:complexType>
</xs:element>

Expand Down
Expand Up @@ -225,6 +225,24 @@ public void testToneTagsAttribute() throws IOException {
assertTrue(isGoalSpecificFromCategoryRule.isGoalSpecific());
}

@Test
public void testPrioAttribute() throws IOException {
PatternRuleLoader prg = new PatternRuleLoader();
String xmlFile = "/xx/grammar-withPrio.xml";
List<AbstractPatternRule> xmlPrioRules = prg.getRules(JLanguageTool.getDataBroker().getFromRulesDirAsStream(xmlFile), xmlFile, null);

Rule rulePrio15 = getRuleById("CAT-PRIO-5-RG-PRIO-10-R-PRIO-15", xmlPrioRules);
assertEquals(15, rulePrio15.getPriority());
Rule rulePrio10 = getRuleById("CAT-PRIO-5-RG-PRIO-10-R-PRIO-0", xmlPrioRules);
assertEquals(10, rulePrio10.getPriority());
Rule rulePrio5 = getRuleById("CAT-PRIO-5-RG-PRIO-0-R-PRIO-0", xmlPrioRules);
assertEquals(5,rulePrio5.getPriority());
Rule rulePrio0_0 = getRuleById("CAT-PRIO-0-RG-PRIO-0-R-PRIO-0", xmlPrioRules);
assertEquals(0, rulePrio0_0.getPriority());
Rule rulePrio0_1 = getRuleById("CAT-PRIO-0-R-PRIO-0", xmlPrioRules);
assertEquals(0, rulePrio0_1.getPriority());
}

private Set<String> getCategoryNames(List<AbstractPatternRule> rules) {
Set<String> categories = new HashSet<>();
for (AbstractPatternRule rule : rules) {
Expand Down
@@ -0,0 +1,86 @@
<?xml-stylesheet type="text/xsl" href="../print.xsl"
title="Pretty print" ?>
<?xml-stylesheet type="text/css" href="../rules.css"
title="Easy editing stylesheet" ?>
<!--
~ LanguageTool, a natural language style checker
~ Copyright (c) 2024. Stefan Viol (https://stevio.de)
~
~ This library is free software; you can redistribute it and/or
~ modify it under the terms of the GNU Lesser General Public
~ License as published by the Free Software Foundation; either
~ version 2.1 of the License, or (at your option) any later version.
~
~ This library is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
~ Lesser General Public License for more details.
~
~ You should have received a copy of the GNU Lesser General Public
~ License along with this library; if not, write to the Free Software
~ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
~ USA
-->

<rules lang="xx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../main/resources/org/languagetool/rules/rules.xsd">
<category id="CAT-PRIO-5" name="CAT-PRIO-5" prio="5">
<rulegroup id="CAT-PRIO-5-RG-PRIO-10" name="CAT-PRIO-5-RG-PRIO-10" prio="10">
<rule id="CAT-PRIO-5-RG-PRIO-10-R-PRIO-15" prio="15">
<pattern>
<token>fake1</token>
</pattern>
<message>msg1</message>
<example correction="">
<marker>fake1</marker>
</example>
<example>fake2</example>
</rule>
<rule id="CAT-PRIO-5-RG-PRIO-10-R-PRIO-0">
<pattern>
<token>fake1</token>
</pattern>
<message>msg3</message>
<example correction="">
<marker>fake1</marker>
</example>
<example>fake2</example>
</rule>
</rulegroup>
<rulegroup id="CAT-PRIO-5-RG-PRIO-0" name="CAT-PRIO-5-RG-PRIO-0">
<rule id="CAT-PRIO-5-RG-PRIO-0-R-PRIO-0">
<pattern>
<token>fake1</token>
</pattern>
<message>msg3</message>
<example correction="">
<marker>fake1</marker>
</example>
<example>fake2</example>
</rule>
</rulegroup>
</category>
<category id="CAT-PRIO-0" name="CAT-PRIO-0">
<rulegroup id="CAT-PRIO-0-RG-PRIO-0" name="CAT-PRIO-5-RG-PRIO-10">
<rule id="CAT-PRIO-0-RG-PRIO-0-R-PRIO-0">
<pattern>
<token>fake1</token>
</pattern>
<message>msg1</message>
<example correction="">
<marker>fake1</marker>
</example>
<example>fake2</example>
</rule>
</rulegroup>
<rule id="CAT-PRIO-0-R-PRIO-0" name="CAT-PRIO-0-R-PRIO-0">
<pattern>
<token>fake1</token>
</pattern>
<message>msg1</message>
<example correction="">
<marker>fake1</marker>
</example>
<example>fake2</example>
</rule>
</category>
</rules>
Expand Up @@ -189,21 +189,7 @@ public LanguageMaintainedState getMaintainedState() {

@Override
public int getRulePriority(Rule rule) {
int categoryPriority = this.getPriorityForId(rule.getCategory().getId().toString());
int rulePriority = this.getPriorityForId(rule.getId());
// if there is a priority defined for the rule,
// it takes precedence over category priority
if (rulePriority != 0) {
return rulePriority;
}
if (categoryPriority != 0) {
return categoryPriority;
}
if (rule.getLocQualityIssueType().equals(ITSIssueType.Style)) {
// don't let style issues hide more important errors
return -50;
}
return 0;
return rule.getLocQualityIssueType().equals(ITSIssueType.Style) ? -50 : super.getRulePriority(rule);
}

@Override
Expand Down
Expand Up @@ -295,21 +295,7 @@ public void close() throws Exception {

@Override
public int getRulePriority(Rule rule) {
int categoryPriority = this.getPriorityForId(rule.getCategory().getId().toString());
int rulePriority = this.getPriorityForId(rule.getId());
// if there is a priority defined for the rule,
// it takes precedence over category priority
if (rulePriority != 0) {
return rulePriority;
}
if (categoryPriority != 0) {
return categoryPriority;
}
if (rule.getLocQualityIssueType().equals(ITSIssueType.Style)) {
// don't let style issues hide more important errors
return -50;
}
return 0;
return rule.getLocQualityIssueType().equals(ITSIssueType.Style) ? -50 : super.getRulePriority(rule);
}

private final static Map<String, Integer> id2prio = new HashMap<>();
Expand Down