Search in sources :

Example 16 with AbstractPatternRule

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);
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 17 with AbstractPatternRule

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;
}
Also used : PatternRule(org.languagetool.rules.patterns.PatternRule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 18 with AbstractPatternRule

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);
}
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 19 with AbstractPatternRule

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();
}
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 20 with AbstractPatternRule

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();
}
Also used : AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Aggregations

AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)22 RuleMatch (org.languagetool.rules.RuleMatch)7 Rule (org.languagetool.rules.Rule)5 ArrayList (java.util.ArrayList)4 InputStream (java.io.InputStream)3 JLanguageTool (org.languagetool.JLanguageTool)3 PatternRuleLoader (org.languagetool.rules.patterns.PatternRuleLoader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Query (org.apache.lucene.search.Query)2 Test (org.junit.Test)2 Language (org.languagetool.Language)2 BitextRule (org.languagetool.rules.bitext.BitextRule)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1