Search in sources :

Example 1 with PatternRuleLoader

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

the class PatternRuleQueryBuilderTest method testCaseSensitive.

public void testCaseSensitive() throws Exception {
    InputStream input = new ByteArrayInputStream(("<?xml version='1.0' encoding='UTF-8'?> <rules lang='en'> <category name='Test'>" + "<rule id='TEST_RULE_1' name='test_1'> <pattern case_sensitive='yes'><token>How</token></pattern> </rule>" + "<rule id='TEST_RULE_2' name='test_2'> <pattern case_sensitive='yes'><token>how</token>" + "</pattern> </rule>" + "<rule id='TEST_RULE_3' name='test_3'> <pattern><token>How</token></pattern> </rule>" + "<rule id='TEST_RULE_4' name='test_4'> <pattern><token>how</token></pattern> </rule>" + "</category> </rules>").getBytes());
    PatternRuleLoader ruleLoader = new PatternRuleLoader();
    List<AbstractPatternRule> rules = ruleLoader.getRules(input, "test.xml");
    PatternRuleQueryBuilder patternRuleQueryBuilder = new PatternRuleQueryBuilder(language, searcher);
    Query query1 = patternRuleQueryBuilder.buildRelaxedQuery(rules.get(0));
    assertEquals(1, searcher.search(query1, 1000).totalHits);
    Query query2 = patternRuleQueryBuilder.buildRelaxedQuery(rules.get(1));
    assertEquals(0, searcher.search(query2, 1000).totalHits);
    Query query3 = patternRuleQueryBuilder.buildRelaxedQuery(rules.get(2));
    assertEquals(1, searcher.search(query3, 1000).totalHits);
    Query query4 = patternRuleQueryBuilder.buildRelaxedQuery(rules.get(3));
    assertEquals(1, searcher.search(query4, 1000).totalHits);
}
Also used : Query(org.apache.lucene.search.Query) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PatternRuleLoader(org.languagetool.rules.patterns.PatternRuleLoader) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Example 2 with PatternRuleLoader

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

the class PatternRuleQueryBuilderTest method makeRule.

private AbstractPatternRule makeRule(String ruleXml, boolean caseSensitive) throws IOException {
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version='1.0' encoding='UTF-8'?>");
    sb.append("<rules lang='en'> <category name='Test'> <rule id='TEST_RULE' name='test'>");
    if (caseSensitive) {
        sb.append("<pattern case_sensitive='yes'>");
    } else {
        sb.append("<pattern>");
    }
    sb.append(ruleXml);
    sb.append("</pattern> </rule> </category> </rules>");
    InputStream input = new ByteArrayInputStream(sb.toString().getBytes());
    PatternRuleLoader ruleLoader = new PatternRuleLoader();
    List<AbstractPatternRule> rules = ruleLoader.getRules(input, "test.xml");
    assertEquals(1, rules.size());
    return rules.get(0);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PatternRuleLoader(org.languagetool.rules.patterns.PatternRuleLoader) AbstractPatternRule(org.languagetool.rules.patterns.AbstractPatternRule)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 AbstractPatternRule (org.languagetool.rules.patterns.AbstractPatternRule)2 PatternRuleLoader (org.languagetool.rules.patterns.PatternRuleLoader)2 Query (org.apache.lucene.search.Query)1