1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.types;
import com.yahoo.language.Language;
import com.yahoo.language.process.Encoder;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.search.query.profile.compiled.CompiledQueryProfileRegistry;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
/**
* A tensor field type in a query profile
*
* @author bratseth
*/
public class TensorFieldType extends FieldType {
private final TensorType type;
/** Creates a tensor field type with information about the kind of tensor this will hold */
public TensorFieldType(TensorType type) {
this.type = type;
}
/** Returns information about the type of tensor this will hold */
@Override
public TensorType asTensorType() { return type; }
@Override
public Class getValueClass() { return Tensor.class; }
@Override
public String stringValue() { return type.toString(); }
@Override
public String toString() { return "field type " + stringValue(); }
@Override
public String toInstanceDescription() { return "a tensor"; }
@Override
public Object convertFrom(Object o, QueryProfileRegistry registry) {
return convertFrom(o, ConversionContext.empty());
}
@Override
public Object convertFrom(Object o, ConversionContext context) {
return convertFrom(o, context.getEncoder(), context.getLanguage());
}
private Object convertFrom(Object o, Encoder encoder, Language language) {
if (o instanceof Tensor) return o;
if (o instanceof String && ((String)o).startsWith("encode(")) return encode((String)o, encoder, language);
if (o instanceof String) return Tensor.from(type, (String)o);
return null;
}
private Tensor encode(String s, Encoder encoder, Language language) {
if ( ! s.endsWith(")"))
throw new IllegalArgumentException("Expected any string enclosed in encode(), but the argument does not end by ')'");
String text = s.substring("encode(".length(), s.length() - 1);
return encoder.encode(text, language, type);
}
public static TensorFieldType fromTypeString(String s) {
return new TensorFieldType(TensorType.fromSpec(s));
}
}
|