This example engine is based on Similar Product Tempplate version v0.1.1 and is modified to support filter recommendation by the item property ‘year’.
For example, recommend movies after year 1990.
Please refer to http://docs.prediction.io/templates/similarproduct/quickstart/
The movie lens 100k data which is in below format:
UserID::MovieID::Rating::Timestamp
Import movielens data using below repository https://github.com/k4hoo/systest/tree/master/0.8/ml100k/demo-movielens
$ python -m batch_import <Access Key> http://127.0.0.1:7070
case class Query( items: List[String], num: Int, categories: Option[Set[String]], whiteList: Option[Set[String]], blackList: Option[Set[String]], recommendFromYear: Option[Int] ) extends Serializable
case class ItemScore( item: String, score: Double, year: Int ) extends Serializable
case class Item(categories: Option[List[String]],year: Int)
Item(categories = properties.getOpt[List[String]]("categories"),year = properties.get[Int]("year"))
isCandidateItem( i = i, items = model.items, categories = query.categories, queryList = queryList, whiteList = whiteList, blackList = blackList, recommendFromYear = query.recommendFromYear )
private def isCandidateItem( i: Int, items: Map[Int, Item], categories: Option[Set[String]], queryList: Set[Int], whiteList: Option[Set[Int]], blackList: Option[Set[Int]], recommendFromYear: Option[Int] ): Boolean = { whiteList.map(_.contains(i)).getOrElse(true) && blackList.map(!_.contains(i)).getOrElse(true) && // discard items in query as well (!queryList.contains(i)) && // filter categories items(i).year > recommendFromYear.getOrElse(1) && categories.map { cat => items(i).categories.map { itemCat => // keep this item if has ovelap categories with the query !(itemCat.toSet.intersect(cat).isEmpty) }.getOrElse(false) // discard this item if it has no categories }.getOrElse(true) }
val itemScores = topScores.map { case (i, s) => new ItemScore( item = model.itemIntStringMap(i), score = s, year = model.items(i).year ) } new PredictedResult(itemScores)
curl -H "Content-Type: application/json" \ -d '{ "items": ["171"], "num": 10, "recommendFromYear":1990 }' \ http://localhost:8000/queries.json