Search in sources :

Example 1 with TextLevelRule

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

the class CommandLineTools method profileRulesOnText.

/**
   * Simple rule profiler - used to run LT on a corpus to see which
   * rule takes most time. Prints results to System.out.
   *
   * @param contents text to check
   * @param lt instance of LanguageTool
   */
public static void profileRulesOnText(String contents, JLanguageTool lt) throws IOException {
    long[] workTime = new long[10];
    List<Rule> rules = lt.getAllActiveRules();
    int ruleCount = rules.size();
    System.out.printf("Testing %d rules%n", ruleCount);
    System.out.println("Rule ID\tTime\tSentences\tMatches\tSentences per sec.");
    List<String> sentences = lt.sentenceTokenize(contents);
    for (Rule rule : rules) {
        if (rule instanceof TextLevelRule) {
            // profile rules for sentences only
            continue;
        }
        int matchCount = 0;
        for (int k = 0; k < 10; k++) {
            long startTime = System.currentTimeMillis();
            for (String sentence : sentences) {
                matchCount += rule.match(lt.getAnalyzedSentence(sentence)).length;
            }
            long endTime = System.currentTimeMillis();
            workTime[k] = endTime - startTime;
        }
        long time = median(workTime);
        float timeInSeconds = time / 1000.0f;
        float sentencesPerSecond = sentences.size() / timeInSeconds;
        System.out.printf(Locale.ENGLISH, "%s\t%d\t%d\t%d\t%.1f", rule.getId(), time, sentences.size(), matchCount, sentencesPerSecond);
        System.out.println();
    }
}
Also used : BitextRule(org.languagetool.rules.bitext.BitextRule) TextLevelRule(org.languagetool.rules.TextLevelRule) Rule(org.languagetool.rules.Rule) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule) TextLevelRule(org.languagetool.rules.TextLevelRule)

Example 2 with TextLevelRule

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

the class WordCoherencyRuleTest method testRules.

@Test
public void testRules() throws IOException {
    JLanguageTool langTool = new JLanguageTool(new Persian());
    TextLevelRule rule = new WordCoherencyRule(TestTools.getMessages("fa"));
    assertThat(rule.match(Collections.singletonList(langTool.getAnalyzedSentence("این یک اتاق است."))).length, is(0));
    assertThat(rule.match(Collections.singletonList(langTool.getAnalyzedSentence("این یک اتاق است. این یک اطاق است."))).length, is(1));
}
Also used : JLanguageTool(org.languagetool.JLanguageTool) Persian(org.languagetool.language.Persian) TextLevelRule(org.languagetool.rules.TextLevelRule) Test(org.junit.Test) PatternRuleTest(org.languagetool.rules.patterns.PatternRuleTest)

Aggregations

TextLevelRule (org.languagetool.rules.TextLevelRule)2 Test (org.junit.Test)1 JLanguageTool (org.languagetool.JLanguageTool)1 Persian (org.languagetool.language.Persian)1 Rule (org.languagetool.rules.Rule)1 BitextRule (org.languagetool.rules.bitext.BitextRule)1 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)1 PatternRuleTest (org.languagetool.rules.patterns.PatternRuleTest)1