我目前正在尝试创建一个应用程序进行文本处理以读取文本文件,然后我使用字典创建单词索引,读取文本文件并检查单词是否已经在那个文件中。如果是,它将打印出索引号并继续检查。
我试图实现一些代码来创建字典。我使用的代码如下:
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
显示&#34;不是&#34;发生2次&#34;&#34;发生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
有谁知道如何填写该代码?非常感谢任何帮助!
答案 0 :(得分:0)
尝试这样的事情:
void Main()
{
var txt = "that i have not had not that place"
.Split(" ".ToCharArray(),StringSplitOptions.RemoveEmptyEntries)
.ToList();
var dict = new OrderedDictionary();
var output = new List<int>();
foreach (var element in txt.Select ((word,index) => new{word,index}))
{
if (dict.Contains(element.word))
{
var count = (int)dict[element.word];
dict[element.word] = ++count;
output.Add(GetIndex(dict,element.word));
}
else
{
dict[element.word] = 1;
output.Add(GetIndex(dict,element.word));
}
}
}
public int GetIndex(OrderedDictionary dictionary, string key)
{
for (int index = 0; index < dictionary.Count; index++)
{
if (dictionary[index] == dictionary[key])
return index; // We found the item
}
return -1;
}
结果:
dict =(6项)
that = 2
i = 1
have = 1
not = 2
had = 1
place = 1
输出=(8项)
0
1
2
3
4
3
0
5