2015-06-09 100 views
-4

我目前正在嘗試創建一個應用程序來執行一些文本處理來讀取文本文件,然後我使用字典創建字索引,讀取文本文件和檢查該單詞是否已經在該文件中。如果是這樣,它會打印索引號並繼續檢查。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 

有誰知道如何完成的代碼?任何幫助深表感謝!

+2

'輸出。 Close()'在'using'語句中是多餘的。當'using'塊退出時,'output'將被關閉。 – Tim

+0

你到底遇到了什麼問題? –

+0

只需將單詞記錄到List中並對Count進行查找即可。 – Paparazzi

回答

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

結果:

字典=(6項)

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

輸出=(8項)

0 
1 
2 
3 
4 
3 
0 
5 
+0

哦,。我在想使用Ordered字典,但是我應該輸入Ordered字典類嗎?很抱歉,因爲我在這個話題上是一個新手而煩惱了你。謝謝@Charles, – Indiastradi

+0

@Indiastradi,我不確定你在問什麼?代碼做你需要它做什麼? – Charles

+0

哦,在這裏,我的意思是,當我嘗試你的代碼,我一直找到「var dict = new OrderedDictionary();」的錯誤;「 ..它說:「無法找到類型或命名空間名稱'OrderedDictionary'(你是否缺少使用指令或程序集引用)?」所以要解決這個錯誤,我應該創建一個有序的字典類?再次感謝你@Charles – Indiastradi