跟踪一个方法中的多个值

本文关键字:方法 一个 跟踪 | 更新日期: 2023-09-27 17:59:19

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(使用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
............
.........
.....

跟踪一个方法中的多个值

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

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

在这种情况下,您必须实现自己的自定义逻辑,这很简单。我希望你一定已经得到了逻辑,你用你想要的值来更改idx参数。

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

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