public static async Task <IEnumerable <Provision> > PrecedentSearchAsync(SearchRequest request, string connectionString)
        {
            if (request == null || request.IsEmpty())
            {
                return(null);
            }
            using (var dataAccess = new MasterSideLetterDataAccess(connectionString))
            {
                var searchSettings = await dataAccess.GetSearchSettingsAsync();

                var provisionHits = new List <Provision>();
                var currentScores = new[] { 0d, 0d, 0d, 0d };
                //var currentScore = 0d;
                var currentContent = "";


                foreach (var provision in dataAccess.GetFilteredProvisions(request))
                {
                    if (provision.Content != currentContent)
                    {
                        currentContent = provision.Content;
                        currentScores  = currentContent == request.TargetText ? new[] { 1d, 1d, 1d, 1d } : GetProvisionScores(searchSettings, request.TargetText, currentContent);
                    }

                    if (currentScores[0] >= searchSettings.WeightedThreshold ||
                        currentScores[1] >= searchSettings.Algorithm1Threshold ||
                        currentScores[2] >= searchSettings.Algorithm2Threshold ||
                        currentScores[3] >= searchSettings.Algorithm3Threshold)
                    {
                        provision.HtmlContent = GetProvisionDifferences(request.TargetText, currentContent);
                        provision.MatchScore  = currentScores[0];
                        provisionHits.Add(provision);
                    }
                }
                return(provisionHits);
            }
        }
        public static async Task InheritProvisionTypesAsync(List <int> provisionIds, string connectionString)
        {
            using (var dataAccess = new MasterSideLetterDataAccess(connectionString))
            {
                var searchSettings = await dataAccess.GetSearchSettingsAsync();

                var provisions    = (await dataAccess.GetProvisionsAsync(provisionIds)).ToList();
                var currentScores = provisions.ToDictionary(p => p, p => new ProvisionTypeScore());
                foreach (var allProvision in dataAccess.GetFilteredProvisions(null, true))
                {
                    Parallel.ForEach(provisions.Where(p => p.Id != allProvision.Id && currentScores[p].Score < 1), provision =>
                    {
                        var score = provision.Content == allProvision.Content ? 1 : GetProvisionScore(searchSettings, provision.Content, allProvision.Content);

                        if (score >= searchSettings.InheritThreshold && score > currentScores[provision].Score)
                        {
                            currentScores[provision].ProvisionType = allProvision.ProvisionType;
                            currentScores[provision].Score         = score;
                        }
                    });
                }

                //update inherited tags in db
                foreach (var pair in currentScores)
                {
                    var provision = pair.Key;
                    var score     = pair.Value;
                    if (score.Score > 0 && provision.ProvisionType != score.ProvisionType)
                    {
                        provision.ProvisionType            = score.ProvisionType;
                        provision.IsProvisionTypeInherited = true;
                        await dataAccess.UpdateProvisionAsync(provision);
                    }
                }
            }
        }