use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.
the class StdoutHandler method handleResult.
@Override
protected void handleResult(Sentence sentence, List<RuleMatch> ruleMatches, Language language) {
if (ruleMatches.size() > 0) {
int i = 1;
System.out.println("\nTitle: " + sentence.getTitle());
for (RuleMatch match : ruleMatches) {
String output = i + ".) Line " + (match.getLine() + 1) + ", column " + match.getColumn() + ", Rule ID: " + match.getRule().getId();
if (match.getRule() instanceof AbstractPatternRule) {
AbstractPatternRule pRule = (AbstractPatternRule) match.getRule();
output += "[" + pRule.getSubId() + "]";
}
System.out.println(output);
String msg = match.getMessage();
msg = msg.replaceAll("<suggestion>", "'");
msg = msg.replaceAll("</suggestion>", "'");
System.out.println("Message: " + msg);
List<String> replacements = match.getSuggestedReplacements();
if (!replacements.isEmpty()) {
System.out.println("Suggestion: " + String.join("; ", replacements));
}
System.out.println(contextTools.getPlainTextContext(match.getFromPos(), match.getToPos(), sentence.getText()));
i++;
checkMaxErrors(++errorCount);
}
}
checkMaxSentences(++sentenceCount);
}
use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.
the class JLanguageTool method getPatternRulesByIdAndSubId.
/**
* Get pattern rules by Id and SubId. This returns a list because rules that use {@code <or>...</or>}
* are internally expanded into several rules.
* @return a List of {@link Rule} objects
* @since 2.3
*/
public List<AbstractPatternRule> getPatternRulesByIdAndSubId(String Id, String subId) {
List<Rule> rules = getAllRules();
List<AbstractPatternRule> rulesById = new ArrayList<>();
for (Rule rule : rules) {
if (rule instanceof AbstractPatternRule) {
if (rule.getId().equals(Id) && ((AbstractPatternRule) rule).getSubId().equals(subId)) {
rulesById.add((AbstractPatternRule) rule);
}
}
}
return rulesById;
}
use of org.languagetool.rules.patterns.AbstractPatternRule 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);
}
use of org.languagetool.rules.patterns.AbstractPatternRule 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();
}
use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.
the class RuleMatchesAsJsonSerializer method writeRule.
private void writeRule(JsonGenerator g, RuleMatch match) throws IOException {
g.writeObjectFieldStart("rule");
g.writeStringField("id", match.getRule().getId());
if (match.getRule() instanceof AbstractPatternRule) {
AbstractPatternRule pRule = (AbstractPatternRule) match.getRule();
if (pRule.getSubId() != null) {
g.writeStringField("subId", pRule.getSubId());
}
}
g.writeStringField("description", match.getRule().getDescription());
g.writeStringField("issueType", match.getRule().getLocQualityIssueType().toString());
if (match.getRule().getUrl() != null) {
// currently only one, but keep it extensible
g.writeArrayFieldStart("urls");
g.writeStartObject();
g.writeStringField("value", match.getRule().getUrl().toString());
g.writeEndObject();
g.writeEndArray();
}
writeCategory(g, match.getRule().getCategory());
g.writeEndObject();
}
Aggregations