Exemplo n.º 1
0
        /// <summary>
        /// Write the cosine similarity of the given <c>NewsItem</c>s to 
        /// the file cosine_similarity_log.txt.
        /// </summary>
        /// <param name="archivist">
        /// The <c>Archivist</c> for getting database information.
        /// </param>
        /// <param name="news">
        /// The <c>NewsItem</c>s to print the cosine similarity of.
        /// </param>
        private static void WriteCosineSimilarity(Archivist archivist, List<NewsItem> news)
        {
            // Sort the news according to title.
            news.Sort((n1, n2) => int.Parse(n1.Title).CompareTo(int.Parse(n2.Title)));

            // Get the vector of all news and add them to a matrix.
            List<SparseVector> vectors = new List<SparseVector>();
            foreach (NewsItem n in news)
            {
                vectors.Add(archivist.GetTfIdfVector(n));
            }

            // Create a matrix.
            SparseMatrix matrix =
                new SparseMatrix(vectors.First().Dimension, vectors.Count);
            // Add entries to the matrix.
            for (int i = 0; i < vectors.Count; i++)
            {
                for (int j = 0; j < vectors.First().Dimension; j++)
                {
                    matrix[j, i] = vectors[i][j];
                }
            }

            // Transpose matrix and thus prepare for multiplication.
            SparseMatrix mTransposed = matrix.Transpose().NormalizeRows();

            // Calculate the result.
            SparseMatrix result = mTransposed.Product(matrix.Transpose().NormalizeRows().Transpose());

            // Write to file.
            using (StreamWriter file = new StreamWriter("cosine_similarity_log.txt"))
            {
                int rowLength = result.Rows;
                int columnLength = result.Columns;

                // Print header.
                file.Write("      ");
                for (int i = 0; i < result.Columns; i++)
                {
                    file.Write("dok" + (i + 1).ToString("0#") + " ");
                }
                file.WriteLine();

                // Print the matrix.
                for (int i = 0; i < rowLength; i++)
                {
                    // Print doc title.
                    file.Write("dok" + (i + 1).ToString("0#") + " ");
                    for (int j = 0; j < columnLength; j++)
                    {
                        file.Write(result[i, j].ToString("0.##0") + " ");
                    }

                    if (i != rowLength - 1)
                    {
                        file.WriteLine();
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void ProductTestGreaterThanZero()
        {
            SparseMatrix expected = new SparseMatrix(3, 3);
            expected[0, 0] = 13.2000008f;
            expected[0, 2] = 2.69999981f;
            expected[1, 1] = 1.64f;
            expected[2, 0] = 10.5f;
            expected[2, 2] = 0.45f;

            SparseMatrix factor = new SparseMatrix(3, 5);
            factor[0, 1] = 2.0f;
            factor[0, 2] = 3.0f;
            factor[1, 3] = 0.1f;
            factor[1, 4] = 1.3f;
            factor[2, 0] = 0.9f;
            factor[2, 1] = 1.5f;
            factor = factor.Transpose();

            SparseMatrix result = TestMatrix.Product(factor);

            Assert.IsTrue(expected.ApproximatelyEqual(result));
        }