Search in sources :

Example 6 with PatternToken

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

the class SameRuleGroupFilterTest method testNoFilteringIfDifferentRulegroups.

@Test
public void testNoFilteringIfDifferentRulegroups() {
    List<PatternToken> fakePatternTokens = new ArrayList<>();
    Rule rule1 = new PatternRule("id1", language, fakePatternTokens, "desc1", "msg1", "shortMsg1");
    Rule rule2 = new PatternRule("id2", language, fakePatternTokens, "desc2", "msg2", "shortMsg2");
    RuleMatch match1 = new RuleMatch(rule1, 10, 20, "Match1");
    RuleMatch match2 = new RuleMatch(rule2, 15, 25, "Match2");
    SameRuleGroupFilter filter = new SameRuleGroupFilter();
    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) PatternRule(org.languagetool.rules.patterns.PatternRule) Test(org.junit.Test)

Example 7 with PatternToken

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

the class SameRuleGroupFilterTest method testFilter.

@Test
public void testFilter() {
    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, 15, 25, "Match2");
    SameRuleGroupFilter filter = new SameRuleGroupFilter();
    List<RuleMatch> filteredMatches = filter.filter(Arrays.asList(match1, match2));
    assertEquals(1, filteredMatches.size());
    assertEquals("Match1", filteredMatches.get(0).getMessage());
}
Also used : PatternToken(org.languagetool.rules.patterns.PatternToken) PatternRule(org.languagetool.rules.patterns.PatternRule) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with PatternToken

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

the class RuleMatchAsXmlSerializerTest method testRuleMatchesToXMLWithCategory.

@Test
public void testRuleMatchesToXMLWithCategory() throws IOException {
    List<RuleMatch> matches = new ArrayList<>();
    String text = "This is a test sentence.";
    List<PatternToken> patternTokens = Collections.emptyList();
    Rule patternRule = new PatternRule("MY_ID", LANG, patternTokens, "my description", "my message", "short message");
    patternRule.setCategory(new Category(new CategoryId("TEST_ID"), "MyCategory"));
    RuleMatch match = new RuleMatch(patternRule, 8, 10, "myMessage");
    match.setColumn(99);
    match.setEndColumn(100);
    match.setLine(44);
    match.setEndLine(45);
    matches.add(match);
    String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, LANG, LANG);
    assertTrue(xml.contains(">\n" + "<error fromy=\"44\" fromx=\"98\" toy=\"45\" tox=\"99\" ruleId=\"MY_ID\" msg=\"myMessage\" " + "replacements=\"\" context=\"...s is a test ...\" contextoffset=\"8\" offset=\"8\" errorlength=\"2\" category=\"MyCategory\" " + "categoryid=\"TEST_ID\" locqualityissuetype=\"uncategorized\"/>\n" + "</matches>\n"));
    patternRule.setCategory(new Category(new CategoryId("CAT_ID"), "MyCategory"));
    RuleMatch match2 = new RuleMatch(patternRule, 8, 10, "myMessage");
    String xml2 = SERIALIZER.ruleMatchesToXml(Collections.singletonList(match2), text, 5, LANG, LANG);
    assertTrue(xml2.contains("category=\"MyCategory\""));
    assertTrue(xml2.contains("categoryid=\"CAT_ID\""));
}
Also used : PatternToken(org.languagetool.rules.patterns.PatternToken) PatternRule(org.languagetool.rules.patterns.PatternRule) ArrayList(java.util.ArrayList) PatternRule(org.languagetool.rules.patterns.PatternRule) Test(org.junit.Test)

Example 9 with PatternToken

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

the class JLanguageToolTest method testOverlapFilter.

@Test
public void testOverlapFilter() throws IOException {
    Category category = new Category(new CategoryId("TEST_ID"), "test category");
    List<PatternToken> elements1 = Arrays.asList(new PatternToken("one", true, false, false));
    PatternRule rule1 = new PatternRule("id1", new English(), elements1, "desc1", "msg1", "shortMsg1");
    rule1.setSubId("1");
    rule1.setCategory(category);
    List<PatternToken> elements2 = Arrays.asList(new PatternToken("one", true, false, false), new PatternToken("two", true, false, false));
    PatternRule rule2 = new PatternRule("id1", new English(), elements2, "desc2", "msg2", "shortMsg2");
    rule2.setSubId("2");
    rule2.setCategory(category);
    JLanguageTool tool = new JLanguageTool(new English());
    tool.addRule(rule1);
    tool.addRule(rule2);
    List<RuleMatch> ruleMatches1 = tool.check("And one two three.");
    assertEquals("one overlapping rule must be filtered out", 1, ruleMatches1.size());
    assertEquals("msg1", ruleMatches1.get(0).getMessage());
    String sentence = "And one two three.";
    AnalyzedSentence analyzedSentence = tool.getAnalyzedSentence(sentence);
    List<Rule> bothRules = new ArrayList<>(Arrays.asList(rule1, rule2));
    List<RuleMatch> ruleMatches2 = tool.checkAnalyzedSentence(ParagraphHandling.NORMAL, bothRules, analyzedSentence);
    assertEquals("one overlapping rule must be filtered out", 1, ruleMatches2.size());
    assertEquals("msg1", ruleMatches2.get(0).getMessage());
}
Also used : PatternRule(org.languagetool.rules.patterns.PatternRule) ArrayList(java.util.ArrayList) AmericanEnglish(org.languagetool.language.AmericanEnglish) English(org.languagetool.language.English) BritishEnglish(org.languagetool.language.BritishEnglish) PatternToken(org.languagetool.rules.patterns.PatternToken) SpellingCheckRule(org.languagetool.rules.spelling.SpellingCheckRule) PatternRule(org.languagetool.rules.patterns.PatternRule) Test(org.junit.Test)

Example 10 with PatternToken

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

the class PatternRuleQueryBuilder method buildRelaxedQuery.

/**
   * Iterate over all elements, ignore those not supported, add the other ones to a BooleanQuery.
   * @throws UnsupportedPatternRuleException if no query could be created for the rule
   */
public Query buildRelaxedQuery(AbstractPatternRule rule) throws UnsupportedPatternRuleException {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    for (PatternToken patternToken : rule.getPatternTokens()) {
        try {
            BooleanClause clause = makeQuery(patternToken);
            builder.add(clause);
        } catch (UnsupportedPatternRuleException e) {
        //System.out.println("Ignoring because it's not supported: " + element + ": " + e);
        // cannot handle - okay to ignore, as we may return too broad matches
        } catch (Exception e) {
            throw new RuntimeException("Could not create query for rule " + rule.getId(), e);
        }
    }
    BooleanQuery query = builder.build();
    if (query.clauses().size() == 0) {
        throw new UnsupportedPatternRuleException("No items found in rule that can be used to build a search query: " + rule);
    }
    return query;
}
Also used : PatternToken(org.languagetool.rules.patterns.PatternToken) IOException(java.io.IOException)

Aggregations

PatternToken (org.languagetool.rules.patterns.PatternToken)21 PatternRule (org.languagetool.rules.patterns.PatternRule)17 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 English (org.languagetool.language.English)7 Rule (org.languagetool.rules.Rule)6 RuleMatch (org.languagetool.rules.RuleMatch)5 PatternTokenBuilder (org.languagetool.rules.patterns.PatternTokenBuilder)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 AmericanEnglish (org.languagetool.language.AmericanEnglish)1 BritishEnglish (org.languagetool.language.BritishEnglish)1 SpellingCheckRule (org.languagetool.rules.spelling.SpellingCheckRule)1