Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            EntropyCounter.ReadFile();

            double h1 = EntropyCounter.getH1();

            Console.WriteLine("Symbols   = " + EntropyCounter.sym);

            Console.WriteLine("H1: " + h1);
            //HuffmanAndShenon.FindOneHuffman();
            //Console.WriteLine("_______-----------________________-----------_______________");
            //Console.WriteLine("H1: " + h1);
            //Console.ReadKey();
            //HuffmanAndShenon.FindOneShennon();
            HuffmanAndShenon.symbols = new List <Symbol>();
            double h2 = EntropyCounter.getH2();

            //Console.WriteLine("H1: " + h1);
            //Console.WriteLine("H2: " + h2);
            //Console.WriteLine("_______-----------________________-----------_______________");
            //Console.ReadKey();
            HuffmanAndShenon.FindOneHuffman();
            Console.WriteLine("_______-----------________________-----------_______________");
            Console.WriteLine("H2: " + h2);
            Console.ReadKey();
            HuffmanAndShenon.FindOneShennon();
            Console.WriteLine("H2: " + h2);
            Console.WriteLine("H3: " + EntropyCounter.getH3());
        }
Ejemplo n.º 2
0
        public static double getH1()
        {
            long size = 0;

            h1CountMap = new Dictionary <char, long>();

            char current;
            long value;

            foreach (String line in lines)
            {
                for (int i = 0; i < line.Length; i++)
                {
                    size++;

                    current = line[i];

                    if (!h1CountMap.TryGetValue(current, out value))
                    {
                        h1CountMap.Add(current, 1L);
                    }
                    else
                    {
                        h1CountMap.Remove(current);
                        h1CountMap.Add(current, value + 1);
                    }
                }
            }

            double p, logP;
            double result = 0;

            foreach (KeyValuePair <char, long> entry in h1CountMap)
            {
                p = (double)entry.Value / size;
                Symbol sym = new Symbol(entry.Key, p);
                HuffmanAndShenon.addSymbol(sym);
                logP = (double)getLog2(p);

                result += p * logP;
            }
            sym = size;

            return(Math.Abs(result));
        }
Ejemplo n.º 3
0
        public static double getH2()
        {
            double result = 0;

            char previous = '\0';
            char current;
            long size = 0;

            Dictionary <char, TwoCharacters> map = new Dictionary <char, TwoCharacters>();

            foreach (String line in lines)
            {
                for (int i = 0; i < line.Length; i++)
                {
                    current = line.Substring(i, 1).ToCharArray()[0];
                    size++;

                    if (previous == '\0')
                    {
                        previous = current;
                    }
                    else
                    {
                        TwoCharacters twoCharacters;
                        map.TryGetValue(current, out twoCharacters);
                        if (twoCharacters == null)
                        {
                            twoCharacters = new TwoCharacters(current);
                            map.Add(current, twoCharacters);
                        }
                        twoCharacters.putCharacter(previous);

                        previous = current;
                    }
                }
            }

            double mainP, temp, p, logP;

            string first, para = "";

            foreach (KeyValuePair <char, TwoCharacters> entry in map)
            {
                first = "" + entry.Key;
                Dictionary <char, long> characterLongMap = entry.Value.getCharacterLongMap();
                temp = 0;
                long tc;
                h1CountMap.TryGetValue(entry.Value.getCurrent(), out tc);
                mainP = (double)tc / size;

                String key;
                foreach (KeyValuePair <char, long> longEntry in characterLongMap)
                {
                    para = longEntry.Key + first;
                    //para = first + longEntry.Key ;
                    p = ((double)longEntry.Value / (size - 1)) / mainP;
                    Symbol pair = new Symbol(para, p);
                    HuffmanAndShenon.addSymbol(pair);
                    logP  = getLog2(p);
                    temp += Math.Abs(logP * p);
                }


                result += mainP * temp;
            }

            return(Math.Abs(result));
        }