跟踪方法中的多个值

Track multiple values from a method

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();
        }
    }
}

该程序计算 string documentresearch_fields 的 cosine_similarity(使用 StreamReader 读取的文本文件 - 一次读取一行)。然后它将所有 double 返回的值保存在另一个文本文件中,值按降序排序。 我想跟踪 research_fields.txt 文件中哪一行(字符串)返回的最高值。 我正在获取值,但无法跟踪 research_fields.txt 中的哪一行(字符串)返回了最高值,反之亦然。

research_fields.txt 文件看起来像......

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
............
.........
.....

在这种情况下,您必须实现自己的自定义逻辑,这很简单。我希望你一定有逻辑,你用你正在寻找的值更改 idx 参数。

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

您也可以创建一个 class 或结构来保存附加值,但如果您只想要一个最终值,那就太过分了。

尝试使用元组在列表中存储行(或行号)。

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);
}