private static void PopulateStaticTypedTargetSpecies(ReferenceProduct p)
 {
     if (p.TargetSpecies == null || !p.TargetSpecies.Any())
     {
         return;
     }
     p.TargetSpeciesTyped = p.TargetSpecies.SelectMany(TargetSpecies.Find).Distinct().ToArray();
 }
 public static ProductSimilarityResult GetMatchingResult(this ActionedProduct product, ReferenceProduct referenceProduct, ProductMatchConfig cfg) => new ProductSimilarityResult
 {
     InputProduct          = product,
     ReferenceProduct      = referenceProduct,
     ProductNameSimilarity =
         new ProductNameMetric(cfg.NameMetricConfig)
         .GetSimilarity(product.Product.Name, referenceProduct.Name)
 };
        private static async Task PopulateTargetSpeciesFromEma(ReferenceProduct expiredProduct)
        {
            var possibleTargetSpecies = new Dictionary <string, string[]>();
            var searchResults         = await EPARTools.GetSearchResults(expiredProduct.Name);

            foreach (var result in searchResults)
            {
                var tf = Path.GetTempFileName();
                using (var tfs = File.OpenWrite(tf))
                {
                    (await GetHttpStream(result)).CopyTo(tfs);
                }

                var targetSpecies = SPCParser.GetTargetSpeciesFromMultiProductPdf(tf);
                foreach (var ts in targetSpecies)
                {
                    possibleTargetSpecies[ts.Key.ToLowerInvariant()] = ts.Value;
                }
                File.Delete(tf);
            }

            var productKey = expiredProduct.Name.ToLowerInvariant();

            //exact name match
            if (possibleTargetSpecies.ContainsKey(productKey))
            {
                expiredProduct.TargetSpecies = possibleTargetSpecies[productKey];
            }

            //todo:smarter nonexact matching

            //name starts with
            else if (possibleTargetSpecies.Keys.Any(k => k.StartsWith(productKey)))
            {
                productKey = possibleTargetSpecies.Keys.Single(k => k.StartsWith(productKey));
                expiredProduct.TargetSpecies = possibleTargetSpecies[productKey];
            }

            //resolve inconsistent spacing in name between VMD and EMA
            else if (possibleTargetSpecies.Keys.Any(k => k.Replace(" ", "").Equals(productKey.Replace(" ", ""))))
            {
                productKey =
                    possibleTargetSpecies.Keys.Single(k => k.Replace(" ", "").Equals(productKey.Replace(" ", "")));
                expiredProduct.TargetSpecies = possibleTargetSpecies[productKey];
            }

            //get the bit after "for" in the name. Risky - e.g. "solution for injection"
            //could maybe do with species lookup for validation
            else if (productKey.Contains(" for "))
            {
                var forSplit = productKey.Split(new[] { "for" }, StringSplitOptions.None);
                var postFor  = forSplit[forSplit.Length - 1].Replace("and", ",").Split(',')
                               .Select(t => t.Trim().ToLowerInvariant())
                               .Where(t => !string.IsNullOrWhiteSpace(t));

                expiredProduct.TargetSpecies = postFor.ToArray();
            }
            else
            {
                Debug.WriteLine($"{expiredProduct.Name} ReferenceProduct not found");
            }
            PopulateStaticTypedTargetSpecies(expiredProduct);
        }