Search in sources :

Example 1 with IncorrectExample

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

the class AfterTheDeadlineEvaluator method run.

private void run(Language lang) throws IOException, InterruptedException {
    List<Rule> rules = getRules(lang);
    int sentenceCount = 0;
    int errorFoundCount = 0;
    System.out.println("Starting test for " + lang.getName() + " on " + urlPrefix);
    System.out.println("Wait time between HTTP requests: " + WAIT_TIME_MILLIS + "ms");
    System.out.println("Starting test on " + rules.size() + " rules");
    for (Rule rule : rules) {
        if (rule.isDefaultOff()) {
            System.out.println("Skipping rule that is off by default: " + rule.getId());
            continue;
        }
        List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
        System.out.println("\n" + rule.getId() + ":");
        if (incorrectExamples.size() == 0) {
            System.out.println(" (no examples)");
            continue;
        }
        for (IncorrectExample example : incorrectExamples) {
            boolean match = queryAtDServer(example);
            sentenceCount++;
            if (match) {
                errorFoundCount++;
            }
            String marker = match ? "+" : "-";
            System.out.println("  [" + marker + "] " + example.getExample().replace("<marker>", "<m>").replace("</marker>", "</m>"));
            Thread.sleep(WAIT_TIME_MILLIS);
        }
    //use this to stop: if (sentenceCount > 100) { break; }
    }
    System.out.println("\nDone.");
    System.out.println("Sentence count: " + sentenceCount);
    float percentage = (float) errorFoundCount / sentenceCount * 100;
    System.out.printf("Expected errors found: " + errorFoundCount + " (%.2f%%)\n", percentage);
}
Also used : Rule(org.languagetool.rules.Rule) IncorrectExample(org.languagetool.rules.IncorrectExample)

Example 2 with IncorrectExample

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

the class AfterTheDeadlineEvaluatorTest method testIsExpectedErrorFound.

@Test
public void testIsExpectedErrorFound() throws XPathExpressionException {
    AfterTheDeadlineEvaluator evaluator = new AfterTheDeadlineEvaluator("fake");
    IncorrectExample example = new IncorrectExample("This <marker>is is</marker> a test");
    assertTrue(evaluator.isExpectedErrorFound(example, "<results><error><string>is is</string></error></results>"));
    assertFalse(evaluator.isExpectedErrorFound(example, "<results><error><string>This is</string></error></results>"));
    assertFalse(evaluator.isExpectedErrorFound(example, "<results></results>"));
    assertTrue(evaluator.isExpectedErrorFound(example, "<results>" + "<error><string>foo bar</string></error>" + "<error><string>is is</string></error>" + "</results>"));
}
Also used : IncorrectExample(org.languagetool.rules.IncorrectExample) Test(org.junit.Test)

Example 3 with IncorrectExample

use of org.languagetool.rules.IncorrectExample 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 4 with IncorrectExample

use of org.languagetool.rules.IncorrectExample 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 5 with IncorrectExample

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

the class BitextPatternRuleHandler method setExample.

private IncorrectExample setExample() {
    IncorrectExample example = null;
    if (inCorrectExample) {
        example = new IncorrectExample(correctExample.toString());
    } else if (inIncorrectExample) {
        String[] corrections = exampleCorrection.toString().split("\\|");
        if (corrections.length > 0 && corrections[0].length() > 0) {
            example = new IncorrectExample(incorrectExample.toString(), Arrays.asList(corrections));
        } else {
            example = new IncorrectExample(incorrectExample.toString());
        }
    } else if (inErrorTriggerExample) {
        throw new RuntimeException("'triggers_error' is not supported for bitext XML");
    }
    correctExample = new StringBuilder();
    incorrectExample = new StringBuilder();
    exampleCorrection = new StringBuilder();
    return example;
}
Also used : IncorrectExample(org.languagetool.rules.IncorrectExample)

Aggregations

IncorrectExample (org.languagetool.rules.IncorrectExample)12 Rule (org.languagetool.rules.Rule)8 JLanguageTool (org.languagetool.JLanguageTool)4 File (java.io.File)3 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)3 PatternRule (org.languagetool.rules.patterns.PatternRule)3 FileReader (java.io.FileReader)2 Test (org.junit.Test)2 CorrectExample (org.languagetool.rules.CorrectExample)2 DisambiguationPatternRule (org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule)2 ArrayList (java.util.ArrayList)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 FieldType (org.apache.lucene.document.FieldType)1 AnalyzedSentence (org.languagetool.AnalyzedSentence)1 AnalyzedTokenReadings (org.languagetool.AnalyzedTokenReadings)1 MultiThreadedJLanguageTool (org.languagetool.MultiThreadedJLanguageTool)1 ChunkTag (org.languagetool.chunking.ChunkTag)1 RuleMatch (org.languagetool.rules.RuleMatch)1 SpellingCheckRule (org.languagetool.rules.spelling.SpellingCheckRule)1