Learning Salesforce Einstein
上QQ阅读APP看书,第一时间看更新

Predicted response design

The Predicted response is also of the JSON format. A simple example for our example of the result is the following JSON:

    {
"itemScores":[
{"item":22,"score":4.07},
{"item":62,"score":4.05},
{"item":75,"score":4.04},
{"item":68,"score":3.81}
]
}

The PreictedResult.java file has the class for the preceding conversion:

    package org.template.recommendation;

import java.io.Serializable;
import java.util.List;

public class PredictedResult implements Serializable{
private final List<ItemScore> itemScores;

public PredictedResult(List<ItemScore> itemScores) {
this.itemScores = itemScores;
}

public List<ItemScore> getItemScores() {
return itemScores;
}

@Override
public String toString() {
return "PredictedResult{" +
"itemScores=" + itemScores +
'}';
}
}

The Scala version of the code uses the case class, and you can define the query class and the predictedResult class as a case class:

    package MyRecommedationScala

import org.apache.predictionio.controller.IEngineFactory
import org.apache.predictionio.controller.Engine

case class Query(
user: String,
num: Int,
categories: Option[Set[String]],
whiteList: Option[Set[String]],
blackList: Option[Set[String]]
) extends Serializable

case class PredictedResult(
itemScores: Array[ItemScore]
) extends Serializable

case class ItemScore(
item: String,
score: Double
) extends Serializable

object ECommerceRecommendationEngine extends IEngineFactory {
def apply() = {
new Engine(
classOf[DataSource],
classOf[Preparator],
Map("ecomm" -> classOf[ECommAlgorithm]),
classOf[Serving])
}
}
Case classes in Scala are regular classes that are immutable by default, decomposable through pattern matching, compared by structural equality, and you can instantiate and operate on them.