Пример #1
0
        public MatchResults CreateResults(string ingredients)
        {
            var parts = ingredients
                        .Split(',')
                        .Select(p => p.Trim().Trim('.'));
            var results = new MatchResults(parts);

            results = _rules.Aggregate(results, (seed, rule) =>
            {
                var lookup   = rule.Ingredients;
                var matches1 = seed.Remainder.Intersect(lookup,
                                                        IngredientComparer.Instance);

                var matches2 = from i in lookup
                               from r in seed.Remainder
                               where r.Contains('/')
                               where r.Split('/').Contains(i)
                               select r;

                var matches3 = matches1.Concat(matches2).ToList();
                if (matches3.Any())
                {
                    seed.Matches.Add(new Rule {
                        Name        = rule.Name,
                        Description = rule.Description,
                        Result      = rule.Result,
                        Ingredients = matches3
                    });
                    if (rule.Result == "danger")
                    {
                        results.Result = "danger";
                    }
                    else if (rule.Result == "warning" && results.Result == "good")
                    {
                        results.Result = "warning";
                    }

                    foreach (var match in matches3)
                    {
                        seed.Remainder.Remove(match);
                    }
                }
                return(seed);
            });

            return(results);
        }
Пример #2
0
        public void Count(MatchResults results)
        {
            try
            {
                if (!string.IsNullOrEmpty(results.ProductName))
                {
                    _db.SortedSetIncrement(
                        PRODUCTS_KEY, results.ProductName, 1, CommandFlags.FireAndForget);
                }

                foreach (var unknown in results.Remainder)
                {
                    if (!string.IsNullOrEmpty(unknown))
                    {
                        _db.SortedSetIncrement(
                            UNKNOWN_INGREDIENTS_KEY, unknown, 1, CommandFlags.FireAndForget);
                    }
                }
            }
            catch
            {
                //swallow error
            }
        }
Пример #3
0
        public MatchResults CreateResults(string ingredients)
        {
            var parts   = ingredients.Split(',').Select(p => p.Trim().Trim('.'));
            var results = new MatchResults(parts);

            results = _rules.Aggregate(results, (seed, rule) =>
            {
                var lookup  = rule.Ingredients;
                var matches = seed.Remainder.Intersect(lookup, StringComparer.OrdinalIgnoreCase).ToList();
                if (matches.Any())
                {
                    seed.Matches.Add(new Rule {
                        Name        = rule.Name,
                        Description = rule.Description,
                        Result      = rule.Result,
                        Ingredients = matches
                    });
                    if (rule.Result == "danger")
                    {
                        results.Result = "danger";
                    }
                    else if (rule.Result == "warning" && results.Result == "good")
                    {
                        results.Result = "warning";
                    }

                    foreach (var match in matches)
                    {
                        seed.Remainder.Remove(match);
                    }
                }
                return(seed);
            });

            return(results);
        }