Skip to content

Commit 9b9f151

Browse files
authored
Add TermVector support for FeatureValueWrapper (#1176)
1 parent 55fd2a5 commit 9b9f151

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

feathr-impl/src/main/java/com/linkedin/feathr/common/util/CoercionUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.linkedin.feathr.common.FeatureTypes;
44
import com.linkedin.feathr.common.FeatureValue;
5+
import com.linkedin.feathr.offline.mvel.plugins.FeatureValueWrapper;
6+
57
import java.util.Collection;
68
import java.util.Collections;
79
import java.util.List;
@@ -178,7 +180,14 @@ public static Map<String, Float> coerceToVector(Object item) {
178180
if (item instanceof FeatureValue) {
179181
// if input is already a FeatureValue, then just return its term vector representation
180182
return ((FeatureValue) item).getAsTermVector();
181-
} else if (item instanceof Collection) {
183+
} else if (item instanceof FeatureValueWrapper) {
184+
Object fv = ((FeatureValueWrapper) item).getFeatureValue();
185+
if (fv instanceof FeatureValue) {
186+
return ((FeatureValue) item).getAsTermVector();
187+
} else {
188+
throw new RuntimeException("Input FeatureValueWrapper " + item + " cannot be converted to feature value.");
189+
}
190+
}else if (item instanceof Collection) {
182191
Collection<?> collection = (Collection<?>) item;
183192
if (collection.isEmpty()) {
184193
return Collections.emptyMap();

feathr-impl/src/test/scala/com/linkedin/feathr/offline/TestFeathrUdfPlugins.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.linkedin.feathr.offline
22

33
import com.linkedin.feathr.common.FeatureValue
4+
import com.linkedin.feathr.common.util.CoercionUtils
45
import com.linkedin.feathr.offline.anchored.keyExtractor.AlienSourceKeyExtractorAdaptor
56
import com.linkedin.feathr.offline.client.plugins.FeathrUdfPluginContext
67
import com.linkedin.feathr.offline.derived.AlienDerivationFunctionAdaptor
@@ -12,6 +13,8 @@ import org.apache.spark.sql.Row
1213
import org.apache.spark.sql.types.{FloatType, StringType, StructField, StructType}
1314
import org.testng.Assert.{assertEquals, assertTrue}
1415
import org.testng.annotations.{BeforeClass, Test}
16+
import scala.collection.JavaConverters._
17+
1518
class TestFeathrUdfPlugins extends FeathrIntegTest {
1619

1720
val MULTILINE_QUOTE = "\"\"\""
@@ -32,6 +35,7 @@ class TestFeathrUdfPlugins extends FeathrIntegTest {
3235
val featureFeatureValueAsAlien = new FeathrFeatureValueAsAlien(featureValue)
3336
assertTrue(mvelContext.canConvert(FeatureValue.getClass, featureFeatureValueAsAlien.getClass))
3437
assertEquals(mvelContext.convert(featureFeatureValueAsAlien, FeatureValue.getClass), featureValue)
38+
assertEquals(CoercionUtils.coerceToVector(featureValue), Map("" -> 2.0f).asJava)
3539
}
3640

3741
@Test (enabled = true)

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
version=1.0.4-rc7
1+
version=1.0.4-rc8
22
SONATYPE_AUTOMATIC_RELEASE=true
33
POM_ARTIFACT_ID=feathr_2.12

0 commit comments

Comments
 (0)