Search in sources :

Example 1 with WordAlternativesItem

use of com.yahoo.prelude.query.WordAlternativesItem in project vespa by vespa-engine.

the class YqlParser method instantiateWordAlternativesItem.

private Item instantiateWordAlternativesItem(String field, OperatorNode<ExpressionOperator> ast) {
    List<OperatorNode<ExpressionOperator>> args = ast.getArgument(1);
    Preconditions.checkArgument(args.size() >= 1, "Expected 1 or more arguments, got %s.", args.size());
    Preconditions.checkArgument(args.get(0).getOperator() == ExpressionOperator.MAP, "Expected MAP, got %s.", args.get(0).getOperator());
    List<WordAlternativesItem.Alternative> terms = new ArrayList<>();
    List<String> keys = args.get(0).getArgument(0);
    List<OperatorNode<ExpressionOperator>> values = args.get(0).getArgument(1);
    for (int i = 0; i < keys.size(); ++i) {
        OperatorNode<ExpressionOperator> value = values.get(i);
        if (value.getOperator() != ExpressionOperator.LITERAL)
            throw newUnexpectedArgumentException(value.getOperator(), ExpressionOperator.LITERAL);
        String term = keys.get(i);
        double exactness = value.getArgument(0, Double.class);
        terms.add(new WordAlternativesItem.Alternative(term, exactness));
    }
    Substring origin = getOrigin(ast);
    Boolean isFromQuery = getAnnotation(ast, IMPLICIT_TRANSFORMS, Boolean.class, Boolean.TRUE, IMPLICIT_TRANSFORMS_DESCRIPTION);
    return leafStyleSettings(ast, new WordAlternativesItem(field, isFromQuery, origin, terms));
}
Also used : Substring(com.yahoo.prelude.query.Substring) ArrayList(java.util.ArrayList) WordAlternativesItem(com.yahoo.prelude.query.WordAlternativesItem)

Example 2 with WordAlternativesItem

use of com.yahoo.prelude.query.WordAlternativesItem in project vespa by vespa-engine.

the class LowercasingTestCase method testLowercasingWordAlternatives.

@Test
public void testLowercasingWordAlternatives() {
    execution = new Execution(new Chain<Searcher>(new VespaLowercasingSearcher(new LowercasingConfig(new LowercasingConfig.Builder().transform_weighted_sets(false)))), Execution.Context.createContextStub(settings));
    Query q = new Query();
    WordAlternativesItem root;
    List<WordAlternativesItem.Alternative> terms = new ArrayList<>();
    terms.add(new Alternative("ABC", 1.0));
    terms.add(new Alternative("def", 1.0));
    root = new WordAlternativesItem(BAMSE, true, null, terms);
    q.getModel().getQueryTree().setRoot(root);
    Result r = execution.search(q);
    root = (WordAlternativesItem) r.getQuery().getModel().getQueryTree().getRoot();
    assertEquals(3, root.getAlternatives().size());
    assertEquals("ABC", root.getAlternatives().get(0).word);
    assertEquals(1.0d, root.getAlternatives().get(0).exactness, 1e-15d);
    assertEquals("abc", root.getAlternatives().get(1).word);
    assertEquals(.7d, root.getAlternatives().get(1).exactness, 1e-15d);
    assertEquals("def", root.getAlternatives().get(2).word);
    assertEquals(1.0d, root.getAlternatives().get(2).exactness, 1e-15d);
}
Also used : Chain(com.yahoo.component.chain.Chain) Execution(com.yahoo.search.searchchain.Execution) Alternative(com.yahoo.prelude.query.WordAlternativesItem.Alternative) Query(com.yahoo.search.Query) ArrayList(java.util.ArrayList) WordAlternativesItem(com.yahoo.prelude.query.WordAlternativesItem) Result(com.yahoo.search.Result) Test(org.junit.Test)

Example 3 with WordAlternativesItem

use of com.yahoo.prelude.query.WordAlternativesItem in project vespa by vespa-engine.

the class YqlParserTestCase method testWordAlternativesInPhrase.

@Test
public void testWordAlternativesInPhrase() {
    QueryTree x = parse("select * from sources * where" + " foo contains phrase(\"forest\", alternatives({\"trees\": 1.0, \"tree\": 0.7}));");
    Item root = x.getRoot();
    assertSame(PhraseItem.class, root.getClass());
    PhraseItem phrase = (PhraseItem) root;
    assertEquals(2, phrase.getItemCount());
    assertEquals("forest", ((WordItem) phrase.getItem(0)).getWord());
    checkWordAlternativesContent((WordAlternativesItem) phrase.getItem(1));
}
Also used : WordAlternativesItem(com.yahoo.prelude.query.WordAlternativesItem) ExactStringItem(com.yahoo.prelude.query.ExactStringItem) WeakAndItem(com.yahoo.prelude.query.WeakAndItem) IndexedItem(com.yahoo.prelude.query.IndexedItem) PrefixItem(com.yahoo.prelude.query.PrefixItem) Item(com.yahoo.prelude.query.Item) SuffixItem(com.yahoo.prelude.query.SuffixItem) PhraseItem(com.yahoo.prelude.query.PhraseItem) RegExpItem(com.yahoo.prelude.query.RegExpItem) SubstringItem(com.yahoo.prelude.query.SubstringItem) AndItem(com.yahoo.prelude.query.AndItem) WordItem(com.yahoo.prelude.query.WordItem) QueryTree(com.yahoo.search.query.QueryTree) PhraseItem(com.yahoo.prelude.query.PhraseItem) Test(org.junit.Test)

Example 4 with WordAlternativesItem

use of com.yahoo.prelude.query.WordAlternativesItem in project vespa by vespa-engine.

the class YqlParserTestCase method testWordAlternatives.

@Test
public void testWordAlternatives() {
    QueryTree x = parse("select * from sources * where foo contains alternatives({\"trees\": 1.0, \"tree\": 0.7});");
    Item root = x.getRoot();
    assertSame(WordAlternativesItem.class, root.getClass());
    WordAlternativesItem alternatives = (WordAlternativesItem) root;
    checkWordAlternativesContent(alternatives);
}
Also used : WordAlternativesItem(com.yahoo.prelude.query.WordAlternativesItem) ExactStringItem(com.yahoo.prelude.query.ExactStringItem) WeakAndItem(com.yahoo.prelude.query.WeakAndItem) IndexedItem(com.yahoo.prelude.query.IndexedItem) PrefixItem(com.yahoo.prelude.query.PrefixItem) Item(com.yahoo.prelude.query.Item) SuffixItem(com.yahoo.prelude.query.SuffixItem) PhraseItem(com.yahoo.prelude.query.PhraseItem) RegExpItem(com.yahoo.prelude.query.RegExpItem) SubstringItem(com.yahoo.prelude.query.SubstringItem) AndItem(com.yahoo.prelude.query.AndItem) WordItem(com.yahoo.prelude.query.WordItem) QueryTree(com.yahoo.search.query.QueryTree) WordAlternativesItem(com.yahoo.prelude.query.WordAlternativesItem) Test(org.junit.Test)

Example 5 with WordAlternativesItem

use of com.yahoo.prelude.query.WordAlternativesItem in project vespa by vespa-engine.

the class YqlParserTestCase method testWordAlternativesWithOrigin.

@Test
public void testWordAlternativesWithOrigin() {
    QueryTree x = parse("select * from sources * where foo contains" + " ([{\"origin\": {\"original\": \" trees \", \"offset\": 1, \"length\": 5}}]" + "alternatives({\"trees\": 1.0, \"tree\": 0.7}));");
    Item root = x.getRoot();
    assertSame(WordAlternativesItem.class, root.getClass());
    WordAlternativesItem alternatives = (WordAlternativesItem) root;
    checkWordAlternativesContent(alternatives);
    Substring origin = alternatives.getOrigin();
    assertEquals(1, origin.start);
    assertEquals(6, origin.end);
    assertEquals("trees", origin.getValue());
    assertEquals(" trees ", origin.getSuperstring());
}
Also used : Substring(com.yahoo.prelude.query.Substring) WordAlternativesItem(com.yahoo.prelude.query.WordAlternativesItem) ExactStringItem(com.yahoo.prelude.query.ExactStringItem) WeakAndItem(com.yahoo.prelude.query.WeakAndItem) IndexedItem(com.yahoo.prelude.query.IndexedItem) PrefixItem(com.yahoo.prelude.query.PrefixItem) Item(com.yahoo.prelude.query.Item) SuffixItem(com.yahoo.prelude.query.SuffixItem) PhraseItem(com.yahoo.prelude.query.PhraseItem) RegExpItem(com.yahoo.prelude.query.RegExpItem) SubstringItem(com.yahoo.prelude.query.SubstringItem) AndItem(com.yahoo.prelude.query.AndItem) WordItem(com.yahoo.prelude.query.WordItem) QueryTree(com.yahoo.search.query.QueryTree) WordAlternativesItem(com.yahoo.prelude.query.WordAlternativesItem) Test(org.junit.Test)

Aggregations

WordAlternativesItem (com.yahoo.prelude.query.WordAlternativesItem)6 Test (org.junit.Test)5 AndItem (com.yahoo.prelude.query.AndItem)3 ExactStringItem (com.yahoo.prelude.query.ExactStringItem)3 IndexedItem (com.yahoo.prelude.query.IndexedItem)3 Item (com.yahoo.prelude.query.Item)3 PhraseItem (com.yahoo.prelude.query.PhraseItem)3 PrefixItem (com.yahoo.prelude.query.PrefixItem)3 RegExpItem (com.yahoo.prelude.query.RegExpItem)3 Substring (com.yahoo.prelude.query.Substring)3 SubstringItem (com.yahoo.prelude.query.SubstringItem)3 SuffixItem (com.yahoo.prelude.query.SuffixItem)3 WeakAndItem (com.yahoo.prelude.query.WeakAndItem)3 WordItem (com.yahoo.prelude.query.WordItem)3 QueryTree (com.yahoo.search.query.QueryTree)3 ArrayList (java.util.ArrayList)3 Query (com.yahoo.search.Query)2 Chain (com.yahoo.component.chain.Chain)1 Alternative (com.yahoo.prelude.query.WordAlternativesItem.Alternative)1 Result (com.yahoo.search.Result)1