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));
}
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);
}
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));
}
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);
}
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());
}
Aggregations