示例#1
0
 /// <summary>
 /// get suggestions from current node
 /// </summary>
 /// <param name="qty">how many suggestions</param>
 /// <param name="considerSuffixLength">consider or not Suffixes length</param>
 /// <param name="getWordProbability">a method delegate for computing a word's probability</param>
 /// <returns>a sorted list of the suggestions according to their probabilities</returns>
 public SortedList<double, List<string>> GetPrefixSuggestions(int qty,bool considerSuffixLength,PrDel getWordProbability, Comparer<double> c)
 {
     return current.GetSuggestions(qty, considerSuffixLength, getWordProbability,c);
 }
示例#2
0
        public SortedList<double, List<string>> GetSuggestions(Int32 qty, bool considerSuffixLength,PrDel getWordProbability,Comparer<double> c)
        {
            if (qty < 0) return null;
            if (suffixes == null) return (new SortedList<double, List<string>>(c));

            if (suffixes.Count < qty) qty = suffixes.Count;
            var suggestions = new SortedList<double, List<string>>(c);
            double pr;
            List<string> li;
            foreach (var s in suffixes)
            {
                pr = getWordProbability(s.word);
                if (considerSuffixLength) pr *= (s.word.Length - GetPrefix().Length + 1); // +1 for the space (' ')
                if (suggestions.ContainsKey(pr)) {
                    li = suggestions[pr];
                    suggestions.Remove(pr);
                }
                else
                    li = new List<string>();
                if (li.Count < 5)
                    li.Add(s.word);
                suggestions.Add(pr,li);
            }
            int  count = 0;
            int amt = qty;
            foreach (var list in suggestions.Values)
            {
                amt -= list.Count;
                count++;
                if (amt <= 0) break;
            }
            var res = suggestions.Take(count);
            var ret = new SortedList<double, List<string>>(c);
            count = 0;
            foreach(var kvp in res)
            {
                count += kvp.Value.Count;
                if (count > qty)
                {
                    var range = kvp.Value.Count - (count-qty);
                    ret.Add(kvp.Key,kvp.Value.GetRange(0,range));
                    break;
                }
                else
                    ret.Add(kvp.Key,kvp.Value);
            }
            return ret;
        }