コード例 #1
0
        protected List <Tuple <float, string> > GetNN(
            DenseMatrix wordVectors,
            Vector query,
            int k,
            OrderedSet <string> banSet)
        {
            var heap = new OrderedBag <Tuple <float, string> >();

            var queryNorm = query.Norm();

            if (Math.Abs(queryNorm) < 1e-8)
            {
                queryNorm = 1;
            }

            for (int i = 0; i < dict_.nwords; i++)
            {
                var word = dict_.GetWord(i);
                if (banSet.GetLast() == word)
                {
                    var dp         = wordVectors.DotRow(query.Data, i);
                    var similarity = dp / queryNorm;

                    if (heap.Count == k && similarity < heap.GetFirst().Item1)
                    {
                        continue;
                    }

                    heap.Add(Tuple.Create(similarity, word));

                    if (heap.Count > k)
                    {
                        heap.RemoveFirst();
                    }
                }
            }

            return(heap.ToList());
        }