public override void SetByIndex(int index, object value) { lock (host.SyncRoot) { host.SetByIndex(index, value); } }
[SuppressMessage("Microsoft.Contracts", "CC1055")] // Skip extra error checking to avoid *potential* AppCompat problems. public override void SetByIndex(int index, Object value) { lock (_root) { _list.SetByIndex(index, value); } }
public virtual Object this[int index] { get { return(sortedList.GetByIndex(index)); } set { sortedList.SetByIndex(index, value); } }
static public int SetByIndex(IntPtr l) { try { System.Collections.SortedList self = (System.Collections.SortedList)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Object a2; checkType(l, 3, out a2); self.SetByIndex(a1, a2); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
public void TestSetByIndex () { SortedList sl1 = new SortedList (24); for (int i = 49; i >= 0; i--) sl1.Add (100 + i, i); try { sl1.SetByIndex (-1, 77); Assert.Fail ("#A"); } catch (ArgumentOutOfRangeException) { } try { sl1.SetByIndex (100, 88); Assert.Fail ("#B"); } catch (ArgumentOutOfRangeException) { } for (int i = 5; i < 25; i++) sl1.SetByIndex (i, -1); for (int i = 0; i < 5; i++) Assert.AreEqual (i, sl1 [100 + i], "#C1"); for (int i = 5; i < 25; i++) Assert.AreEqual (-1, sl1 [100 + i], "#C2"); for (int i = 25; i < 50; i++) Assert.AreEqual (i, sl1 [100 + i], "#C3"); }
/* record leaving of a function */ void LeaveFunction(SortedList functions, int functionId) { int index = functions.IndexOfKey(functionId); if(index != -1) { int newValue = (int)functions.GetByIndex(index) - 1; if(newValue <= 0) { functions.RemoveAt(index); } else { functions.SetByIndex(index, newValue); } } }
/* record call to a function */ void EnterFunction(SortedList functions, int functionId) { int index = functions.IndexOfKey(functionId); if(index == -1) { functions.Add(functionId, 1); } else { /* if in the list, add 1 to its counter (need to keep keys unique) */ functions.SetByIndex(index, 1 + (int)functions.GetByIndex(index)); } }
private void btn_p5_termList_Click(object sender, EventArgs e) { string line = "", Term = "", POS = "", DocNO_tag = ""; //資料列, 詞彙, 詞性標記 int index = 0, value = 0, pt = 0; StringBuilder SB5 = new StringBuilder(); List<string> temp_list = new List<string>(); SortedList Term_DF_slist = new SortedList(); //整個文件集之"詞彙列表" 及詞會出現次數 DF List<KeyValuePair<string, int>> Doctermtf_list = new List<KeyValuePair<string, int>>(); //-記錄每篇文件出現之詞彙集詞頻 - // if ((openFileDialog1.ShowDialog() == DialogResult.OK) && ((txt_p5_fileName.Text = openFileDialog1.FileName) != null)) //開啟Window開啟檔案管理對話視窗 //{ using (StreamReader myTextFileReader = new StreamReader(@txt_p5_fileName.Text, Encoding.Default)) { SortedList DocTermTF_slist = new SortedList(); //暫存每篇文件之"詞彙列表" 及詞會出現次數 TF // ---- 處理文件集中的每一篇文件與文件中的所有詞彙 (詞彙列表 ,計算 TF , 計算 DF)---- while (myTextFileReader.EndOfStream != true) //非資料檔案結尾 { line = (myTextFileReader.ReadLine().Trim()) + " "; // ----- 若為一篇新文件的開始 if (line.Contains("<DocNo>")) { TotalDocNum++; DocNO_tag = line.Substring(line.IndexOf("<DocNo>"), ((line.IndexOf("</DocNo>") + 8) - line.IndexOf("<DocNo>"))); //line = line.Remove(line.IndexOf("<DocNo>"), ((line.IndexOf("</DocNo>") + 8) - line.IndexOf("<DocNo>"))); // -------------------------------------- 計算詞彙之 DF --------------------------------------------------- if (temp_list.Count != 0) { foreach (string str in temp_list) //複製與計算出現在每一文件之詞彙的"DF" { //-- 計算詞彙之 DF -- if ((index = Term_DF_slist.IndexOfKey(str)) != -1) //在termList中找尋特定的Term,若Term不存在怎則傳回-1 { value = (int)Term_DF_slist.GetByIndex(index); value++; //該term存在,則term的出現次數加1 Term_DF_slist.SetByIndex(index, value); } else Term_DF_slist.Add(str, 1); //該詞彙(term)不存在於termList中,則加入該新詞彙(term) } temp_list.Clear(); //清除紀錄每一篇文件內之詞彙的SortedList } //--------------記錄每篇文件出現之詞彙集詞頻 -Doctermtf_list ----------- if (DocTermTF_slist.Count != 0) { foreach (DictionaryEntry obj in DocTermTF_slist) { KeyValuePair<string, int> x = new KeyValuePair<string, int>((string)obj.Key, (int)obj.Value); Doctermtf_list.Add(x); } DocTermTF_slist.Clear(); } KeyValuePair<string, int> x1 = new KeyValuePair<string, int>(DocNO_tag, 0); //加入文件編號標籤 Doctermtf_list.Add(x1); continue; } // ---------------------------------- 記錄並產生整個文件集之"詞彙列表" 及詞會出現次數 TF------------------------------------------ string[] terms = line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); foreach (string str in terms) { if (str.IndexOf("(") == -1 || str.IndexOf(")") == -1) //找不到'()',就不處理/ continue; Term = str.Substring(0, str.IndexOf("(")); POS = str.Substring(str.IndexOf("(")); if (Term.Length != 0) //若有"詞彙"存在 { //--- 記錄每篇文件之詞彙及其出現次數 --- if ((pt = DocTermTF_slist.IndexOfKey((Term + POS))) != -1) { value = (int)DocTermTF_slist.GetByIndex(pt); value++; //該term存在,則term的出現次數加1 DocTermTF_slist.SetByIndex(pt, value); //紀錄整個文件集之"詞彙列表" 及詞會出現次數 TF } else DocTermTF_slist.Add((Term + POS), 1); //該詞彙(term)不存在於termList中,則加入該新詞彙(term) // --- 紀錄出現在每一文件之詞彙有哪些,用於計算詞彙之DF if ((index = temp_list.IndexOf((Term + POS))) == -1) temp_list.Add((Term + POS)); //紀錄出現在每一文件之詞彙有哪些,用於計算詞彙之DF } } //TF詞頻比較 if (Doctermtf_list.Count != 0) { foreach (KeyValuePair<string, int> kvp in Doctermtf_list) { //KeyValuePair<string, int> x = new KeyValuePair<string, int>((string)obj.Key, (int)obj.Value); } } }//End of while每一文件 // ----記錄並計算最後一篇文件之詞彙 DF -------- if (temp_list.Count != 0) { foreach (string str in temp_list) //複製與計算出現在每一文件之詞彙的"DF" { //-- 計算詞彙之 DF -- if ((index = Term_DF_slist.IndexOfKey(str)) != -1) //在termList中找尋特定的Term,若Term不存在怎則傳回-1 { value = (int)Term_DF_slist.GetByIndex(index); value++; //該term存在,則term的出現次數加1 Term_DF_slist.SetByIndex(index, value); } else Term_DF_slist.Add(str, 1); //該詞彙(term)不存在於termList中,則加入該新詞彙(term) } temp_list.Clear(); //清除紀錄每一篇文件內之詞彙的SortedList } if (DocTermTF_slist.Count != 0) { foreach (DictionaryEntry obj in DocTermTF_slist) { KeyValuePair<string, int> x = new KeyValuePair<string, int>((string)obj.Key, (int)obj.Value); Doctermtf_list.Add(x); } DocTermTF_slist.Clear(); } }//End of using // --- 顯示詞彙列表 "term-tf" (未過濾前) --- StringBuilder SB = new StringBuilder(); //StringBuilder適合未知數目之字串動態串接,此處使用string物件則無法完成此大資料檔案的讀取串接 foreach (KeyValuePair<string, int> kvp in Doctermtf_list) SB.AppendLine(kvp.Key + " " + kvp.Value); rtb_p5_souce.Text = SB.ToString(); //顯示經詞彙挑選後資料 //}//End of if // ============================================== 依詞彙出現次數挑選過濾詞彙 ============================================== int tf = int.Parse(txt_p5_tf.Text); //過濾掉詞彙次數tf(txt_p5_tf.Text)以下之詞彙 List<KeyValuePair<string, int>> DoctermtfSelected_list = new List<KeyValuePair<string, int>>(); MaxTfValue = 0; foreach (KeyValuePair<string, int> kvp in Doctermtf_list) { if (kvp.Value >= tf || kvp.Value == 0) { DoctermtfSelected_list.Add(kvp); //建立"挑選過濾後之詞彙-詞彙頻率列表" if (kvp.Value > MaxTfValue) //找尋最大"詞彙頻率TF"值 MaxTfValue = kvp.Value; } if (kvp.Value > tf && Term_list.Contains(kvp.Key) == false) Term_list.Add(kvp.Key); //建立"挑選過濾後之全部詞彙列表" Term_list.Sort(); } // --- 顯示詞彙列表 "term-tf" (過濾後) --- Dictionary<string, int> termSelected_DF_dic = new Dictionary<string, int>(); //挑選過濾後之"詞彙總數"列表及詞彙之"文件頻df" rtb_p5_selected.Clear(); StringBuilder SB1 = new StringBuilder(); //StringBuilder適合未知數目之字串動態串接,此處使用string物件則無法完成此大資料檔案的讀取串接 StringBuilder SB2 = new StringBuilder(); foreach (KeyValuePair<string, int> kvp in DoctermtfSelected_list) { SB1.AppendLine(kvp.Key + " " + kvp.Value.ToString()); if (((index = Term_DF_slist.IndexOfKey(kvp.Key)) != -1) && (termSelected_DF_dic.ContainsKey(kvp.Key) == false)) //詞彙挑選後之"term-TF" 與 "term-DF"相互配對對應 { termSelected_DF_dic.Add((string)Term_DF_slist.GetKey(index), (int)Term_DF_slist.GetByIndex(index)); SB2.AppendLine((string)Term_DF_slist.GetKey(index) + " " + (int)Term_DF_slist.GetByIndex(index)); } } rtb_p5_selected.Text = SB1.ToString(); //顯示經詞彙挑選後資料 rtb_p5_termDF.Text = SB2.ToString(); //顯示經詞彙挑選後資料 /*/ ============================================= 計算詞彙的權重 "tf_idf" ============================================= StringBuilder SB3 = new StringBuilder(); Dictionary<string, double> termSelected_TDIDF_dic = new Dictionary<string, double>(); int TF = 0, DF = 0; double idf = 0.0, tf_idf = 0.0; foreach (KeyValuePair<string, int> kvp in DoctermtfSelected_list) { if (kvp.Key.Contains("<DocNo>") && (kvp.Value == 0)) { KeyValuePair<string, double> tag = new KeyValuePair<string, double>(kvp.Key, 0.0); DocTermtfidf_list.Add(tag); tf_idf = 0.0; } TF = kvp.Value; if (termSelected_DF_dic.TryGetValue(kvp.Key, out DF)) { idf = 1 / (double)DF; tf_idf = (TF / (double)MaxTfValue) * (Math.Log(1 / idf)); //基底=e } KeyValuePair<string, double> x = new KeyValuePair<string, double>(kvp.Key, tf_idf); DocTermtfidf_list.Add(x); SB3.AppendLine(kvp.Key + " " + tf_idf); } rtb_p5_tfidf.Text = SB3.ToString(); //顯示經詞彙挑選後之"詞彙與tfidf" // ---- 顯示經詞彙出現次數挑選後出現在每一篇文件中的所有詞彙 StringBuilder SB4 = new StringBuilder(); foreach (string str in Term_list) SB4.AppendLine(str); rtb_p5_TermList.Text = SB4.ToString(); }//End 0f Event Fuction */ // ============================================= 詞彙的權重都設為1 ============================================= StringBuilder SB3 = new StringBuilder(); Dictionary<string, double> termSelected_TDIDF_dic = new Dictionary<string, double>(); int TF = 0, DF = 0; double idf = 0.0, tf_idf = 0.0; foreach (KeyValuePair<string, int> kvp in DoctermtfSelected_list) { if (kvp.Key.Contains("<DocNo>") && (kvp.Value == 0)) { KeyValuePair<string, double> tag = new KeyValuePair<string, double>(kvp.Key, 0.0); DocTermtfidf_list.Add(tag); tf_idf = 0.0; } // TF = kvp.Value; if (termSelected_DF_dic.TryGetValue(kvp.Key, out DF)) { //idf = 1 / (double)DF; tf_idf = 1; //基底=e } KeyValuePair<string, double> x = new KeyValuePair<string, double>(kvp.Key, tf_idf); DocTermtfidf_list.Add(x); SB3.AppendLine(kvp.Key + " " + tf_idf); } rtb_p5_tfidf.Text = SB3.ToString(); //顯示經詞彙挑選後之"詞彙與tfidf" // ---- 顯示經詞彙出現次數挑選後出現在每一篇文件中的所有詞彙 StringBuilder SB4 = new StringBuilder(); foreach (string str in Term_list) SB4.AppendLine(str); rtb_p5_TermList.Text = SB4.ToString(); }//End 0f Event Fuction
public void TestSetByIndex() { SortedList sl1 = new SortedList(24); for (int i = 49; i>=0; i--) sl1.Add(100+i,i); try { sl1.SetByIndex(-1,77); Fail("sl.SetByIndex: ArgumentOutOfRangeException not caught, when key is out of range"); } catch (ArgumentOutOfRangeException) {} try { sl1.SetByIndex(100,88); Fail("sl.SetByIndex: ArgumentOutOfRangeException not caught, when key is out of range"); } catch (ArgumentOutOfRangeException) {} for(int i=5; i<25; i++) sl1.SetByIndex(i,-1); for(int i=0; i<5; i++) AssertEquals("sl.SetByIndex: set failed(1)",sl1[100+i],i); for(int i=5; i<25; i++) AssertEquals("sl.SetByIndex: set failed(2)",sl1[100+i],-1); for(int i=25; i<50; i++) AssertEquals("sl.SetByIndex: set failed(3)",sl1[100+i],i); }