Ejemplo n.º 1
0
        public void CalculateCosineDistanceBetweenDifferentVectors()
        {
            double[] vector1 = new double[3];
            vector1[0] = 1;
            vector1[1] = 2;
            vector1[2] = 3;
            double[] vector2 = new double[3];
            vector2[0] = 7;
            vector2[1] = 8;
            vector2[2] = 9;
            double distance = DistanceCalculator.CalculateCosineSimilarity(vector1, vector2);

            Assert.True(Math.Abs(distance - 0.9594119) <= TOLERANCE);
        }
Ejemplo n.º 2
0
        // GET: Sentences/Related/Id
        public async Task <IActionResult> Related(Guid Id)
        {
            if (!ModelState.IsValid)
            {
                return(NotFound());
            }
            var _sent = await _context.Sentences
                        .SingleOrDefaultAsync(s => s.Id == Id &&
                                              s.Type == SentenceType.OBJECTIVE &&
                                              s.InferSentVectorsString != null);

            if (_sent == null)
            {
                return(NotFound());
            }

            var inferSentVector = _sent.InferSentVectorsDouble;

            var relatedSentencesQuery = _context.Sentences
                                        .Where(candidate => candidate.InferSentVectorsString != null && candidate.Id != Id && candidate.TextEntryId != _sent.TextEntryId)
                                        .Select(candidate => new
            {
                sentence = candidate,
                distance = DistanceCalculator.CalculateCosineSimilarity(
                    _sent.InferSentVectorsDouble,
                    candidate.InferSentVectorsDouble)     // distance = 1 if the two sentences are the same
            })
                                        .Where(_sentence => _sentence.distance <1.0 && _sentence.distance> 0.7)
                                        .OrderByDescending(candidate => candidate.distance)
                                        .Take(5)
                                        .Select(_sentence => _sentence.sentence);

            List <Sentence> relatedSentences = await relatedSentencesQuery.ToListAsync();

            return(Json(new
            {
                Sentences = relatedSentences.Select(
                    sent => new SentenceViewModel(sent)),
                SentenceId = Id
            }));
        }