Example #1
0
        public Entity GetPossibleEntity(string input)
        {
            List <Entity> possibilities = new List <Entity>();
            int           mincost       = int.MaxValue;
            Entity        possibility   = null;

            foreach (Entity t in entities)
            {
                int cost = LevenshteinDistance.Compute(input, t.Name);
                if (cost == 0)
                {
                    return(t);
                }
                if (cost <= mincost)
                {
                    mincost = cost;
                    possibilities.Add(t);
                }
            }

            decimal k = -1m;

            foreach (Entity t in possibilities)
            {
                decimal result = ngram.CompareBigram(input, t.Name);
                if (k == -1m || result > k)
                {
                    k           = result;
                    possibility = t;
                }
            }
            return(possibility);
        }
Example #2
0
        public static Candidate Create(List <string> list, string input)
        {
            Candidate candidate = new Candidate();

            candidate.items = (from s in list
                               let ld = LevenshteinDistance.Compute(input, s)
                                        let unigram = NGram.CompareUnigram(input, s)
                                                      let bigram = NGram.CompareBigram(input, s)
                                                                   let ngram = NGram.Compare(input.Length, input, s)
                                                                               orderby bigram descending
                                                                               orderby ld ascending
                                                                               select new DataItem(ld, unigram, bigram, ngram, s)).Take(100);
            return(candidate);
        }
Example #3
0
        /// <summary>
        /// もしかして…
        /// </summary>
        /// <param name="s"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string GetPossibility(string s, IEnumerable <string> source)
        {
            if (s == null)
            {
                throw new ArgumentNullException("s");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            List <string> possibilityList = new List <string>();
            int           mincost         = int.MaxValue;
            string        moshikashite    = null;

            foreach (string t in source)
            {
                int cost = LevenshteinDistance.Compute(s, t);
                if (cost == 0)
                {
                    return(t);
                }
                if (cost <= mincost)
                {
                    mincost = cost;
                    possibilityList.Add(t);
                }
            }

            decimal k = -1m;

            foreach (string t in possibilityList)
            {
                decimal result = NGram.CompareBigram(s, t);
                if (k == -1m || result > k)
                {
                    k            = result;
                    moshikashite = t;
                }
            }
            return(moshikashite);
        }