Esempio n. 1
0
        private void Entropy(object sender, RoutedEventArgs e)
        {
            string text = RichText.GetText(OriginalText).Substring(0, RichText.GetText(OriginalText).Length - 2).ToLower();

            if (!checkText(text))
            {
                MessageBox.Show("Введите символ алфавита, не пустую строку :)");
                return;
            }

            int[] alphCount = new int[alph.Length];
            int   n         = 0;

            double[] alphChance = new double[alph.Length];
            double   entropy    = 0.0D;


            string rplChars = " ,.!?:-;()[]'\"";

            foreach (var chr in rplChars)
            {
                text = text.Replace(chr.ToString(), "");
            }
            string binText = "";
            var    textChr = Encoding.ASCII.GetBytes(text);

            foreach (int chr in textChr)
            {
                binText += Convert.ToString(chr, 2).PadLeft(8, '0');
            }

            for (int i = 0; i < binText.Length; i++)
            {
                for (int j = 0; j < alph.Length; j++)
                {
                    if (binText[i] == alph[j])
                    {
                        alphCount[j]++;
                        n++;
                    }
                }
            }

            for (int i = 0; i < alphChance.Length; i++)
            {
                alphChance[i] = (double)alphCount[i] / n;
            }

            for (int i = 0; i < alphChance.Length; i++)
            {
                if (alphChance[i] != 0)
                {
                    entropy += alphChance[i] * Math.Log(alphChance[i], 2);
                }
            }

            TextEntropy.Text = (entropy * (-1)).ToString();
        }