Search in sources :

Example 11 with AbstractPatternRule

use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.

the class JLanguageTool method activateDefaultPatternRules.

/**
   * Loads and activates the pattern rules from
   * {@code org/languagetool/rules/<languageCode>/grammar.xml}.
   */
private void activateDefaultPatternRules() throws IOException {
    List<AbstractPatternRule> patternRules = language.getPatternRules();
    List<String> enabledRules = language.getDefaultEnabledRulesForVariant();
    List<String> disabledRules = language.getDefaultDisabledRulesForVariant();
    if (!enabledRules.isEmpty() || !disabledRules.isEmpty()) {
        for (AbstractPatternRule patternRule : patternRules) {
            if (enabledRules.contains(patternRule.getId())) {
                patternRule.setDefaultOn();
            }
            if (disabledRules.contains(patternRule.getId())) {
                patternRule.setDefaultOff();
            }
        }
    }
    userRules.addAll(patternRules);
}
Also used : AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 12 with AbstractPatternRule

use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.

the class CompactStdoutHandler method handleResult.

@Override
protected void handleResult(Sentence sentence, List<RuleMatch> ruleMatches, Language language) {
    if (ruleMatches.size() > 0) {
        for (RuleMatch match : ruleMatches) {
            String ruleId = match.getRule().getId();
            if (match.getRule() instanceof AbstractPatternRule) {
                AbstractPatternRule pRule = (AbstractPatternRule) match.getRule();
                ruleId = pRule.getFullId();
            }
            System.out.println(ruleId + ": " + contextTools.getContext(match.getFromPos(), match.getToPos(), sentence.getText()));
            checkMaxErrors(++errorCount);
        }
    }
    checkMaxSentences(++sentenceCount);
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 13 with AbstractPatternRule

use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.

the class CommandLineTools method printMatches.

/**
   * Displays matches in a simple text format.
   * @param ruleMatches Matches from rules.
   * @param prevMatches Number of previously found matches.
   * @param contents  The text that was checked.
   * @param contextSize The size of contents displayed.
   * @since 1.0.1
   */
private static void printMatches(List<RuleMatch> ruleMatches, int prevMatches, String contents, int contextSize) {
    int i = 1;
    ContextTools contextTools = new ContextTools();
    contextTools.setContextSize(contextSize);
    for (RuleMatch match : ruleMatches) {
        Rule rule = match.getRule();
        String output = i + prevMatches + ".) Line " + (match.getLine() + 1) + ", column " + match.getColumn() + ", Rule ID: " + rule.getId();
        if (rule instanceof AbstractPatternRule) {
            AbstractPatternRule pRule = (AbstractPatternRule) rule;
            if (pRule.getSubId() != null) {
                output += "[" + pRule.getSubId() + "]";
            }
        }
        System.out.println(output);
        String msg = match.getMessage();
        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(), contents));
        if (rule.getUrl() != null) {
            System.out.println("More info: " + rule.getUrl());
        }
        if (i < ruleMatches.size()) {
            System.out.println();
        }
        i++;
    }
}
Also used : RuleMatch(org.languagetool.rules.RuleMatch) BitextRule(org.languagetool.rules.bitext.BitextRule) TextLevelRule(org.languagetool.rules.TextLevelRule) Rule(org.languagetool.rules.Rule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) ContextTools(org.languagetool.tools.ContextTools) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 14 with AbstractPatternRule

use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.

the class Main method main.

/**
   * Command line tool to check plain text files.
   */
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
    JnaTools.setBugWorkaroundProperty();
    CommandLineParser commandLineParser = new CommandLineParser();
    CommandLineOptions options = null;
    try {
        options = commandLineParser.parseOptions(args);
    } catch (WrongParameterNumberException e) {
        commandLineParser.printUsage();
        System.exit(1);
    } catch (IllegalArgumentException e) {
        System.err.println(e.toString());
        System.exit(1);
    } catch (UnknownParameterException e) {
        if (e.getMessage() != null) {
            System.err.println(e.getMessage());
        } else {
            System.err.println(e.toString());
        }
        commandLineParser.printUsage(System.err);
        System.exit(1);
    }
    if (options.isPrintUsage()) {
        commandLineParser.printUsage();
        System.exit(1);
    }
    if (options.isPrintVersion()) {
        System.out.println("LanguageTool version " + JLanguageTool.VERSION + " (" + JLanguageTool.BUILD_DATE + ")");
        System.exit(0);
    }
    if (options.isPrintLanguages()) {
        printLanguages();
        System.exit(0);
    }
    if (options.getFilename() == null) {
        options.setFilename("-");
    }
    String languageHint = null;
    if (options.getLanguage() == null) {
        if (!options.isXmlFormat() && !options.isAutoDetect()) {
            System.err.println("No language specified, using English (no spell checking active, " + "specify a language variant like 'en-GB' if available)");
        }
        options.setLanguage(new English());
    } else if (!options.isXmlFormat() && !options.isApplySuggestions()) {
        languageHint = "Expected text language: " + options.getLanguage().getName();
    }
    options.getLanguage().getSentenceTokenizer().setSingleLineBreaksMarksParagraph(options.isSingleLineBreakMarksParagraph());
    Main prg = new Main(options);
    if (options.getFalseFriendFile() != null) {
        List<AbstractPatternRule> ffRules = prg.lt.loadFalseFriendRules(options.getFalseFriendFile());
        for (AbstractPatternRule ffRule : ffRules) {
            prg.lt.addRule(ffRule);
        }
    }
    if (prg.lt.getAllActiveRules().size() == 0) {
        List<String> catIds = options.getEnabledCategories().stream().map(i -> i.toString()).collect(Collectors.toList());
        throw new RuntimeException("No rules are active. Please make sure your rule ids " + "(" + options.getEnabledRules() + ") and " + "category ids (" + catIds + ") are correct");
    }
    if (languageHint != null) {
        String spellHint = prg.isSpellCheckingActive() ? "" : " (no spell checking active, specify a language variant like 'en-GB' if available)";
        System.err.println(languageHint + spellHint);
    }
    prg.setListUnknownWords(options.isListUnknown());
    if (options.isProfile()) {
        prg.setProfilingMode();
    }
    if (options.isBitext()) {
        if (options.getMotherTongue() == null) {
            throw new IllegalArgumentException("You have to set the source language (as mother tongue) in bitext mode");
        }
        File bitextRuleFile = options.getBitextRuleFile() != null ? new File(options.getBitextRuleFile()) : null;
        prg.setBitextMode(options.getMotherTongue(), options.getDisabledRules(), options.getEnabledRules(), bitextRuleFile);
    }
    if (options.isRecursive()) {
        prg.runRecursive(options.getFilename(), options.getEncoding(), options.isXmlFiltering());
    } else {
        if (options.isLineByLine()) {
            prg.runOnFileLineByLine(options.getFilename(), options.getEncoding());
        } else {
            prg.runOnFile(options.getFilename(), options.getEncoding(), options.isXmlFiltering());
        }
    }
    prg.cleanUp();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) JLanguageTool(org.languagetool.JLanguageTool) StringTools.readerToString(org.languagetool.tools.StringTools.readerToString) ArrayList(java.util.ArrayList) MultiThreadedJLanguageTool(org.languagetool.MultiThreadedJLanguageTool) HashSet(java.util.HashSet) Charset(java.nio.charset.Charset) BOMInputStream(org.apache.commons.io.input.BOMInputStream) LanguageIdentifier(org.languagetool.language.LanguageIdentifier) TabBitextReader(org.languagetool.bitext.TabBitextReader) ApiPrintMode(org.languagetool.tools.StringTools.ApiPrintMode) AmericanEnglish(org.languagetool.language.AmericanEnglish) BitextRule(org.languagetool.rules.bitext.BitextRule) IOException(java.io.IOException) ByteOrderMark(org.apache.commons.io.ByteOrderMark) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) Rule(org.languagetool.rules.Rule) English(org.languagetool.language.English) List(java.util.List) Tools(org.languagetool.tools.Tools) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StringTools.filterXML(org.languagetool.tools.StringTools.filterXML) SAXException(org.xml.sax.SAXException) Language(org.languagetool.Language) PatternRuleLoader(org.languagetool.rules.patterns.PatternRuleLoader) Languages(org.languagetool.Languages) JnaTools(org.languagetool.tools.JnaTools) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) InputStream(java.io.InputStream) StringTools.readerToString(org.languagetool.tools.StringTools.readerToString) AmericanEnglish(org.languagetool.language.AmericanEnglish) English(org.languagetool.language.English) File(java.io.File) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 15 with AbstractPatternRule

use of org.languagetool.rules.patterns.AbstractPatternRule in project languagetool by languagetool-org.

the class DatabaseHandler method handleResult.

@Override
protected void handleResult(Sentence sentence, List<RuleMatch> ruleMatches, Language language) {
    try {
        java.sql.Date nowDate = new java.sql.Date(new Date().getTime());
        for (RuleMatch match : ruleMatches) {
            String smallContext = smallContextTools.getContext(match.getFromPos(), match.getToPos(), sentence.getText());
            insertSt.setString(1, language.getShortCode());
            Rule rule = match.getRule();
            insertSt.setString(2, rule.getId());
            insertSt.setString(3, rule.getCategory().getName());
            if (rule instanceof AbstractPatternRule) {
                AbstractPatternRule patternRule = (AbstractPatternRule) rule;
                insertSt.setString(4, patternRule.getSubId());
            } else {
                insertSt.setNull(4, Types.VARCHAR);
            }
            insertSt.setString(5, rule.getDescription());
            insertSt.setString(6, StringUtils.abbreviate(match.getMessage(), 255));
            String context = contextTools.getContext(match.getFromPos(), match.getToPos(), sentence.getText());
            if (context.length() > MAX_CONTEXT_LENGTH) {
                // let's skip these strange cases, as shortening the text might leave us behind with invalid markup etc
                continue;
            }
            insertSt.setString(7, context);
            insertSt.setString(8, StringUtils.abbreviate(smallContext, 255));
            // should actually be the dump's date, but isn't really used anyway...
            insertSt.setDate(9, nowDate);
            insertSt.setDate(10, nowDate);
            insertSt.setString(11, sentence.getUrl());
            insertSt.setString(12, sentence.getSource());
            insertSt.addBatch();
            if (++batchCount >= batchSize) {
                executeBatch();
                batchCount = 0;
            }
            checkMaxErrors(++errorCount);
            if (errorCount % 100 == 0) {
                System.out.println("Storing error #" + errorCount + " for text:");
                System.out.println("  " + sentence.getText());
            }
        }
        checkMaxSentences(++sentenceCount);
    } catch (DocumentLimitReachedException | ErrorLimitReachedException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("Error storing matches for '" + sentence.getTitle() + "'", e);
    }
}
Also used : Date(java.util.Date) IOException(java.io.IOException) java.sql(java.sql) RuleMatch(org.languagetool.rules.RuleMatch) Rule(org.languagetool.rules.Rule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) 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