Search in sources :

Example 1 with CorrectExample

use of org.languagetool.rules.CorrectExample in project languagetool by languagetool-org.

the class UselessExampleFinder method run.

private void run(Language lang) throws IOException {
    File basePath = new File("/lt/git/languagetool/languagetool-language-modules");
    if (!basePath.exists()) {
        throw new RuntimeException("basePath does not exist: " + basePath);
    }
    String langCode = lang.getShortCode();
    File xml = new File(basePath, "/" + langCode + "/src/main/resources/org/languagetool/rules/" + langCode + "/grammar.xml");
    List<String> xmlLines = IOUtils.readLines(new FileReader(xml));
    JLanguageTool tool = new JLanguageTool(lang);
    for (Rule rule : tool.getAllActiveRules()) {
        if (!(rule instanceof PatternRule)) {
            continue;
        }
        List<CorrectExample> correctExamples = rule.getCorrectExamples();
        List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
        for (IncorrectExample incorrectExample : incorrectExamples) {
            checkCorrections(rule, correctExamples, incorrectExample, xmlLines);
        }
    }
    System.err.println("Useless examples: " + uselessExampleCount);
    System.err.println("Removed lines: " + removedLinesCount);
    for (String xmlLine : xmlLines) {
        System.out.println(xmlLine);
    }
}
Also used : PatternRule(org.languagetool.rules.patterns.PatternRule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) CorrectExample(org.languagetool.rules.CorrectExample) JLanguageTool(org.languagetool.JLanguageTool) FileReader(java.io.FileReader) Rule(org.languagetool.rules.Rule) PatternRule(org.languagetool.rules.patterns.PatternRule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) IncorrectExample(org.languagetool.rules.IncorrectExample) File(java.io.File)

Example 2 with CorrectExample

use of org.languagetool.rules.CorrectExample in project languagetool by languagetool-org.

the class FalseFriendRuleHandler method endElement.

@Override
public void endElement(String namespaceURI, String sName, String qName) throws SAXException {
    switch(qName) {
        case RULE:
            if (language.equalsConsiderVariantsIfSpecified(textLanguage) && translationLanguage != null && translationLanguage.equalsConsiderVariantsIfSpecified(motherTongue) && language != motherTongue && !translations.isEmpty()) {
                formatter.applyPattern(messages.getString("false_friend_hint"));
                String tokensAsString = StringUtils.join(patternTokens, " ").replace('|', '/');
                Object[] messageArguments = { tokensAsString, messages.getString(textLanguage.getShortCode()), formatTranslations(translations), messages.getString(motherTongue.getShortCode()) };
                String description = formatter.format(messageArguments);
                PatternRule rule = new FalseFriendPatternRule(id, language, patternTokens, messages.getString("false_friend_desc") + " " + tokensAsString, description, messages.getString("false_friend"));
                rule.setCorrectExamples(correctExamples);
                rule.setIncorrectExamples(incorrectExamples);
                rule.setCategory(Categories.FALSE_FRIENDS.getCategory(messages));
                if (defaultOff) {
                    rule.setDefaultOff();
                }
                rules.add(rule);
            }
            if (patternTokens != null) {
                patternTokens.clear();
            }
            break;
        case TOKEN:
            finalizeTokens();
            break;
        case PATTERN:
            inPattern = false;
            break;
        case TRANSLATION:
            if (currentTranslationLanguage != null && currentTranslationLanguage.equalsConsiderVariantsIfSpecified(motherTongue)) {
                // currentTranslationLanguage can be null if the language is not supported
                translations.add(translation);
            }
            if (currentTranslationLanguage != null && currentTranslationLanguage.equalsConsiderVariantsIfSpecified(textLanguage) && language.equalsConsiderVariantsIfSpecified(motherTongue)) {
                suggestions.add(translation.toString());
            }
            translation = new StringBuilder();
            inTranslation = false;
            currentTranslationLanguage = null;
            break;
        case EXAMPLE:
            if (inCorrectExample) {
                correctExamples.add(new CorrectExample(correctExample.toString()));
            } else if (inIncorrectExample) {
                incorrectExamples.add(new IncorrectExample(incorrectExample.toString()));
            }
            inCorrectExample = false;
            inIncorrectExample = false;
            correctExample = new StringBuilder();
            incorrectExample = new StringBuilder();
            break;
        case MESSAGE:
            inMessage = false;
            break;
        case RULEGROUP:
            if (!suggestions.isEmpty()) {
                List<String> l = new ArrayList<>(suggestions);
                suggestionMap.put(id, l);
                suggestions.clear();
            }
            inRuleGroup = false;
            break;
    }
}
Also used : CorrectExample(org.languagetool.rules.CorrectExample) IncorrectExample(org.languagetool.rules.IncorrectExample)

Example 3 with CorrectExample

use of org.languagetool.rules.CorrectExample in project languagetool by languagetool-org.

the class PatternRuleTest method testCorrectSentences.

private void testCorrectSentences(JLanguageTool languageTool, JLanguageTool allRulesLanguageTool, Language lang, AbstractPatternRule rule) throws IOException {
    List<CorrectExample> goodSentences = rule.getCorrectExamples();
    // necessary for XML Pattern rules containing <or>
    List<AbstractPatternRule> rules = allRulesLanguageTool.getPatternRulesByIdAndSubId(rule.getId(), rule.getSubId());
    for (CorrectExample goodSentenceObj : goodSentences) {
        // enable indentation use
        String goodSentence = goodSentenceObj.getExample().replaceAll("[\\n\\t]+", "");
        goodSentence = cleanXML(goodSentence);
        assertTrue(lang + ": Empty correct example in rule " + rule.getFullId(), goodSentence.trim().length() > 0);
        boolean isMatched = false;
        // necessary for XML Pattern rules containing <or>
        for (Rule auxRule : rules) {
            isMatched = isMatched || match(auxRule, goodSentence, languageTool);
        }
        if (isMatched) {
            AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(goodSentence);
            StringBuilder sb = new StringBuilder("Analyzed token readings:");
            for (AnalyzedTokenReadings atr : analyzedSentence.getTokens()) {
                sb.append(" ").append(atr);
            }
            fail(lang + ": Did not expect error in:\n" + "  " + goodSentence + "\n" + "  " + sb + "\n" + "Matching Rule: " + rule.getFullId());
        }
    // avoid matches with all the *other* rules:
    /*
      List<RuleMatch> matches = allRulesLanguageTool.check(goodSentence);
      for (RuleMatch match : matches) {
        System.err.println("WARN: " + lang.getShortCode() + ": '" + goodSentence + "' did not match "
                + rule.getId() + " but matched " + match.getRule().getId());
      }
      */
    }
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence) CorrectExample(org.languagetool.rules.CorrectExample) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) DisambiguationPatternRule(org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule) Rule(org.languagetool.rules.Rule) AnalyzedTokenReadings(org.languagetool.AnalyzedTokenReadings)

Aggregations

CorrectExample (org.languagetool.rules.CorrectExample)3 IncorrectExample (org.languagetool.rules.IncorrectExample)2 Rule (org.languagetool.rules.Rule)2 File (java.io.File)1 FileReader (java.io.FileReader)1 AnalyzedSentence (org.languagetool.AnalyzedSentence)1 AnalyzedTokenReadings (org.languagetool.AnalyzedTokenReadings)1 JLanguageTool (org.languagetool.JLanguageTool)1 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)1 PatternRule (org.languagetool.rules.patterns.PatternRule)1 SpellingCheckRule (org.languagetool.rules.spelling.SpellingCheckRule)1 DisambiguationPatternRule (org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule)1