2015-06-09 149 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