Search in sources :

Example 6 with ChunkTag

use of org.languagetool.chunking.ChunkTag in project languagetool by languagetool-org.

the class PatternRuleLoaderTest method testGetRules.

@Test
public void testGetRules() throws Exception {
    PatternRuleLoader prg = new PatternRuleLoader();
    String name = "/xx/grammar.xml";
    List<AbstractPatternRule> rules = prg.getRules(JLanguageTool.getDataBroker().getFromRulesDirAsStream(name), name);
    assertTrue(rules.size() >= 30);
    Rule demoRule1 = getRuleById("DEMO_RULE", rules);
    assertEquals("http://fake-server.org/foo-bar-error-explained", demoRule1.getUrl().toString());
    assertEquals("[This is <marker>fuu bah</marker>.]", demoRule1.getCorrectExamples().toString());
    List<IncorrectExample> incorrectExamples = demoRule1.getIncorrectExamples();
    assertEquals(1, incorrectExamples.size());
    assertEquals("This is <marker>foo bar</marker>.", incorrectExamples.get(0).getExample());
    Rule demoRule2 = getRuleById("API_OUTPUT_TEST_RULE", rules);
    assertNull(demoRule2.getUrl());
    assertEquals(ITSIssueType.Uncategorized, demoRule1.getLocQualityIssueType());
    assertEquals("tag inheritance failed", ITSIssueType.Addition, getRuleById("TEST_GO", rules).getLocQualityIssueType());
    assertEquals("tag inheritance overwrite failed", ITSIssueType.Uncategorized, getRuleById("TEST_PHRASES1", rules).getLocQualityIssueType());
    assertEquals("tag inheritance overwrite failed", ITSIssueType.Characters, getRuleById("test_include", rules).getLocQualityIssueType());
    List<Rule> groupRules1 = getRulesById("test_spacebefore", rules);
    assertEquals("tag inheritance form category failed", ITSIssueType.Addition, groupRules1.get(0).getLocQualityIssueType());
    assertEquals("tag inheritance overwrite failed", ITSIssueType.Duplication, groupRules1.get(1).getLocQualityIssueType());
    List<Rule> groupRules2 = getRulesById("test_unification_with_negation", rules);
    assertEquals("tag inheritance from rulegroup failed", ITSIssueType.Grammar, groupRules2.get(0).getLocQualityIssueType());
    Set<String> categories = getCategoryNames(rules);
    assertEquals(4, categories.size());
    assertTrue(categories.contains("misc"));
    assertTrue(categories.contains("otherCategory"));
    assertTrue(categories.contains("Test tokens with min and max attributes"));
    assertTrue(categories.contains("A category that's off by default"));
    PatternRule demoRuleWithChunk = (PatternRule) getRuleById("DEMO_CHUNK_RULE", rules);
    List<PatternToken> patternTokens = demoRuleWithChunk.getPatternTokens();
    assertEquals(2, patternTokens.size());
    assertEquals(null, patternTokens.get(1).getPOStag());
    assertEquals(new ChunkTag("B-NP-singular"), patternTokens.get(1).getChunkTag());
    List<Rule> orRules = getRulesById("GROUP_WITH_URL", rules);
    assertEquals(3, orRules.size());
    assertEquals("http://fake-server.org/rule-group-url", orRules.get(0).getUrl().toString());
    assertEquals("http://fake-server.org/rule-group-url-overwrite", orRules.get(1).getUrl().toString());
    assertEquals("http://fake-server.org/rule-group-url", orRules.get(2).getUrl().toString());
    assertEquals("short message on rule group", ((PatternRule) orRules.get(0)).getShortMessage());
    assertEquals("overwriting short message", ((PatternRule) orRules.get(1)).getShortMessage());
    assertEquals("short message on rule group", ((PatternRule) orRules.get(2)).getShortMessage());
    // make sure URLs don't leak to the next rule:
    List<Rule> orRules2 = getRulesById("OR_GROUPS", rules);
    for (Rule rule : orRules2) {
        assertNull("http://fake-server.org/rule-group-url", rule.getUrl());
    }
    Rule nextRule = getRuleById("DEMO_CHUNK_RULE", rules);
    assertNull("http://fake-server.org/rule-group-url", nextRule.getUrl());
}
Also used : ChunkTag(org.languagetool.chunking.ChunkTag) Rule(org.languagetool.rules.Rule) IncorrectExample(org.languagetool.rules.IncorrectExample) Test(org.junit.Test)

Example 7 with ChunkTag

use of org.languagetool.chunking.ChunkTag in project languagetool by languagetool-org.

the class AbstractPatternRulePerformerTest method testTestAllReadingsWithChunks.

@Test
public void testTestAllReadingsWithChunks() throws Exception {
    PatternToken chunkPatternToken = new PatternToken(null, false, false, false);
    chunkPatternToken.setChunkTag(new ChunkTag("myChunk"));
    PatternRule simpleRule = new PatternRule("FAKE", new Demo(), Collections.singletonList(chunkPatternToken), "descr", "message", "short");
    PatternTokenMatcher elemMatcher = new PatternTokenMatcher(chunkPatternToken);
    AbstractPatternRulePerformer p = new MockAbstractPatternRulePerformer(simpleRule, new Unifier(null, null));
    assertFalse(p.testAllReadings(tokenReadings("bar", null), elemMatcher, null, 0, 0, 0));
    assertTrue(p.testAllReadings(tokenReadings("bar", "myChunk"), elemMatcher, null, 0, 0, 0));
    assertFalse(p.testAllReadings(tokenReadings("bar", "otherChunk"), elemMatcher, null, 0, 0, 0));
}
Also used : ChunkTag(org.languagetool.chunking.ChunkTag) Demo(org.languagetool.language.Demo) Test(org.junit.Test)

Example 8 with ChunkTag

use of org.languagetool.chunking.ChunkTag in project languagetool by languagetool-org.

the class SubjectVerbAgreementRule method prevChunkIsNominative.

boolean prevChunkIsNominative(AnalyzedTokenReadings[] tokens, int startPos) {
    for (int i = startPos; i > 0; i--) {
        AnalyzedTokenReadings token = tokens[i];
        List<ChunkTag> chunkTags = token.getChunkTags();
        if (chunkTags.contains(NPS) || chunkTags.contains(NPP)) {
            if (token.hasPartialPosTag("NOM")) {
                return true;
            }
        } else {
            return false;
        }
    }
    return false;
}
Also used : ChunkTag(org.languagetool.chunking.ChunkTag) AnalyzedTokenReadings(org.languagetool.AnalyzedTokenReadings)

Aggregations

ChunkTag (org.languagetool.chunking.ChunkTag)8 AnalyzedTokenReadings (org.languagetool.AnalyzedTokenReadings)5 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 Test (org.junit.Test)2 AnalyzedToken (org.languagetool.AnalyzedToken)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 DictionaryLookup (morfologik.stemming.DictionaryLookup)1 IStemmer (morfologik.stemming.IStemmer)1 Demo (org.languagetool.language.Demo)1 IncorrectExample (org.languagetool.rules.IncorrectExample)1 Rule (org.languagetool.rules.Rule)1