Beispiel #1
0
        private void InitData(string fileName)
        {
            var lettersCount = new Dictionary <char, double>();

            _trgetBigrams = new double[_alphabet.Length, _alphabet.Length];

            for (int i = 0; i < _alphabet.Length; i++)
            {
                for (int j = 0; j < _alphabet.Length; j++)
                {
                    _trgetBigrams[i, j] = 0;
                }
            }

            var text      = ReadText(fileName);
            var textMaker = new TextMaker();

            text = textMaker.ProcessText(text, _alphabet);

            for (int i = 0; i < _alphabet.Length; i++)
            {
                lettersCount.Add(_alphabet[i], 0);
            }

            for (int i = 0; i < text.Length; i++)
            {
                lettersCount[text[i]]++;

                if (i != text.Length - 1)
                {
                    _trgetBigrams[GetIndex(text[i]), GetIndex(text[i + 1])]++;
                }
            }

            for (int i = 0; i < _alphabet.Length; i++)
            {
                for (int j = 0; j < _alphabet.Length; j++)
                {
                    _trgetBigrams[i, j] /= text.Length - 1;
                }
            }

            _lettersOrderByCount = lettersCount.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value).Keys.ToList();
        }
Beispiel #2
0
        public string EncodeByKey(string text, string key)
        {
            var textMaker = new TextMaker();

            text = textMaker.ProcessText(text, _alphabet);

            for (int i = 0; i < _alphabet.Length; i++)
            {
                if (key.IndexOf(_alphabet[i]) < 0)
                {
                    key = key + _alphabet[i];
                }
            }

            var sb = new StringBuilder();

            for (int i = 0; i < text.Length; i++)
            {
                sb.Append(_alphabet[key.IndexOf(text[i])]);
            }

            return(sb.ToString());
        }