Search in sources :

Example 26 with PatternRule

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

the class SimpleRuleCounter method countForLanguage.

private void countForLanguage(List<Rule> allRules, Language language) {
    int simpleCount = 0;
    for (Rule rule : allRules) {
        boolean isSimple = true;
        if (rule instanceof PatternRule) {
            PatternRule patternRule = (PatternRule) rule;
            List<PatternToken> tokens = patternRule.getPatternTokens();
            for (PatternToken token : tokens) {
                if (!isSimple(token)) {
                    isSimple = false;
                    break;
                }
            }
            if (isSimple) {
                simpleCount++;
            //System.out.println("Simple: " + patternRule.getId());
            //System.out.println(patternRule.toXML());
            //System.out.println("-------------------------");
            }
        }
    }
    float percent = (float) simpleCount / allRules.size() * 100;
    //System.out.printf(simpleCount + "/" + allRules.size() + " = %.0f%% for " + language + "\n", percent);
    System.out.printf("%.0f%% for " + language + "\n", percent);
}
Also used : PatternToken(org.languagetool.rules.patterns.PatternToken) PatternRule(org.languagetool.rules.patterns.PatternRule) Rule(org.languagetool.rules.Rule) PatternRule(org.languagetool.rules.patterns.PatternRule)

Example 27 with PatternRule

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

the class DashRule method loadCompoundFile.

private void loadCompoundFile(String path) throws IOException {
    try (InputStream stream = JLanguageTool.getDataBroker().getFromResourceDirAsStream(path);
        InputStreamReader reader = new InputStreamReader(stream, "utf-8");
        BufferedReader br = new BufferedReader(reader)) {
        String line;
        int counter = 0;
        while ((line = br.readLine()) != null) {
            counter++;
            if (line.isEmpty() || line.charAt(0) == '#') {
                // ignore comments
                continue;
            }
            if (line.endsWith("+")) {
                // skip non-hyphenated suggestions
                continue;
            } else if (line.endsWith("*")) {
                line = removeLastCharacter(line);
            }
            List<PatternToken> tokList = new ArrayList<PatternToken>();
            String[] tokens = line.split("-");
            int tokenCounter = 0;
            for (String token : tokens) {
                tokenCounter++;
                // token
                tokList.add(new PatternToken(token, true, false, false));
                if (tokenCounter < tokens.length) {
                    // add dash
                    tokList.add(new PatternToken("[—–]", false, true, false));
                }
            }
            PatternRule dashRule = new PatternRule("DASH_RULE" + counter, Languages.getLanguageForName("Polish"), tokList, "", "Błędne użycie myślnika zamiast myślnika. " + "Poprawnie: <suggestion>" + line.replaceAll("[–—]", "-") + "</suggestion>.", line.replaceAll("[–—]", "-"));
            dashRules.add(dashRule);
        }
    }
}
Also used : PatternToken(org.languagetool.rules.patterns.PatternToken) InputStreamReader(java.io.InputStreamReader) PatternRule(org.languagetool.rules.patterns.PatternRule) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList)

Example 28 with PatternRule

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

the class RuleWithMaxFilterTest method testNoFilteringIfNotOverlapping.

@Test
public void testNoFilteringIfNotOverlapping() {
    List<PatternToken> fakePatternTokens = new ArrayList<>();
    PatternRule rule1 = new PatternRule("id1", language, fakePatternTokens, "desc1", "msg1", "shortMsg1");
    PatternRule rule2 = new PatternRule("id1", language, fakePatternTokens, "desc2", "msg2", "shortMsg2");
    RuleMatch match1 = new RuleMatch(rule1, 10, 20, "Match1");
    RuleMatch match2 = new RuleMatch(rule2, 21, 25, "Match2");
    RuleWithMaxFilter filter = new RuleWithMaxFilter();
    List<RuleMatch> filteredMatches = filter.filter(Arrays.asList(match1, match2));
    assertEquals(2, filteredMatches.size());
}
Also used : PatternToken(org.languagetool.rules.patterns.PatternToken) PatternRule(org.languagetool.rules.patterns.PatternRule) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

PatternRule (org.languagetool.rules.patterns.PatternRule)28 PatternToken (org.languagetool.rules.patterns.PatternToken)17 Rule (org.languagetool.rules.Rule)13 ArrayList (java.util.ArrayList)12 RuleMatch (org.languagetool.rules.RuleMatch)9 Test (org.junit.Test)8 English (org.languagetool.language.English)8 JLanguageTool (org.languagetool.JLanguageTool)6 File (java.io.File)4 FileReader (java.io.FileReader)3 IncorrectExample (org.languagetool.rules.IncorrectExample)3 Ignore (org.junit.Ignore)2 Language (org.languagetool.Language)2 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1