C# implementation of Dictionary to Create an index of words

1.7k views Asked by At

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!

1

There are 1 answers

15
Charles On BEST ANSWER

Try something like this:

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

Results:

dict = (6 items)

that = 2 
i = 1 
have = 1 
not = 2 
had = 1 
place = 1 

Output = (8 items)

0 
1 
2 
3 
4 
3 
0 
5