I'm trying to compute the Chi-Square values for a list of keywords.
It is strange that the chi-square values (sorted in alphabetical order of the keywords) are cumulative. I can't find where the problem lies after some troubleshooting. Hope that someone is able to help.
Please let me know if you need further clarifications on the code below. Thanks.
//ptxt is a SortedDictionary list <string keyword, string plaintext>foreach (string k in ptxt.Keys) { totalLetChiSq = 0; int pStrLen = 0; int count = 0; string kUpper = ptxt[k].ToUpperInvariant(); //Validate if each plaintext character matches the language letter frequency foreach (char i in kUpper) { for (var j = 0; j < alphaArr.Length; j++) { if (i == alphaArr[j]) { pStr += i; //pStr contains only characters that are present in the language letter frequency pStrLen++; //Total no of characters for pStr } } } ptxtArrList = letterCount(pStr); //Letter Frequency count for pStr //Compute Chi-Square values for all the keywords foreach (char i in ptxtArrList.Keys) { letChiSq.Clear(); numerator = ptxtArrList[i] - (langFreq[count] * pStrLen); denominator = langFreq[count] * pStrLen; cs = (Math.Pow(Convert.ToDouble(numerator), 2)) / Convert.ToDouble(denominator); letChiSq.Add(i, cs); // Store chi-sq value of each letter totalLetChiSq += letChiSq[i]; // Compute chi-square value for a keyword count++; //increment counter for the next language letter frequency } try { KeysChiSq.Add(k, totalLetChiSq); } catch { MessageBox.Show("End of keyword file!"); break; } MessageBox.Show("Keyword " + k + " has a Chi-Square value of " + KeysChiSq[k].ToString()); } }