private static RankedName CalculateRankForName(SearchReadyValue name,
                                                       List <string> searchTerms,
                                                       string query,
                                                       bool queryContainsPunctuation,
                                                       int nameIndex)
        {
            var rankValues = new List <double>();

            rankValues.Add(CalculateRankValueForPrefixMatch(name, query));

            rankValues.Add(CalculateRankValueForAcronymMatch(name, query));

            for (int searchTermIndex = 0; searchTermIndex < searchTerms.Count; searchTermIndex++)
            {
                string searchTerm = searchTerms[searchTermIndex];

                List <string> words = queryContainsPunctuation ? name.LowercaseWordsWithPunctuation : name.LowercaseWords;
                for (int wordIndex = 0; wordIndex < words.Count; wordIndex++)
                {
                    string word = words[wordIndex];

                    rankValues.Add(
                        CalculateRankValueForWordMatch(word, query, searchTerm, searchTermIndex, wordIndex, nameIndex));
                }
            }

            return(new RankedName
            {
                Name = name.OriginalValue, Ranks = rankValues.OrderByDescending(r => r).Take(5).ToList()
            });
        }
        private static double CalculateRankValueForAcronymMatch(SearchReadyValue name, string query)
        {
            if (name.Acronym.StartsWith(query))
            {
                // Acronym Prefix match
                double prefixAmount = (double)query.Length / (double)name.Acronym.Length;
                double rankValue    = prefixAmount;
                return(rankValue);
            }

            return(0);
        }
        public static double CalculateRankValueForPrefixMatch(SearchReadyValue name, string query)
        {
            if (name.LowercaseValue.StartsWith(query))
            {
                // Prefix match
                double prefixAmount = (double)query.Length / (double)name.LowercaseValue.Length;
                double rankValue    = (prefixAmount / 2) + 0.5;
                return(rankValue);
            }

            return(0);
        }
        public static List <RankedName> GetRankedNames(List <SearchReadyValue> organisationNames,
                                                       List <string> searchTerms,
                                                       string query,
                                                       bool queryContainsPunctuation)
        {
            var names = new List <RankedName>();

            for (var nameIndex = 0; nameIndex < organisationNames.Count; nameIndex++)
            {
                SearchReadyValue name = organisationNames[nameIndex];
                names.Add(CalculateRankForName(name, searchTerms, query, queryContainsPunctuation, nameIndex));
            }

            return(names);
        }