public void GetUserData(IManosContext ctx) { var ratings = MyRecommender.Instance.Data; var user_mapping = MyRecommender.Instance.UserMapping; var item_mapping = MyRecommender.Instance.ItemMapping; int user_id = ctx.GetUserID("users"); if (user_id < 0 || user_id > ratings.MaxUserID) { ctx.Response.StatusCode = 404; // TODO maybe different error code ctx.Response.End(); return; } var match = new Regex(@"users/\d+/(\w+)").Match(ctx.Request.Path); string command = match.Success ? match.Groups[1].Value : string.Empty; switch(command) { case "statistics": ctx.Response.WriteLine("uid={0} num_ratings={1}", user_mapping.ToOriginalID(user_id), ratings.ByUser[user_id].Count); break; case "events": var items_string = ctx.Request.QueryData.GetString("items"); if (items_string == null) { foreach (int index in ratings.ByUser[user_id]) ctx.Response.WriteLine("eid={0} iid={1} value={2}", index, item_mapping.ToOriginalID(ratings.Items[index]), ratings[index].ToString(CultureInfo.InvariantCulture)); } else { var requested_items = new HashSet<int>(from i in items_string.Split(',') select item_mapping.ToInternalID(int.Parse(i))); foreach (int index in ratings.ByUser[user_id]) { int item_id = ratings.Items[index]; if (requested_items.Contains(item_id)) ctx.Response.WriteLine("eid={0} iid={1} value={2}", index, item_mapping.ToOriginalID(item_id), ratings[index].ToString(CultureInfo.InvariantCulture)); } } break; case "events/ratings": // TODO support timestamps goto case "events"; case "": ctx.Response.WriteLine("no user metadata available"); break; default: ctx.Response.StatusCode = 404; // TODO maybe different error break; } ctx.Response.End(); }
public void Predict(IManosContext ctx) { var recommender = MyRecommender.Instance.Predictor; var ratings = MyRecommender.Instance.Data; var item_mapping = MyRecommender.Instance.ItemMapping; int user_id = ctx.GetUserID("users", "predictions"); if (user_id < 0 || user_id > recommender.MaxUserID) { ctx.Response.StatusCode = 404; // TODO maybe different error ctx.Response.End(); return; } var query_data = ctx.Request.QueryData; // TODO do this in a more elegant way var items_string = query_data.GetString("items"); var requested_items = items_string != null ? from i in items_string.Split(',') select item_mapping.ToInternalID(int.Parse(i)) : Enumerable.Range(0, ratings.MaxItemID + 1); // TODO do this in a more elegant way var override_value = query_data.GetString("useStoredRatings"); bool show_stored_ratings = override_value != null ? override_value.Equals("true", StringComparison.InvariantCultureIgnoreCase) : false; // TODO sort results if (show_stored_ratings) { var known_items = new HashSet<int>( from index in ratings.ByUser[user_id] select ratings.Items[index]); foreach (int item_id in requested_items) if (known_items.Contains(item_id)) ctx.Response.WriteLine("iid={0} value={1}", item_mapping.ToOriginalID(item_id), ratings[user_id, item_id]); else ctx.Response.WriteLine("iid={0} value={1}", item_mapping.ToOriginalID(item_id), recommender.Predict(user_id, item_id).ToString(CultureInfo.InvariantCulture)); } else foreach (int item_id in requested_items) ctx.Response.WriteLine("iid={0} value={1}", item_mapping.ToOriginalID(item_id), recommender.Predict(user_id, item_id).ToString(CultureInfo.InvariantCulture)); ctx.Response.End(); }