use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.
the class ConstantTensorTransformer method transformConstantReference.
private ExpressionNode transformConstantReference(ReferenceNode node, RankProfileTransformContext context) {
Value value = context.constants().get(node.getName());
if (value == null || value.type().rank() == 0) {
return node;
}
TensorValue tensorValue = (TensorValue) value;
String featureName = CONSTANT + "(" + node.getName() + ")";
String tensorType = tensorValue.asTensor().type().toString();
context.rankPropertiesOutput().put(featureName + ".value", tensorValue.toString());
context.rankPropertiesOutput().put(featureName + ".type", tensorType);
// TODO: This allows us to reference constant "a" as "a" instead of "constant(a)", but we shouldn't allow that
return new ReferenceNode(CONSTANT, Arrays.asList(new NameNode(node.getName())), null);
}
use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.
the class ConstantDereferencerTestCase method testConstantDereferencer.
@Test
public void testConstantDereferencer() throws ParseException {
ConstantDereferencer c = new ConstantDereferencer();
Map<String, Value> constants = new HashMap<>();
constants.put("a", Value.parse("1.0"));
constants.put("b", Value.parse("2"));
constants.put("c", Value.parse("3.5"));
TransformContext context = new TransformContext(constants);
assertEquals("1.0 + 2.0 + 3.5", c.transform(new RankingExpression("a + b + c"), context).toString());
assertEquals("myMacro(1.0,2.0)", c.transform(new RankingExpression("myMacro(a, b)"), context).toString());
}
use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.
the class TensorFlowImporter method importConstant.
private static Optional<TensorFunction> importConstant(TensorFlowModel model, TensorFlowOperation operation, SavedModelBundle bundle) {
String name = operation.vespaName();
if (model.largeConstants().containsKey(name) || model.smallConstants().containsKey(name)) {
return operation.function();
}
Tensor tensor;
if (operation.getConstantValue().isPresent()) {
Value value = operation.getConstantValue().get();
if (!(value instanceof TensorValue)) {
// scalar values are inserted directly into the expression
return operation.function();
}
tensor = value.asTensor();
} else {
// Here we use the type from the operation, which will have correct dimension names after name resolving
tensor = TensorConverter.toVespaTensor(readVariable(operation.node().getName(), bundle), operation.type().get());
operation.setConstantValue(new TensorValue(tensor));
}
if (tensor.type().rank() == 0 || tensor.size() <= 1) {
model.smallConstant(name, tensor);
} else {
model.largeConstant(name, tensor);
}
return operation.function();
}
use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.
the class FieldTermMatch method compute.
/**
* Computes the fieldTermMatch features:
* <ul>
* <li><code>firstPosition</code> - the position of the first occurrence of this query term in this index field</li>
* <li><code>occurrences</code> - the position of the first occurrence of this query term in this index field</li>
* </ul>
* @param queryTerm the term to return these features for
* @param field the field value to compute over, assumed to be a space-separated string of tokens
* @return a features object containing the two values described above
*/
public static Features compute(String queryTerm, String field) {
Map<String, Value> features = new HashMap<>();
String[] tokens = field.split(" ");
int occurrences = 0;
int firstPosition = 1000000;
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].equals(queryTerm)) {
if (occurrences == 0)
firstPosition = i;
occurrences++;
}
}
features.put("firstPosition", new DoubleValue(firstPosition));
features.put("occurrences", new DoubleValue(occurrences));
return new Features(features);
}
use of com.yahoo.searchlib.rankingexpression.evaluation.Value in project vespa by vespa-engine.
the class RankingExpressionWithTensorFlowTestCase method assertSmallConstant.
private void assertSmallConstant(String name, TensorType type, RankProfileSearchFixture search) {
Value value = search.rankProfile("my_profile").getConstants().get(name);
assertNotNull(value);
assertEquals(type, value.type());
}
Aggregations