Ejemplo n.º 1
0
        public List<KeyValuePair<string, double>> RankedResults()
        {
            List<Document> documents = new List<Document>();
            HashSet<string> dataSet = new HashSet<string>();
            foreach (var result in results)
            {
                Document d = new Document(result);
                documents.Add(d);
                foreach (var term in d.tokens()){
                    dataSet.Add(term);
                }
            }
            //Build Document Vectors
            Dictionary<string, Vector> documentVectors = new Dictionary<string, Vector>();
            foreach (var document in documents)
            {
                documentVectors.Add(document.ToString(), new Vector(dataSet, document));
            }
            //Build Query Vector
            Query query = new Query(queryString);
            Vector queryVector = new Vector(dataSet, query);

            Dictionary<string, double> relevance = new Dictionary<string, double>();
            foreach (var documentVector in documentVectors)
            {
                relevance.Add(documentVector.Key, Vector.GetSimilarityScore(queryVector, documentVector.Value));
            }
            //Sort result by most relevant
            List<KeyValuePair<string, double>> myList = relevance.ToList();
            return myList;
        }
Ejemplo n.º 2
0
        public void TestGetVectorNorm1()
        {
            double[] vectorList1 = { 0, 0, 0, 0, 0, 0, 0 };
            var vector1 = new Vector(vectorList1);

            double expected_result = 0;
            double actual_result = Vector.GetVectorNorm(vector1);
            Assert.AreEqual(expected_result, actual_result, "Test GetVectorNorm1 failed");
        }
Ejemplo n.º 3
0
 public static double GetVectorNorm(Vector vector)
 {
     double result = 0.0;
     foreach (var weight in vector.vectorRep)
     {
         result += weight * weight;
     }
     return result;
 }
Ejemplo n.º 4
0
 public static double GetDotProduct(Vector vectorA, Vector vectorB)
 {
     double result = 0.0;
     for (int i = 0; i < vectorA.vectorRep.Count; i++)
     {
         result += vectorA.vectorRep[i] * vectorB.vectorRep[i];
     }
     return result;
 }
Ejemplo n.º 5
0
        public void TestGetSimilarityScore2()
        {
            double[] vectorList1 = { 0, 0, 0, 0, 0, 0, 0 };
            double[] vectorList2 = { 1.12, 0.91, 1.414, 0, 0.89, 0, 1.0 };
            var vector1 = new Vector(vectorList1);
            var vector2 = new Vector(vectorList2);

            double expected_result = 0;
            double actual_result = Vector.GetSimilarityScore(vector1, vector2);
            Assert.AreEqual(expected_result, actual_result, "Test GetSimilarityScore2 failed");
        }
Ejemplo n.º 6
0
        public void TestGetSimilarityScore1()
        {
            double[] vectorList1 = { 1.0, 0.89, 0, 1.0, 2.14, 3.414, 1.414 };
            double[] vectorList2 = { 1.12, 0.91, 1.414, 0, 0.89, 0, 1.0 };
            var vector1 = new Vector(vectorList1);
            var vector2 = new Vector(vectorList2);

            double expected_result = Math.Cos(5.2485/(5.873996 * 21.026492));
            double actual_result = Vector.GetSimilarityScore(vector1, vector2);
            Assert.AreEqual(expected_result, actual_result, "Test GetSimilarityScore1 failed");
        }
Ejemplo n.º 7
0
        public void TestGetDotProduct3()
        {
            double[] vectorList1 = { 0, 0, 0, 0, 0, 0, 0 };
            double[] vectorList2 = { 0, 0, 0, 0, 0, 0, 0 };
            var vector1 = new Vector(vectorList1);
            var vector2 = new Vector(vectorList2);

            double expected_result = 0;
            double actual_result = Vector.GetDotProduct(vector1, vector2);
            Assert.AreEqual(expected_result, actual_result, "Test GetDotProduct3 failed");
        }
Ejemplo n.º 8
0
        public void TestGetDotProduct1()
        {
            double[] vectorList1 = {1.0, 0.89, 0, 1.0, 2.14, 3.414, 1.414};
            double[] vectorList2 = { 1.12, 0.91, 1.414, 0, 0.89, 0, 1.0 };
            var vector1 = new Vector(vectorList1);
            var vector2 = new Vector(vectorList2);

            double expected_result = 5.2485;
            double actual_result = Vector.GetDotProduct(vector1, vector2);
            Assert.AreEqual(expected_result, actual_result, "Test GetDotProduct1 failed");
        }
Ejemplo n.º 9
0
 //compares two vectors and returns their similarity using cosine similarity model.
 public static double GetSimilarityScore(Vector vectorA, Vector vectorB)
 {
     double dotProduct = GetDotProduct(vectorA, vectorB);
     if (dotProduct == 0)
     {
         return 0;
     }
     else
     {
         return Math.Cos(dotProduct / ((GetVectorNorm(vectorA)) * GetVectorNorm(vectorB)));
     }
 }
Ejemplo n.º 10
0
        public void TestGetVectorNorm2()
        {
            double[] vectorList1 = { 1.0, 0.89, 0, 1.0, 2.14, 3.414, 1.414 };
            var vector1 = new Vector(vectorList1);

            double expected_result = 21.026492;
            double actual_result = Vector.GetVectorNorm(vector1);
            Assert.AreEqual(expected_result, actual_result, "Test GetVectorNorm2 failed");
        }