Source Code : C # implementation Apriori algorithm (source code)

C # implementation Apriori algorithm (source code)

  1. /*Program.cs*/
  2. using System; 
  3. using System.Collections.Generic; 
  4. using System.Text; 
  5. using System.Data; 
  6. using System.Data.SqlClient; 
  7. using System.Text.RegularExpressions; 
  8. using System.Collections; 
  9. namespace testApriori1 
  10.  publicclass TestApriori 
  11.  { 
  12.  staticvoid Main(string[] args) 
  13.  { 
  14.  Console.WriteLine(DateTime.Now); 
  15.  ArrayList D = GetEventsFromDB();//
  16.  ArrayList I = GetItems1FromDB();//
  17.  float s = 0.01f;//
  18.  List L = new List();//
  19.  L = Apriori(D, I, s); 
  20.  for (int i = 0; i < L.Count; i++) 
  21.  { 
  22.  Console.WriteLine(L[i].Items); 
  23.  Console.WriteLine(L[i].Sup); 
  24.  } 
  25.  Console.WriteLine(DateTime.Now); 
  26.  Console.Read(); 
  27.  } 
  28.  #region-----Apriori-----
  29.  /// 
  30.  /// Apriori
  31.  /// 
  32.  /// 
  33.  /// 
  34.  /// 
  35.  /// 
  36.  static List Apriori(ArrayList D,ArrayList I,float sup) 
  37.  { 
  38.  List L = new List();//
  39.  if (I.Count == 0) return L; 
  40.  else
  41.  { 
  42.  int[] Icount = newint[I.Count];//,0
  43.  ArrayList Ifrequent = new ArrayList();//
  44.  //?
  45.  Regex r=new Regex(","); 
  46.  for (int i = 0; i < D.Count; i++) 
  47.  { 
  48.  string[] subD=r.Split(D[i].ToString()); 
  49.  for (int j = 0; j < I.Count;j++ ) 
  50.  { 
  51.  string[] subI = r.Split(I[j].ToString()); 
  52.  bool subIInsubD=true; 
  53.  for(int m=0;m
  54.  { 
  55.  bool subImInsubD=false; 
  56.  for(int n=0;n
  57.  if (subI[m] == subD[n]) 
  58.  { 
  59.  subImInsubD = true; 
  60.  continue
  61.  } 
  62.  if(subImInsubD==false) subIInsubD=false; 
  63.  } 
  64.  if(subIInsubD==true) Icount[j]++; 
  65.  } 
  66.  } 
  67.  //L
  68.  for (int i = 0; i < Icount.Length;i++ ) 
  69.  { 
  70.  if (Icount[i] >= sup * D.Count) 
  71.  { 
  72.  Ifrequent.Add(I[i]); 
  73.  ItemSet iSet = new ItemSet(); 
  74.  iSet.Items=I[i].ToString(); 
  75.  iSet.Sup = Icount[i]; 
  76.  L.Add(iSet); 
  77.  } 
  78.  } 
  79.  I.Clear(); 
  80.  I = AprioriGen(Ifrequent); 
  81.  L.AddRange(Apriori(D, I, sup)); 
  82.  return L; 
  83.  } 
  84.  } 
  85.  #endregion-----Apriori-----
  86.  #region-------Apriori-gen---------
  87.  /// 
  88.  /// Apriori-gen
  89.  /// 
  90.  /// 
  91.  /// 
  92.  static ArrayList AprioriGen(ArrayList L) 
  93.  { 
  94.  ArrayList Lk=new ArrayList(); 
  95.  Regex r=new Regex(","); 
  96.  for(int i=0;i
  97.  { 
  98.  string[] subL1 = r.Split(L[i].ToString()); 
  99.  for(int j=i+1;j
  100.  { 
  101.  string[] subL2 = r.Split(L[j].ToString()); 
  102.  //Ltemp
  103.  string temp = L[j].ToString();//
  104.  for(int m=0;m
  105.  { 
  106.  bool subL1mInsubL2=false; 
  107.  for(int n=0;n
  108.  { 
  109.  if (subL1[m] == subL2[n]) subL1mInsubL2 = true; 
  110.  } 
  111.  if (subL1mInsubL2 == false) temp = temp + "," + subL1[m]; 
  112.  } 
  113.  //temp?L?+1temp
  114.  string[] subTemp = r.Split(temp); 
  115.  if (subTemp.Length == subL1.Length + 1) 
  116.  { 
  117.  bool isExists = false; 
  118.  for (int m = 0; m < Lk.Count; m++) 
  119.  { 
  120.  bool isContained = true; 
  121.  for (int n = 0; n < subTemp.Length; n++) 
  122.  { 
  123.  if (!Lk[m].ToString().Contains(subTemp[n])) isContained=false; 
  124.  } 
  125.  if (isContained == true) isExists = true; 
  126.  } 
  127.  if(isExists==false) Lk.Add(temp); 
  128.  } 
  129.  } 
  130.  } 
  131.  return Lk; 
  132.  } 
  133.  #endregion-------Apriori-gen---------
  134.  #region----------------
  135.  /// 
  136.  /// 
  137.  /// 
  138.  /// 
  139.  static ArrayList GetItems1FromDB() 
  140.  { 
  141.  string commandString = "select distinct Model from dbo.vAssocSeqLineItems"; 
  142.  DataSet ds = ExcuteDataSetByCommandString(commandString); 
  143.  int countItems1 = 0; 
  144.  countItems1 = ds.Tables[0].Rows.Count; 
  145.  ArrayList Items1 = new ArrayList(); 
  146.  for (int i = 0; i < countItems1; i++) 
  147.  Items1.Add(ds.Tables[0].Rows[i]["Model"].ToString()); 
  148.  return Items1; 
  149.  } 
  150.  #endregion----------------
  151.  #region----------------
  152.  /// 
  153.  /// 
  154.  /// 
  155.  /// 
  156.  static ArrayList GetEventsFromDB() 
  157.  { 
  158.  string commandString="select count(OrderNumber) from vAssocSeqOrders"
  159.  DataSet ds=ExcuteDataSetByCommandString(commandString); 
  160.  int countEvent = Convert.ToInt32(ds.Tables[0].Rows[0][0]);//
  161.  ArrayList events=new ArrayList(); 
  162.  string temp=null; 
  163.  string orderNumber=null;//OrderNumber,Order
  164.  //
  165.  commandString = "select OrderNumber,Model from vAssocSeqLineItems"; 
  166.  ds = ExcuteDataSetByCommandString(commandString); 
  167.  for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
  168.  { 
  169.  if (orderNumber == null)//
  170.  { 
  171.  orderNumber = ds.Tables[0].Rows[i]["OrderNumber"].ToString(); 
  172.  temp = ds.Tables[0].Rows[i]["Model"].ToString(); 
  173.  } 
  174.  elseif (orderNumber == ds.Tables[0].Rows[i]["OrderNumber"].ToString())//Order
  175.  { 
  176.  temp = temp +","+ ds.Tables[0].Rows[i]["Model"]; 
  177.  } 
  178.  else//Order
  179.  { 
  180.  events.Add(temp); 
  181.  orderNumber = ds.Tables[0].Rows[i]["OrderNumber"].ToString(); 
  182.  temp=ds.Tables[0].Rows[i]["Model"].ToString(); 
  183.  } 
  184.  if (i == ds.Tables[0].Rows.Count - 1) events.Add(temp); 
  185.  } 
  186.  return events; 
  187.  } 
  188.  #endregion----------------
  189.  #region-----commandString------
  190.  /// 
  191.  /// commandString
  192.  /// 
  193.  /// 
  194.  /// 
  195.  static DataSet ExcuteDataSetByCommandString(string commandString) 
  196.  { 
  197.  string connectionString = "server=.;database=AdventureWorksDW;trusted_connection=true"; 
  198.  SqlConnection sqlCon = new SqlConnection(connectionString); 
  199.  sqlCon.Open(); 
  200.  SqlCommand sqlCmd = new SqlCommand(); 
  201.  sqlCmd.Connection = sqlCon; 
  202.  sqlCmd.CommandText = commandString; 
  203.  DataSet ds = new DataSet(); 
  204.  SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd); 
  205.  sqlAdapter.Fill(ds); 
  206.  sqlCon.Close(); 
  207.  return ds; 
  208.  } 
  209.  #endregion-----commandString------
  210.  }