예제 #1
0
        public IEnumerable<KeyValuePair<int, double>> Rank(IEnumerable<string> queryTerms, SortedDictionary<string, Term> terms, int documentCount)
        {
            SortedList<int, double> scores = new SortedList<int, double>();
            foreach (var queryTerm in queryTerms)
            {
                if (!terms.ContainsKey(queryTerm))
                    return null;

                Term currentTerm = terms[queryTerm];
                int documentFrequency = currentTerm.Frequency;
                foreach (var posting in currentTerm.Postings)
                {
                    int docId = posting.Key;
                    int termFrequency = posting.Value;
                    double weight = GetTfIdf(termFrequency, documentCount, documentFrequency);
                    if (scores.ContainsKey(docId))
                    {
                        scores[docId] += weight;                        
                    }
                    else
                    {
                        scores.Add(docId, weight);
                    }
                }
            }

            return scores.OrderByDescending(kvp => kvp.Value);
        }