Search in sources :

Example 1 with Rule

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

the class ExampleSentenceCorrectionCreator 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.getAllRules()) {
        if (!(rule instanceof PatternRule)) {
            continue;
        }
        List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
        for (IncorrectExample incorrectExample : incorrectExamples) {
            checkCorrections(rule, incorrectExample, xmlLines, tool);
        }
    }
    System.err.println("Added corrections: " + addedCorrectionsCount);
    for (String xmlLine : xmlLines) {
        System.out.println(xmlLine);
    }
}
Also used : PatternRule(org.languagetool.rules.patterns.PatternRule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) 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 Rule

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

the class ExampleSentencePrinter 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);
    }
    JLanguageTool tool = new JLanguageTool(lang);
    System.out.println("<html>");
    System.out.println("<head>");
    System.out.println("  <title>LanguageTool examples sentences</title>");
    System.out.println("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
    System.out.println("</head>");
    System.out.println("<body>");
    int i = 1;
    for (Rule rule : tool.getAllRules()) {
        List<IncorrectExample> incorrectExamples = rule.getIncorrectExamples();
        if (incorrectExamples.size() > 0) {
            String example = incorrectExamples.get(0).getExample().replace("<marker>", "<b>").replace("</marker>", "</b>");
            System.out.println(i + ". " + example + " [" + rule.getId() + "]<br>");
            i++;
        }
    }
    System.out.println("</body>");
    System.out.println("</html>");
}
Also used : JLanguageTool(org.languagetool.JLanguageTool) Rule(org.languagetool.rules.Rule) IncorrectExample(org.languagetool.rules.IncorrectExample) File(java.io.File)

Example 3 with Rule

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

the class PatternRuleTest method getMatches.

private List<RuleMatch> getMatches(Rule rule, String sentence, JLanguageTool languageTool) throws IOException {
    AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(sentence);
    RuleMatch[] matches = rule.match(analyzedSentence);
    if (CHECK_WITH_SENTENCE_SPLITTING) {
        // "real check" with sentence splitting:
        for (Rule r : languageTool.getAllActiveRules()) {
            languageTool.disableRule(r.getId());
        }
        languageTool.enableRule(rule.getId());
        List<RuleMatch> realMatches = languageTool.check(sentence);
        List<String> realMatchRuleIds = new ArrayList<>();
        for (RuleMatch realMatch : realMatches) {
            realMatchRuleIds.add(realMatch.getRule().getId());
        }
        for (RuleMatch match : matches) {
            String ruleId = match.getRule().getId();
            if (!match.getRule().isDefaultOff() && !realMatchRuleIds.contains(ruleId)) {
                System.err.println("WARNING: " + languageTool.getLanguage().getName() + ": missing rule match " + ruleId + " when splitting sentences for test sentence '" + sentence + "'");
            }
        }
    }
    return Arrays.asList(matches);
}
Also used : AnalyzedSentence(org.languagetool.AnalyzedSentence) RuleMatch(org.languagetool.rules.RuleMatch) ArrayList(java.util.ArrayList) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) DisambiguationPatternRule(org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule) Rule(org.languagetool.rules.Rule)

Example 4 with Rule

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

the class PatternRuleTest method validateRuleIds.

private void validateRuleIds(Language lang, JLanguageTool languageTool) {
    List<Rule> allRules = languageTool.getAllRules();
    Set<String> ids = new HashSet<>();
    Set<Class> ruleClasses = new HashSet<>();
    Set<String> categoryIds = new HashSet<>();
    for (Rule rule : allRules) {
        assertIdUniqueness(ids, ruleClasses, lang, rule);
        if (rule.getId().equalsIgnoreCase("ID")) {
            System.err.println("WARNING: " + lang.getShortCodeWithCountryAndVariant() + " has a rule with id 'ID', this should probably be changed");
        }
        Category category = rule.getCategory();
        if (category != null && category.getId() != null) {
            String catId = category.getId().toString();
            if (!catId.matches("[A-Z0-9_-]+") && !categoryIds.contains(catId)) {
                System.err.println("WARNING: category id '" + catId + "' doesn't match expected regexp [A-Z0-9_-]+");
                categoryIds.add(catId);
            }
        }
    }
}
Also used : Category(org.languagetool.rules.Category) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) DisambiguationPatternRule(org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule) Rule(org.languagetool.rules.Rule) HashSet(java.util.HashSet)

Example 5 with Rule

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

the class SpellingCheckRuleTest method testIgnorePhrases.

@Test
public void testIgnorePhrases() throws IOException {
    JLanguageTool langTool = new JLanguageTool(new AmericanEnglish());
    assertThat(langTool.check("A test with myfoo mybar").size(), is(2));
    for (Rule rule : langTool.getAllActiveRules()) {
        if (rule instanceof SpellingCheckRule) {
            ((SpellingCheckRule) rule).acceptPhrases(Arrays.asList("myfoo mybar", "Myy othertest"));
        } else {
            langTool.disableRule(rule.getId());
        }
    }
    assertThat(langTool.check("A test with myfoo mybar").size(), is(0));
    // the words on their own are not ignored
    assertThat(langTool.check("A test with myfoo and mybar").size(), is(2));
    assertThat(langTool.check("myfoo mybar here").size(), is(0));
    assertThat(langTool.check("Myfoo mybar here").size(), is(0));
    assertThat(langTool.check("MYfoo mybar here").size(), is(2));
    assertThat(langTool.check("Myy othertest is okay").size(), is(0));
    assertThat(langTool.check("And Myy othertest is okay").size(), is(0));
    assertThat(langTool.check("But Myy Othertest is not okay").size(), is(2));
    assertThat(langTool.check("But myy othertest is not okay").size(), is(2));
}
Also used : SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) JLanguageTool(org.languagetool.JLanguageTool) AmericanEnglish(org.languagetool.language.AmericanEnglish) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) Rule(org.languagetool.rules.Rule) Test(org.junit.Test)

Aggregations

Rule (org.languagetool.rules.Rule)67 JLanguageTool (org.languagetool.JLanguageTool)16 PatternRule (org.languagetool.rules.patterns.PatternRule)15 ArrayList (java.util.ArrayList)14 RuleMatch (org.languagetool.rules.RuleMatch)14 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)12 Test (org.junit.Test)11 Language (org.languagetool.Language)11 IncorrectExample (org.languagetool.rules.IncorrectExample)8 English (org.languagetool.language.English)7 SpellingCheckRule (org.languagetool.rules.spelling.SpellingCheckRule)7 File (java.io.File)6 PatternToken (org.languagetool.rules.patterns.PatternToken)6 AnalyzedSentence (org.languagetool.AnalyzedSentence)5 AmericanEnglish (org.languagetool.language.AmericanEnglish)5 BitextRule (org.languagetool.rules.bitext.BitextRule)5 GermanSpellerRule (org.languagetool.rules.de.GermanSpellerRule)4 DisambiguationPatternRule (org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule)4 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3