public AnsCodingTable(FrequencyDictionary <int> observedFrequencies, int denominator = -1, byte[] encryptionKey = null)
        {
            if (denominator <= 0)
            {
                denominator = DEFAULT_DENOMINATOR;
            }

            Denominator         = (int)observedFrequencies.TotalFrequency;
            FrequencyDictionary = new Dictionary <int, Fraction>();
            foreach (FrequencyStorage <int> feature in observedFrequencies.GetSortedFrequencies())
            {
                FrequencyDictionary[feature.ContainedObject] = new Fraction(feature.Frequency, Denominator);
            }
            List <Fraction> toSimplify = new List <Fraction>();

            foreach (KeyValuePair <int, Fraction> pair in FrequencyDictionary)
            {
                toSimplify.Add(pair.Value);
            }
            toSimplify.Sort(Fraction.SORTER);
            int sharedDenominator = denominator;

            try
            {
                Fraction.ScaleFractionsToDenominator(toSimplify, sharedDenominator);
            } catch (ArgumentOutOfRangeException e)
            {
                throw new ArgumentException("Denominator insufficient to construct table without losing frequency fidelity. Either reduce the block size, or increase the denominator", e);
            }
            Streamer    = new CodingTableStreamer(FrequencyDictionary);
            Denominator = toSimplify[0].Denominator;
            GenerateTable(encryptionKey);
        }
Ejemplo n.º 2
0
        public void Test(string text, Dictionary <string, int> expectedResult)
        {
            var actualResult1 = FrequencyDictionary.GetFrequenciesAtText(text);
            var actualResult2 = FrequencyDictionary.GetFrequenciesAtTextSecondVersion(text);

            Assert.That(actualResult1, Is.EqualTo(expectedResult));
            Assert.That(actualResult2, Is.EqualTo(expectedResult));
        }
Ejemplo n.º 3
0
        RankingDictionary <int> GetCandidateRanking(List <SudokuCell> block)
        {
            FrequencyDictionary <int> candidateFrequency = new FrequencyDictionary <int>();

            foreach (SudokuCell cell in block)
            {
                candidateFrequency.AddRange(cell.Candidates.ToArray());
            }
            return(candidateFrequency.GetRankingDictionary());
        }
Ejemplo n.º 4
0
        public void AddTest(int result, params string[] values)
        {
            var frequencyDictionary = new FrequencyDictionary <string>();

            foreach (var value in values)
            {
                frequencyDictionary.Add(value);
            }

            Assert.AreEqual(result, frequencyDictionary.GetValue(values.First()));
        }
        private void EncodeBlock(int blockContentLength, int targetDenominator)
        {
            Output.WriteLong(blockContentLength, 23);

            FrequencyDictionary <int> frequencies = new FrequencyDictionary <int>();

            for (int i = 0; i < blockContentLength; i++)
            {
                frequencies.AddData(Block[i]);
            }
            AnsCodingTable blockTable = new AnsCodingTable(frequencies, targetDenominator);

            blockTable.WriteTable(Output);
            WriteEncoding(blockContentLength, blockTable);
        }
        public void EncodeBlock(int[] blockIn, int count, int targetDenominator, byte[] encryptionKey = null)
        {
            Output.WriteLong(count, 23);
            Block = blockIn;
            FrequencyDictionary <int> frequencies = new FrequencyDictionary <int>();
            int offsetCount = count;

            for (int i = 0; i < offsetCount; i++)
            {
                frequencies.AddData(Block[i]);
            }
            AnsCodingTable blockTable = new AnsCodingTable(frequencies, targetDenominator, encryptionKey);

            blockTable.WriteTable(Output);
            WriteEncoding(count, blockTable);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Кодирование исходного текста блочным способом c использованием метрики
        /// </summary>
        /// <param name="fileNameSource">Имя файла исходного текста</param>
        /// <param name="sizeBlock">Размкр блока</param>
        /// <returns>Закодированый текст блочным методом с использованием метрики, представленный в виде списка битов</returns>
        public List <bool> EncodeBlockWithMetrics(string fileNameSource, int sizeBlock,
                                                  out List <double> distanсeEuclide, out List <double> distanceChebyshev, out List <double> distanceCityBlock)
        {
            FrequencyDictionary frequencyDictionary = new FrequencyDictionary(fileNameSource);

            Dictionary <string, double> p = ComparisonVector.GetVectorZerosFromFrequencyDictionary(frequencyDictionary);
            Dictionary <string, double> q = ComparisonVector.GetVectorZerosFromFrequencyDictionary(frequencyDictionary);

            List <double> distanceE  = new List <double>();
            List <double> distanceC  = new List <double>();
            List <double> distanceCB = new List <double>();

            //Открываем поток для считывания с файла
            StreamReader fileRead      = new StreamReader(@fileNameSource, Encoding.Default);
            List <bool>  encodedSource = new List <bool>();
            //Считываем весь текст с файла
            string text = fileRead.ReadToEnd();
            //int indexText = 0;
            int indexBegin = 0;
            int indexEnd   = 0;

            //пока не конец текста
            while (indexEnd < text.Length)
            {
                //indexBegin = 0;
                //indexEnd = 0;
                List <string> listWords = new List <string>();
                //идём по тексту, пока не наберём слов количеством в размер блока и пока не конец текста
                for (int i = 0; (i < sizeBlock) && (indexEnd < text.Length); i++)
                {
                    //пока это буква увеличиваем индекс конца слова
                    while (indexEnd < text.Length && char.IsLetter(text[indexEnd]))
                    {
                        indexEnd++;
                    }
                    //если мы прошлись по слову
                    string word;
                    if (indexBegin != indexEnd)
                    {
                        word = text.Substring(indexBegin, indexEnd - indexBegin);
                    }
                    else //это знак препенания, пробел или знак табуляции
                    {
                        indexEnd++;
                        word = text.Substring(indexBegin, indexEnd - indexBegin);
                    }
                    indexBegin = indexEnd;
                    //добавляем слово в блок
                    listWords.Add(word);
                }
                //кодирум блок слов и добавляем получившиеся биты в список битов
                encodedSource.AddRange(EncodeBlock(listWords));

                //работа с растояниями
                q = ComparisonVector.GetVectorFrequencyFromBlock(frequencyDictionary, listWords);
                distanceE.Add(ComparisonVector.GetEuclideanDistance(p, q));
                distanceC.Add(ComparisonVector.GetChebyshevDistance(p, q));
                distanceCB.Add(ComparisonVector.GetCityBlockDistance(p, q));
                p = q;
            }

            //for (int i = 0; i < distanceE.Count; i++)
            //{
            //    Console.Write(distanceE[i].ToString() + " " +  distanceC[i].ToString() +
            //        " " + distanceCB[i].ToString() + "\n");
            //}

            distanсeEuclide   = distanceE;
            distanceChebyshev = distanceC;
            distanceCityBlock = distanceCB;

            //возращаем закодированный блок слов в виде списка бит
            return(encodedSource);
        }
Ejemplo n.º 8
0
 RankingDictionary<int> GetCandidateRanking(List<SudokuCell> block)
 {
     FrequencyDictionary<int> candidateFrequency = new FrequencyDictionary<int>();
     foreach(SudokuCell cell in block)
     {
         candidateFrequency.AddRange(cell.Candidates.ToArray());
     }
     return candidateFrequency.GetRankingDictionary();
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Returns distortion between necessary and calculated value.
 /// For example between theoretical and practical values.
 /// </summary>
 /// <param name="chain">
 /// The chain.
 /// </param>
 /// <param name="alphabet">
 /// The alphabet.
 /// </param>
 /// <returns>
 /// The <see cref="double"/>.
 /// </returns>
 public abstract double Distortion(ComplexChain chain, FrequencyDictionary alphabet);
Ejemplo n.º 10
0
 /// <summary>
 /// Returns the state of criterion. True, if everything is done, false - otherwise.
 /// </summary>
 /// <param name="chain">
 /// The chain.
 /// </param>
 /// <param name="alphabet">
 /// Chains alphabet.
 /// </param>
 /// <returns>
 /// The state of criterion.
 /// </returns>
 public abstract bool State(ComplexChain chain, FrequencyDictionary alphabet);
Ejemplo n.º 11
0
 /// <summary>
 ///  Updates data for computing a new value of the criterion.
 /// </summary>
 /// <param name="chain">
 /// A new chain.
 /// </param>
 /// <param name="alphabet">
 /// A new alphabet.
 /// </param>
 public void Renew(ComplexChain chain, FrequencyDictionary alphabet)
 {
     this.chain = chain;
     this.alphabet = alphabet;
 }
Ejemplo n.º 12
0
        public void GetTest(int result, string value)
        {
            var frequencyDictionary = new FrequencyDictionary <string>();

            Assert.AreEqual(result, frequencyDictionary.GetValue(value));
        }
Ejemplo n.º 13
0
        private void buttonRun_Click(object sender, EventArgs e)
        {
            //Сначала для классического алгритма
            if (textBoxOutC.Text != "")
            {
                labelOutС.Enabled   = true;
                textBoxOutC.Enabled = true;
                //получаем частотный словарь
                FrequencyDictionary frequencyDictionary = new FrequencyDictionary(openFileDialogInput.FileName);
                SizeDictionaryC             = frequencyDictionary.CountWord;
                textBoxSizeDictionaryС.Text = SizeDictionaryC.ToString() + "  слов";

                //построение дерева
                var         startTime   = System.Diagnostics.Stopwatch.StartNew();
                HuffmanTree huffmanTree = new HuffmanTree(frequencyDictionary);
                startTime.Stop();
                var resultTime = startTime.Elapsed.TotalSeconds;

                //string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                //        resultTime.Hours,
                //        resultTime.Minutes,
                //        resultTime.Seconds,
                //        resultTime.Milliseconds);
                textBoxTimeBuildTree.Text = resultTime.ToString();

                //Кодирование
                startTime = System.Diagnostics.Stopwatch.StartNew();
                huffmanTree.Encode(@openFileDialogInput.FileName, @textBoxOutC.Text);
                startTime.Stop();
                resultTime = resultTime + startTime.Elapsed.TotalSeconds;
                //elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                //        resultTime.Hours,
                //        resultTime.Minutes,
                //        resultTime.Seconds,
                //        resultTime.Milliseconds);
                textBoxSpeedEncodeС.Text = resultTime.ToString();

                //сбрасываем дерево
                huffmanTree = new HuffmanTree();

                //Декодируем
                startTime = System.Diagnostics.Stopwatch.StartNew();
                huffmanTree.Decode(@textBoxOutC.Text, @textBoxDecodeFileC.Text);
                startTime.Stop();
                resultTime = startTime.Elapsed.TotalSeconds;
                //elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                //        resultTime.Hours,
                //        resultTime.Minutes,
                //        resultTime.Seconds,
                //        resultTime.Milliseconds);
                textBoxSpeedDecodeС.Text = resultTime.ToString();

                //Получаем другие статитстики
                SizeFileEncodeC           = new FileInfo(@textBoxOutC.Text).Length;
                textBoxSizeCompressС.Text = SizeFileEncodeC.ToString() + "  байт";

                textBoxCoefficientCompressС.Text = ((SizeFileInput * 1.0) / SizeFileEncodeC).ToString();

                groupBoxClassicHuffmanAlgo.Enabled = true;
            }

            //Потом для адаптивного
            if (textBoxOutA.Text != "")
            {
                labelOutA.Enabled   = true;
                textBoxOutA.Enabled = true;

                HuffmanAdaptiveTree huffmanAdaptiveTree = new HuffmanAdaptiveTree();
                //Кодирование
                var startTime = System.Diagnostics.Stopwatch.StartNew();
                huffmanAdaptiveTree.Encode(@openFileDialogInput.FileName, @textBoxOutA.Text);
                startTime.Stop();
                var resultTime = startTime.Elapsed.TotalSeconds;
                //var elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                //        resultTime.Hours,
                //        resultTime.Minutes,
                //        resultTime.Seconds,
                //        resultTime.Milliseconds);
                textBoxSpeedEncodeA.Text = resultTime.ToString();

                //сбрасываем дерево
                huffmanAdaptiveTree = new HuffmanAdaptiveTree();
                huffmanAdaptiveTree.Reset();

                //Декодируем
                startTime = System.Diagnostics.Stopwatch.StartNew();
                huffmanAdaptiveTree.Decode(@textBoxOutA.Text, @textBoxDecodeFileA.Text);
                startTime.Stop();
                resultTime = startTime.Elapsed.TotalSeconds;
                //elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                //        resultTime.Hours,
                //        resultTime.Minutes,
                //        resultTime.Seconds,
                //        resultTime.Milliseconds);
                textBoxSpeedDecodeA.Text = resultTime.ToString();

                //Получаем другие статитстики
                SizeFileEncodeA           = new FileInfo(@textBoxOutA.Text).Length;
                textBoxSizeCompressA.Text = SizeFileEncodeA.ToString() + "  байт";

                textBoxCoefficientCompressA.Text = ((SizeFileInput * 1.0) / SizeFileEncodeA).ToString();

                groupBoxAdaptiveHuffmanAlgo.Enabled = true;
            }
        }