3
我想输入一个List<string[]>
和获取从一个列表<string[]>唯一的字符串的计数到字典
输出是一个字典,其中键是用于一个索引和值唯一的字符串是float数组在阵列中表示关键的一个string[]
在List<string[]>
到目前为止,这里的计数每个位置是什么,我试图
static class CT
{
//Counts all terms in array
public static Dictionary<string, float[]> Termfreq(List<string[]> text)
{
List<string> unique = new List<string>();
foreach (string[] s in text)
{
List<string> groups = s.Distinct().ToList();
unique.AddRange(groups);
}
string[] index = unique.Distinct().ToArray();
Dictionary<string, float[]> countset = new Dictionary<string, float[]>();
return countset;
}
}
static void Main()
{
/* local variable definition */
List<string[]> doc = new List<string[]>();
string[] a = { "That", "is", "a", "cat" };
string[] b = { "That", "bat", "flew","over","the", "cat" };
doc.Add(a);
doc.Add(b);
// Console.WriteLine(doc);
Dictionary<string, float[]> ret = CT.Termfreq(doc);
foreach (KeyValuePair<string, float[]> kvp in ret)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
Console.ReadLine();
}
我被困在字典中的一部分。什么是最有效的实施方式?
为什么你会在这里使用'float'?什么将会是一个非整数值? –
您能否提供预期产出的样本? – dcastro
理想情况下有一个更有用的输入,其中有不止一次出现单词... –