2009-10-30 41 views
0

獲取的話行列我做了這個代碼,你看我執行多個查詢從一個數組

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Collections; 

namespace IR 
{ 
    class Program 
    { 
     public static bool Search(string[] stop, string w) 
     { 
      for (int i = 0; i < stop.Length; i++) 
       if (w == stop[i]) 
        return true; 
      return false; 
      // i didnt add the else thing becuse i want it to continue searching 
     } 

     public static int Count(ArrayList doc, string term) 
     { 
      int c=0; 
      for (int i = 0; i < doc.Count; i++) 
      { 
       if (term == doc[i].ToString()) 
       { 
        c++; 
       } 

      } 
      return c; // be carefull keep this put of the for 
     } 

     static void Main(string[] args) 
     { 
      string d1 = "java is fun. java is a programming language."; 
      string d2 = "java is a hard language of all language."; 
      string d3 = "all language is hard. hard language."; 

      string[] stop={"","is","all","a","of","the","to","at"}; 
      string[] d1words = d1.Split(' ','.'); 
      string[] d2words = d2.Split(' ', '.'); 
      string[] d3words = d3.Split(' ', '.'); 


      //removing stop words 
      //after addding the using system collections 

      ArrayList d1good = new ArrayList(); 
      for (int i = 0; i < d1words.Length; i++) 
       if (Search(stop, d1words[i]) == false) 
        d1good.Add(d1words[i]); 

      ArrayList d2good = new ArrayList(); 
      for (int i = 0; i < d2words.Length; i++) 
       if (Search(stop, d2words[i]) == false) 
        d2good.Add(d2words[i]); 

      ArrayList d3good = new ArrayList(); 
      for (int i = 0; i < d3words.Length; i++) 
       if (Search(stop, d3words[i]) == false) 
        d3good.Add(d3words[i]); 


      for (int i = 0; i < d1good.Count; i++) 

       Console.WriteLine(d1good[i]); 

      Console.WriteLine("----------------------------"); 

      for (int i = 0; i < d2good.Count; i++) 

       Console.WriteLine(d2good[i]); 

      Console.WriteLine("----------------------------"); 

      for (int i = 0; i < d3good.Count; i++) 

       Console.WriteLine(d3good[i]); 

      Console.WriteLine("----------------------------"); 



      double[,] W = new double[3, 5]; 
      W[0, 0] = Count(d1good, "java") * Math.Log(3.0/2.0);// the 2 i have to count it not just add it i just did it to save time 
      W[0, 1] = Count(d1good, "programming") * Math.Log(3.0/1.0); 
      W[0, 2] = Count(d1good, "language") * Math.Log(3.0/3.0); 
      W[0, 3] = Count(d1good, "hard") * Math.Log(3.0/2.0); 
      W[0, 4] = Count(d1good, "fun") * Math.Log(3.0/1.0); 

      W[1, 0] = Count(d2good, "java") * Math.Log(3.0/2.0);// the 2 i have to count it not just add it i just did it to save time 
      W[1, 1] = Count(d2good, "programming") * Math.Log(3.0/1.0); 
      W[1, 2] = Count(d2good, "language") * Math.Log(3.0/3.0); 
      W[1, 3] = Count(d2good, "hard") * Math.Log(3.0/2.0); 
      W[1, 4] = Count(d2good, "fun") * Math.Log(3.0/1.0); 

      W[2, 0] = Count(d3good, "java") * Math.Log(3.0/2.0);// the 2 i have to count it not just add it i just did it to save time 
      W[2, 1] = Count(d3good, "programming") * Math.Log(3.0/1.0); 
      W[2, 2] = Count(d3good, "language") * Math.Log(3.0/3.0); 
      W[2, 3] = Count(d3good, "hard") * Math.Log(3.0/2.0); 
      W[2, 4] = Count(d3good, "fun") * Math.Log(3.0/1.0); 

      Console.WriteLine("----------------------------"); 
      Console.WriteLine("----------------------------"); 

      for (int i = 0; i < 3; i++) 
      { 
       for(int j=0; j<5; j++) 
       { 
        Console.Write(W[i,j] + ", "); 
       } 
       Console.WriteLine(); 
      } 

     } 
    } 
} 

現在我希望程序讀取用戶在Word(寫入/ readline的),其中有三串和然後執行搜索每個(字符串),並給我的單詞的排名(這是更重複的單詞)exp:java --- 4次\ hard --- 5

現在我知道了如何閱讀一個單詞,我知道我應該分割字符串以獲取單詞,但是如何獲得每個單詞的排名然後根據排名排列它們的方式是什麼?

回答

1

那真的很容易使用LINQ:(凡words是某種類型的IEnumerable<string>,例如數組)

var frequency = words.GroupBy(word => word) 
        .ToDictionary(g => g.Key, g => g.Count()); 

我看你使用的是ArrayList儘管清楚其.NET 2.0至少(通過使用指令來判斷) - 仍然使用非泛型集合的任何理由?

編輯:好的,非LINQ的解決方案(雖然我建議你去學習,而不是LINQ,真的 - 它使生活所以容易得多):

Dictionary<string, int> frequency = new Dictionary<string, int>(); 
foreach (string word in words) 
{ 
    int count; 
    frequency.TryGetValue(word, out count); // Will default to 0 if not found 
    frequency[word] = count + 1; 
} 
+0

我不想使用LINQ喬恩,,,我仍然不是癡迷者 thanx爲您的回覆 – nader 2009-10-30 12:01:59

+0

尊重 - 當然這是最好的regared作爲一個機會熟悉(如果這是不使用Linq的唯一原因)? – Murph 2009-10-30 12:48:50

+0

如果你向我解釋你在這裏做了什麼可以嗎? 請裸露在我身邊 – nader 2009-10-30 13:45:41