I'm currently trying to create an application to do some text processing to read in a text file, then I use a dictionary to create index of words, reading a text file and checking to see if the word is already in that file or not. If so, it will print out the index number and continue the check.
I've tried to implement some code to create the dictionary. The code I'm using is as follows:
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();
}
}
Here is an example of the text file I'm inputting: http://pastebin.com/ZRVbhWhV
A quick ctrl-F
shows that "not" occurs 2 times and "that" occurs 4 times. What I need to do is to index each word and call it in like this:
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
Does anyone know how to complete that code? Any help is much appreciated!
Try something like this:
Results:
dict = (6 items)
Output = (8 items)