RecipeItem ParseSingleMixedFraction(string str) { var ri = new RecipeItem(); const string intDblFind = @"(?<int1>[0-9]*)\s+(?<num1>[0-9]+)/(?<don1>[0-9]+)\s(?<words>[\w*\W*]+)"; var intDblRegEx = new Regex(intDblFind); var intDblMatches = intDblRegEx.Match(str); if (intDblMatches.Success) { double int1, num1, don1, tot1; double.TryParse(intDblMatches.Groups["int1"].Value, out int1); double.TryParse(intDblMatches.Groups["num1"].Value, out num1); double.TryParse(intDblMatches.Groups["don1"].Value, out don1); if (Math.Abs(don1) > EPSILON) { tot1 = int1 + num1 / don1; } else { tot1 = int1; } ri.Amount = tot1; ri.UpAmount = 0; ri.Value = intDblMatches.Groups["words"].Value; } return(ri); }
//ParseUnit takes in a reffrenct to a recipe item, sets the messu //unit and value of recipe item. string ParseUnit(ref RecipeItem str) { Console.WriteLine("----------------------------------"); Console.WriteLine("str: {0}", str.Value); var ur = new Units(); var unit = JsonConvert.DeserializeObject <AllUnits>(File.ReadAllText(@"../../Json/Units.json")); // var cupReg = unit.unitRegex[0]; var loopRegEx = ""; foreach (var u in unit.unitTypes) { foreach (var l in u.units) { loopRegEx = l.regex + wRegEx; //Console.WriteLine("regeex: {0}", loopRegEx); var match = new Regex(loopRegEx).Match(str.Value.ToLower()); if (match.Success) { str.MessumentUnit = l.name; str.Value = match.Groups["words"].Value; Console.WriteLine("we made a {0} match: {1}\n", str.MessumentUnit, str.Value); return(str.MessumentUnit); } } } Console.WriteLine("we made no match."); return(""); }
RecipeItem ParseInt(string str) { var ri = new RecipeItem(); const string intFind = @"(?<int1>[0-9]+)\s+(?<words>[\w*\W*]+)"; var intRegEx = new Regex(intFind); var matches = intRegEx.Match(str); if (matches.Success) { double.TryParse(matches.Groups["int1"].Value, out double int1); ri.Amount = int1; ri.UpAmount = 0; ri.Value = matches.Groups["words"].Value; } return(ri); }
RecipeItem ParseEitherTo(string str) { var ri = new RecipeItem(); //this regex will seperate mixed fractions seperated by the word to with group names for each part of each //fraction. const string eitherToEither = @"((?<int1>[0-9]*)\s*)((?<num1>[0-9]+)/(?<don1>[0-9]+))*\s+to\s+((?<int2>[0-9]*)\s*)((?<num2>[0-9]+)/(?<don2>[0-9]+))*\s(?<words>[\w*\s,""]+)"; var eitherToEitherRegEx = new Regex(eitherToEither); var eitherToMatches = eitherToEitherRegEx.Match(str); if (eitherToMatches.Success) { double tot1, tot2; double.TryParse(eitherToMatches.Groups["int1"].Value, out double int1); double.TryParse(eitherToMatches.Groups["num1"].Value, out double num1); double.TryParse(eitherToMatches.Groups["don1"].Value, out double don1); double.TryParse(eitherToMatches.Groups["int2"].Value, out double int2); double.TryParse(eitherToMatches.Groups["num2"].Value, out double num2); double.TryParse(eitherToMatches.Groups["don2"].Value, out double don2); //Do some math if we need to do some division. if (Math.Abs(don1) > EPSILON) { tot1 = int1 + num1 / don1; } else { tot1 = int1; } if (Math.Abs(don2) > EPSILON) { tot2 = int2 + num2 / don2; } else { tot2 = int2; } ri.Amount = tot1; ri.UpAmount = tot2; ri.Value = eitherToMatches.Groups["words"].Value; } return(ri); }
RecipeItem ParseDouble(string str) { var ri = new RecipeItem(); const string dblFind = @"(?<num1>[0-9]+)/(?<don1>[0-9]+)\s+(?<words>[\w*\W*]+)"; var dblRegEx = new Regex(dblFind); var matches = dblRegEx.Match(str); if (matches.Success) { double.TryParse(matches.Groups["num1"].Value, out double num1); double.TryParse(matches.Groups["don1"].Value, out double don1); var tot = num1 / don1; ri.Amount = tot; ri.UpAmount = 0; ri.Value = matches.Groups["words"].Value; } return(ri); }
//Function to parse recipe value public RecipeItem ParseRecipeItem(string str) { var ri = new RecipeItem(); var recipeItem = ParseEitherTo(str); if ((recipeItem != null) && (Math.Abs(recipeItem.Amount) > EPSILON)) { recipeItem.MessumentUnit = ParseUnit(ref recipeItem); return(recipeItem); } recipeItem = ParseSingleMixedFraction(str); if (Math.Abs(recipeItem.Amount) > EPSILON) { recipeItem.MessumentUnit = ParseUnit(ref recipeItem); return(recipeItem); } recipeItem = ParseDouble(str); if (Math.Abs(recipeItem.Amount) > EPSILON) { recipeItem.MessumentUnit = ParseUnit(ref recipeItem); return(recipeItem); } recipeItem = ParseInt(str); if (Math.Abs(recipeItem.Amount) > EPSILON) { recipeItem.MessumentUnit = ParseUnit(ref recipeItem); return(recipeItem); } ri.Value = str; ri.Amount = 0; ri.UpAmount = 0; return(ri); }
// public void Process(ref RecipeItem rec) { string text = rec.Value.ToLower(); // Annotation var annotation = new Annotation(text); pipeline.annotate(annotation); // these are all the sentences in this document // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types var sentences = annotation.get(typeof(CoreAnnotations.SentencesAnnotation)); if (sentences == null) { return; } var adj = ""; var noun = ""; foreach (Annotation sentence in sentences as ArrayList) { //var token = sentence.get(typeof(CoreAnnotations.PartOfSpeechAnnotation)); var token = sentence.get(typeof(CoreAnnotations.TokensAnnotation)); CoreLabel prev = new CoreLabel(); CoreLabel next; bool isNote = false; foreach (CoreLabel typ in token as ArrayList) { object word = typ.get(typeof(CoreAnnotations.TextAnnotation)); var pos = typ.get(typeof(CoreAnnotations.PartOfSpeechAnnotation)); Console.WriteLine("type: {0}, word: {1}", pos, word); string test = pos.ToString().ToLower(); if (isNote) { rec.Notes += " " + word; } if (test.Contains(",")) { isNote = true; } if (test.Contains("jj")) { adj += " " + word; } if (test.Contains("nn")) { noun += " " + word; } if (prev.value() != null) { word = prev.get(typeof(CoreAnnotations.TextAnnotation)); pos = prev.get(typeof(CoreAnnotations.PartOfSpeechAnnotation)); } prev = typ; } } Console.WriteLine("\n"); rec.Adj = adj; rec.Noun = noun; }
static void Main(string[] args) { var compositeRecipe = new Dictionary <string, List <RecipeItem> >(); var langProc = new LangProcessing(); const string str = "I went or a run. Then I went to work. I had a good lunch meeting with a friend name John Jr. The commute home was pretty good."; var rec = new RecipeItem(); rec.Value = str; //langProc.Process(ref rec); //handl string query; if (args.Length > 0) { query = args[0] ?? "french onion soup"; } else { query = "Fried okra"; } var ih = new InternetHelpers(); var rf = new RecipeFactory(); var r = new Recipe(); IList <Result> paging = new List <Result>(); IList <Recipe> recipes = new List <Recipe>(); var listRequest = ih.GetUrls(query); var count = 0; var logStuff = new LogStuff(); logStuff.recipeTotal = 0; logStuff.yieldTotal = 0; var yeildArr = new List <int>(); while (paging != null && count < 1) { listRequest.Start = count * 10 + 1; paging = listRequest.Execute().Items; foreach (var item in paging) { HtmlNodeCollection nodes = ih.GetNodes(item.Link); r = new Recipe(); if (nodes != null && item.Link.Contains("recipe")) { Console.WriteLine("\n{0}\n", item.Link); var node = nodes[0]; JObject jRecipe = JObject.Parse(node.InnerHtml); IList <JsonRecipe> searchResults = new List <JsonRecipe>(); r.recipeUrl = item.Link; if (jRecipe.TryGetValue("name", out JToken val)) { r.name = val.ToString(); } if (jRecipe.TryGetValue("recipeYield", out val)) { var temp = val.ToString(); var yield = rf.ParseYield(temp); if (yield > logStuff.maxYield) { logStuff.maxYield = yield; } if (yield < logStuff.minYield) { logStuff.minYield = yield; } logStuff.recipeTotal++; logStuff.yieldTotal += yield; r.recipeYield = yield; } if (jRecipe.TryGetValue("cookTime", out val)) { r.cookTime = val.ToString(); } if (jRecipe.TryGetValue("prepTime", out val)) { r.prepTime = val.ToString(); } if (jRecipe.TryGetValue("totalTime", out val)) { r.totalTime = val.ToString(); } if (jRecipe.TryGetValue("recipeInstructions", out val)) { var count2 = 1; foreach (JToken result in val.Children().Values()) { // JToken.ToObject is a helper method that uses JsonSerializer internally var searchResult = result.ToString(); r.instructions.Add(count2, searchResult); count2++; } } if (jRecipe.TryGetValue("recipeIngredient", out val)) { foreach (JToken result in val.Children().Values()) { // JToken.ToObject is a helper method that uses JsonSerializer internally var searchResult = result.ToString(); var temp = rf.ParseRecipeItem(searchResult); langProc.Process(ref temp); var noun = temp.Noun; if (!r.ingredients.ContainsKey(noun)) { r.ingredients.Add(noun, temp); } if (compositeRecipe.ContainsKey(noun)) { compositeRecipe[noun].Add(temp); } else { List <RecipeItem> tList = new List <RecipeItem>(); tList.Add(temp); compositeRecipe.Add(noun, tList); } } } recipes.Add(r); } } count++; } foreach (var ri in compositeRecipe) { Console.WriteLine("item: {0}, count: {1}\n", ri.Key, ri.Value.Count); foreach (var item in ri.Value) { Console.WriteLine("\tname : {0}", item.Adj.Trim() + " " + item.Noun.Trim()); } } var avgYield = logStuff.yieldTotal / logStuff.recipeTotal; foreach (var ri in recipes) { var recipeRatio = (double)ri.recipeYield / avgYield; foreach (var i in ri.ingredients) { i.Value.Amount = i.Value.Amount * recipeRatio; } // PrintRecipe(ri); } Console.WriteLine("\nratio: {0} minYield: {1} maxYield: {2}\n", avgYield, logStuff.minYield, logStuff.maxYield); return; }