Search in sources :

Example 1 with KeywordTextParser

use of org.codice.ddf.opensearch.endpoint.KeywordTextParser in project ddf by codice.

the class KeywordTextParserTest method testSpacing.

@Test
public void testSpacing() {
    // TODO add more expressions to test
    List<String> inputs = new ArrayList<>();
    inputs.add(" A B OR C");
    inputs.add(" A B OR C ");
    inputs.add(" A B  OR C ");
    inputs.add(" A       B OR C");
    inputs.add("      A B OR C");
    inputs.add("A B      OR            C           ");
    inputs.add("    A                 B   OR   C     NOT D AND           E   ");
    for (String input : inputs) {
        KeywordTextParser parser = Parboiled.createParser(KeywordTextParser.class);
        ParsingResult<?> result = new ReportingParseRunner(parser.inputPhrase()).run(input);
        assertEquals("Failed on input [" + input + "]. Parse Error [" + getErrorOutput(result) + "]", 0, result.parseErrors.size());
        assertEquals("Failed to parse [" + input + "] properly.", input, ParseTreeUtils.getNodeText(result.parseTreeRoot, result.inputBuffer));
    }
}
Also used : ArrayList(java.util.ArrayList) KeywordTextParser(org.codice.ddf.opensearch.endpoint.KeywordTextParser) ReportingParseRunner(org.parboiled.parserunners.ReportingParseRunner) Test(org.junit.Test)

Example 2 with KeywordTextParser

use of org.codice.ddf.opensearch.endpoint.KeywordTextParser in project ddf by codice.

the class KeywordTextParserTest method trace.

// We have been using this for debugging purposes, its not meant to be a test.
@Ignore
@Test
public void trace() {
    Map<String, String> inputToOutput = new LinkedHashMap<>();
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    List<String> inputs = new ArrayList<>();
    // inputs.add("A \"(test test2)\" OR test2");
    inputs.add("A B  C D");
    for (String input : inputs) {
        KeywordTextParser parser = Parboiled.createParser(KeywordTextParser.class);
        ParsingResult<ASTNode> result = new TracingParseRunner(parser.inputPhrase()).run(input);
        // ParsingResult<ASTNode> result = new
        // ReportingParseRunner(parser.inputPhrase()).run(input);
        KeywordFilterGenerator kfg = new KeywordFilterGenerator(filterBuilder);
        Filter filter = kfg.getFilterFromASTNode(result.resultValue);
        inputToOutput.put(input, filter.toString());
    // visualize(result);
    }
    for (Map.Entry<String, String> iteration : inputToOutput.entrySet()) {
        System.out.println(iteration.getKey() + " : " + iteration.getValue());
    }
}
Also used : KeywordFilterGenerator(org.codice.ddf.opensearch.endpoint.KeywordFilterGenerator) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TracingParseRunner(org.parboiled.parserunners.TracingParseRunner) Filter(org.opengis.filter.Filter) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) ASTNode(org.codice.ddf.opensearch.endpoint.ASTNode) KeywordTextParser(org.codice.ddf.opensearch.endpoint.KeywordTextParser) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with KeywordTextParser

use of org.codice.ddf.opensearch.endpoint.KeywordTextParser in project ddf by codice.

the class OpenSearchQuery method addContextualFilter.

public void addContextualFilter(String searchTerms, String selectors) throws ParsingException {
    Filter filter = null;
    KeywordFilterGenerator keywordFilterGenerator = new KeywordFilterGenerator(filterBuilder);
    KeywordTextParser parser = Parboiled.createParser(KeywordTextParser.class);
    // translate the search terms into an abstract syntax tree
    ParsingResult<ASTNode> result = new RecoveringParseRunner(parser.inputPhrase()).run(searchTerms);
    // make sure it's a good result before using it
    if (result.matched && !result.hasErrors()) {
        filter = generateContextualFilter(selectors, keywordFilterGenerator, result);
    } else if (result.hasErrors()) {
        throw new ParsingException("Unable to parse keyword search phrase. " + generateParsingError(result));
    }
    if (filter != null) {
        filters.add(filter);
    }
}
Also used : KeywordFilterGenerator(org.codice.ddf.opensearch.endpoint.KeywordFilterGenerator) PolygonSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.PolygonSpatialFilter) SpatialFilter(ddf.catalog.impl.filter.SpatialFilter) TemporalFilter(ddf.catalog.impl.filter.TemporalFilter) SpatialDistanceFilter(ddf.catalog.impl.filter.SpatialDistanceFilter) Filter(org.opengis.filter.Filter) BBoxSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.BBoxSpatialFilter) ParsingException(org.parboiled.errors.ParsingException) ASTNode(org.codice.ddf.opensearch.endpoint.ASTNode) RecoveringParseRunner(org.parboiled.parserunners.RecoveringParseRunner) KeywordTextParser(org.codice.ddf.opensearch.endpoint.KeywordTextParser)

Example 4 with KeywordTextParser

use of org.codice.ddf.opensearch.endpoint.KeywordTextParser in project ddf by codice.

the class KeywordTextParserTest method testNegatives.

@Test
public void testNegatives() {
    // TODO add more expressions to test
    List<String> inputs = new ArrayList<>();
    // these should fail even with loose parsing
    inputs.add("");
    inputs.add("()");
    inputs.add("( )");
    inputs.add("  ");
    inputs.add("( Keyword");
    inputs.add("(Keyword");
    inputs.add("Keyword)");
    inputs.add("Keyword )");
    inputs.add("product2 NOT))(((( anothertitle");
    inputs.add("(A AND B) NOT ((C OR D) AND (B NOT A) OR E");
    inputs.add("(A AND B) NOT ((C\" AND (B NOT A)) OR E");
    inputs.add("(A AND B) NOT (\"C\" AND \"B)) OR E");
    inputs.add("(A AND B) NOT (\"A \"C\"\" AND (B)) OR E");
    // this could be made valid
    inputs.add("(\"A)()()(((()))()((()))))()(((((()))((\" B) OR C");
    // if an escape character
    // were introduced
    inputs.add("() (stuff) OR C");
    // one missing leading parenthesis
    inputs.add("(((((((((((stuff))))))))))) OR C)");
    // one missing trailing parenthesis
    inputs.add("((((((((((((stuff)))))))))) OR C)");
    inputs.add("((((((((((((\"stuff (stuff2)\"))))))))))) OR C)");
    inputs.add("(\"A)()()(((()))()((()))))()(((((()))((\" B) OR C");
    inputs.add("\"\"");
    inputs.add("\"");
    for (String input : inputs) {
        KeywordTextParser parser = Parboiled.createParser(KeywordTextParser.class);
        ParsingResult<?> result = new ReportingParseRunner(parser.inputPhrase()).run(input);
        LOGGER.debug("input = {}\t\t=====>result matched = {}", input, result.matched);
        assertThat("[" + input + "] should have failed.", result.parseErrors.size(), greaterThan(0));
    }
}
Also used : ArrayList(java.util.ArrayList) KeywordTextParser(org.codice.ddf.opensearch.endpoint.KeywordTextParser) ReportingParseRunner(org.parboiled.parserunners.ReportingParseRunner) Test(org.junit.Test)

Example 5 with KeywordTextParser

use of org.codice.ddf.opensearch.endpoint.KeywordTextParser in project ddf by codice.

the class KeywordTextParserTest method testPositives.

@Test
public void testPositives() {
    // TODO add more expressions to test
    List<String> inputs = new ArrayList<>();
    inputs.add("נּ€");
    inputs.add("A AND B");
    inputs.add("A AND   B AND C");
    inputs.add("A OR B");
    inputs.add("(A OR B)  ");
    inputs.add("  (\"B\")");
    inputs.add("(  B)");
    inputs.add("(\"A OR B  \"  )");
    inputs.add("A NOT B");
    inputs.add("A B");
    inputs.add("AND");
    inputs.add("\"AND\"");
    inputs.add("test tester3");
    inputs.add("A B OR C");
    inputs.add("A \"B\" OR C");
    inputs.add("A \"test\" OR C");
    inputs.add("A B OR \"C\"");
    inputs.add("$%'^&_+` ''''veryInterest*ingKeyword!@#$%^&_+-=`~1234567890[]{}\\|?/><,.:; OR %^&_+-=*`~123");
    inputs.add("test TeSt1 OR another3test");
    inputs.add("A B C D E A N D N O T O R F G H I \"J 1 2 3 4 5 6\" 2 * 7 8 9 # $ % ^ ! } [ ` ; ' <");
    inputs.add("(\"A14356377856nyin8o789l;;l453 234l56;23$$#%#$@^#@&&!\" B) OR C");
    inputs.add("(A AND B) OR C");
    inputs.add("(A AND B) NOT ((C OR D) AND (B NOT A)) OR E");
    inputs.add("A AND B AND C AND D OR E NOT F");
    inputs.add("A B AND C D OR E NOT F");
    inputs.add("(A B AND C D) OR E NOT F");
    inputs.add("A NOT A");
    inputs.add("A (NOT C) D");
    inputs.add("(((Apple) AND (((Orange) OR Banana))))");
    inputs.add("A  B");
    inputs.add("(\"A14356377856nyin8o789l;;l453      234l56;23$$#%#$@^#@&&!\" B) OR C");
    inputs.add("( A )");
    inputs.add(" AND ");
    inputs.add("    keyword2       ");
    inputs.add(" AND");
    inputs.add("AND ");
    inputs.add(" AND ");
    inputs.add("\" AND \"");
    inputs.add("(\"Keyword \")");
    inputs.add("\" Keyword\"");
    inputs.add("( \"Keyword\")");
    inputs.add("A ( OR ) B");
    inputs.add("((((((((((((\"stuff   stuff2\"))))))))))) OR C)");
    inputs.add("(\"A14356377856nyin8o789l;;l453      234l56;23$$#%#$@^#@&&!\" B) OR C");
    for (String input : inputs) {
        KeywordTextParser parser = Parboiled.createParser(KeywordTextParser.class);
        ParsingResult<?> result = new ReportingParseRunner(parser.inputPhrase()).run(input);
        LOGGER.debug("input = {}\t\t=====>result matched = {}", input, result.matched);
        assertEquals("Failed on input [" + input + "]. Parse Error [" + getErrorOutput(result) + "]", 0, result.parseErrors.size());
        assertEquals("Failed to parse [" + input + "] properly.", input, ParseTreeUtils.getNodeText(result.parseTreeRoot, result.inputBuffer));
    }
}
Also used : ArrayList(java.util.ArrayList) KeywordTextParser(org.codice.ddf.opensearch.endpoint.KeywordTextParser) ReportingParseRunner(org.parboiled.parserunners.ReportingParseRunner) Test(org.junit.Test)

Aggregations

KeywordTextParser (org.codice.ddf.opensearch.endpoint.KeywordTextParser)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 ReportingParseRunner (org.parboiled.parserunners.ReportingParseRunner)3 ASTNode (org.codice.ddf.opensearch.endpoint.ASTNode)2 KeywordFilterGenerator (org.codice.ddf.opensearch.endpoint.KeywordFilterGenerator)2 Filter (org.opengis.filter.Filter)2 FilterBuilder (ddf.catalog.filter.FilterBuilder)1 GeotoolsFilterBuilder (ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder)1 SpatialDistanceFilter (ddf.catalog.impl.filter.SpatialDistanceFilter)1 SpatialFilter (ddf.catalog.impl.filter.SpatialFilter)1 TemporalFilter (ddf.catalog.impl.filter.TemporalFilter)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 BBoxSpatialFilter (org.codice.ddf.opensearch.endpoint.query.filter.BBoxSpatialFilter)1 PolygonSpatialFilter (org.codice.ddf.opensearch.endpoint.query.filter.PolygonSpatialFilter)1 Ignore (org.junit.Ignore)1 ParsingException (org.parboiled.errors.ParsingException)1 RecoveringParseRunner (org.parboiled.parserunners.RecoveringParseRunner)1 TracingParseRunner (org.parboiled.parserunners.TracingParseRunner)1