Track multiple values from a method

113 views Asked by At
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace VectorSpaceModel
{
    class CS_Temp
    {
        static void Main(string[] args)
        {
            System.IO.StreamReader file = new System.IO.StreamReader(@"C:\research_fields.txt");

            List<double> d = new List<double>() {0.0};
            Program p = new Program();
            string document = "The trie data structure has many properties which make it especially attractive for representing large files of data. These properties include fast retrieval time, quick unsuccessful search determination, and finding the longest match to a given identifier. The main drawback is the space requirement. In this paper the concept of trie compaction is formalized. An exact algorithm for optimal trie compaction and three algorithms for approximate trie compaction are given, and an analysis of the three algorithms is done. The analysis indicate that for actual tries, reductions of around 70 percent in the space required by the uncompacted trie can be expected. The quality of the compaction is shown to be insensitive to the number of nodes, while a more relevant parameter is the alphabet size of the key.";
            string line;

            while ((line = file.ReadLine()) != null)
            {
                d.Add(p.calculate_CS(line, document));
            }

            d.Sort();
            d.Reverse();

            System.IO.StreamWriter fileW = new System.IO.StreamWriter(@"C:\write_research_fields_temp.txt");
            foreach (double item in d)
            {
                fileW.WriteLine(item.ToString());
            }

            fileW.Close();
        }
    }
}

This program calculates cosine_similarity of string document and research_fields ( a text file read by using StreamReader - reading one line at one time). Then it is saving all the double returned values in another text file, values sorted and order by descending. I want to keep track of against which line (string) in the research_fields.txt file the highest value returned. I'm getting the values but not able to keep track that against which line (string) in research_fields.txt has the highest value returned and vice versa.

The research_fields.txt file is look like......

access control policies
active learning
ad hoc network
ad hoc routing
agent based reasoning
animating crowded pedestrian
anomaly detection
ant colony optimization
applied mathematics
approximation algorithm
archiving system
artificial intelligence
artificial neural network
aspect oriented programming
............
.........
.....
2

There are 2 answers

4
Marcelo de Aguiar On BEST ANSWER

Try using a tuple to store line (or line number) in the list.

var d = new List<Tuple<string, double>>();
.
.
.
while ((line = file.ReadLine()) != null)
{
    d.Add(Tuple.Create(line, p.calculate_CS(line, document)));
}
.
.
.
foreach (double item in d.OrderByDescending(t => t.Item2))
{
    fileW.WriteLine("{0} from line {1}", item.Item2, item.Item1);
}
0
Devesh On

In this case you have to implement your own custom logic which is simple. I hope you must have got the logic , you change the idx parameter with the value you are looking for.

           int idx = -1;
           double maxVal = -1;
           for(int i = 0 ; i < count ; i++)
            {
                if(arr[i] > maxVal)
                  {
                      maxVal = arr[i];
                      idx = i;
                  }
            }

You can also create a class or struct to hold the additional value but it will be overkill if you want just one final value.