我目前正在尝试创建一个应用程序来执行一些文本处理来读取文本文件,然后我使用字典创建字索引,读取文本文件和检查该单词是否已经在该文件中。如果是这样,它会打印索引号并继续检查。C#实现字典创建字索引
我试着实现一些代码来创建字典。我使用的代码如下:
private void bagofword_Click(object sender, EventArgs e)
{ //creating dictionary in background
Dictionary<string, int> dict = new Dictionary<string, int>();
string rawinputbow = File.ReadAllText(textBox31.Text);
string[] inputbow = rawinputbow.Split(' ');
foreach (String word in inputbow)
{
if (dict.ContainsKey(word))
{
dict[word]++;
}
else
{
dict[word] = 1;
}
}
var ordered = from k in dict.Keys
orderby dict[k] descending
select k;
using (StreamWriter output = new StreamWriter("D:\\output.txt"))
{
foreach (String k in ordered)
{
output.WriteLine(String.Format("{0}: {1}", k, dict[k]));
}
output.Close();
}
}
这里是文本文件我输入的一个示例:http://pastebin.com/ZRVbhWhV
快速ctrl-F
显示,“不”时的2倍和“该”的发生的4倍。我需要做的是索引每个单词,并调用它像这样:
sample input : "that I have not had not that place" dictionary : output.txt: index word 5 1 I 1 2 have 2 3 had 4 4 not 3 5 that 4 6 place 5 6
有谁知道如何完成的代码?任何帮助深表感谢!
'输出。 Close()'在'using'语句中是多余的。当'using'块退出时,'output'将被关闭。 – Tim
你到底遇到了什么问题? –
只需将单词记录到List中并对Count进行查找即可。 – Paparazzi