public static List <Tuple <Recipe, double> > GetTopTwelveRecipes(IngredientList userPreferences, IEnumerable <Recipe> recipes) { List <Tuple <Recipe, double> > topTwelve = new List <Tuple <Recipe, double> >(13); double twelfthMin = 99999; int size = 0; foreach (Recipe recipe in recipes) { IngredientList recipeIngr = new IngredientList(recipe); double score = MinSquaredDist(userPreferences, 1.5, recipeIngr, 1); if (score < twelfthMin) { topTwelve = insertIntoRecipeList(new Tuple <Recipe, double>(recipe, score), topTwelve, size); size++; if (size > 12) { twelfthMin = topTwelve.ElementAt(11).Item2; topTwelve.RemoveAt(12); size--; } else if (size == 12) { twelfthMin = topTwelve.ElementAt(11).Item2; } } } return(topTwelve); }
public bool containsAll(IngredientList other) { foreach (String key in other.Keys) { if (other[key].Equals(1) && !this[key].Equals(1)) { return(false); } } return(true); }
public static double MinSquaredDist(IngredientList listA, IngredientList listB) { double sum = 0; IEnumerable <String> keys = getTempVectorSpace(listA, listB); foreach (String key in keys) { sum += Math.Pow(((int)listA[key]) - ((int)listB[key]), 2); } return(Math.Sqrt(sum)); }
static async void Test(string[] args) { string[] foods = { "pork", "bread", "peppers", "sugar", "corn" }; string[] pantryList = { "bread", "turkey", "corn", "potatoes", "cheese", "milk" }; IngredientList userPreferences = new IngredientList(foods); IngredientList pantry = new IngredientList(pantryList); List <Tuple <Recipe, double> > recs = await Recommender.GetRecommendations(userPreferences, pantry); foreach (Tuple <Recipe, double> rec in recs) { Recipe rep = rec.Item1; Console.WriteLine(rep.Label); } }
public async Task Tests() { string[] foods = { "pork", "bread", "peppers", "sugar", "corn" }; string[] pantryList = { "potatoes", "pork" }; IngredientList userPreferences = new IngredientList(foods); IngredientList pantry = new IngredientList(pantryList); List <Tuple <Recipe, double> > recs = await Recommender.GetRecommendations(userPreferences, pantry); foreach (Tuple <Recipe, double> rec in recs) { Recipe rep = rec.Item1; Debug.WriteLine(rep.Label); } }
private static IEnumerable <String> getTempVectorSpace(IngredientList listA, IngredientList listB) { return(listA.Keys.Union(listB.Keys)); }
/* Steps: * Step One (when table objects are added): Convert into IngredientList objects * Step Two: Create a query with the items from the pantry and the allergy/diet restrictions * Step Three: Rank each recipe against the user preferences * Step Four: Return the sorted list */ public static async Task <List <Tuple <Recipe, double> > > GetRecommendations(IngredientList userPreferences, IngredientList pantry, IEnumerable <string> dietaryRestrictions, string diet) { if (client == null) { InitializeClient(); } //TODO: Set the RecipeSearchSettings with allergies and diets //Is there a way to figure out if the user settings have changed? String searchQuery = ""; foreach (String key in pantry.Keys) { if (pantry[key].Equals(1)) { searchQuery += key + " "; } } RecipeSearchSettings searchSettings = new RecipeSearchSettings(); if (dietaryRestrictions != null && dietaryRestrictions.Count() > 0) { foreach (var restriction in dietaryRestrictions) { var enumRestriction = Health.DEFAULT; Enum.TryParse <Health>(restriction, out enumRestriction); if (searchSettings.Health == null) { searchSettings.Health = new List <Health>(); } searchSettings.Health.Add(enumRestriction); } } if (diet != null) { var enumRestriction = Diet.BALANCED; Enum.TryParse <Diet>(diet, out enumRestriction); searchSettings.Diet = enumRestriction; } var recipeRecs = await client.SearchRecipes(searchQuery, searchSettings); if (recipeRecs.Count() == 0) { return(new List <Tuple <Recipe, double> >()); } else { return(GetTopTwelveRecipes(userPreferences, recipeRecs)); } }