static void Main(string[] args) { string input = File.ReadAllText(@"C:\TOC\book.txt"); List <KeyValuePair <char, double> > charsAndProbs = input.GroupBy(c => c) .Select(c => new KeyValuePair <char, double>(c.Key, c.Count())) .OrderByDescending(k => k.Value).ToList(); Dictionary <char, string> codes = new Dictionary <char, string>(); Fano(charsAndProbs, codes, 0, charsAndProbs.Count - 1); foreach (KeyValuePair <char, string> a in codes) { Console.WriteLine($"{a.Key}: {a.Value}"); } StringBuilder builder = new StringBuilder(); foreach (char symbol in input) { builder.Append(codes[symbol]); } string encoded = builder.ToString(); Console.WriteLine("Encoded:"); Console.WriteLine(encoded); StringBuilder decoder = new StringBuilder(); for (int i = 0; i < encoded.Length; i++) { foreach (KeyValuePair <char, string> keyValuePair in codes) { if (encoded.Substring(i, keyValuePair.Value.Length) == keyValuePair.Value) { decoder.Append(keyValuePair.Key); i += keyValuePair.Value.Length - 1; break; } } } string decoded = decoder.ToString(); Console.WriteLine("Decoded:"); Console.WriteLine(decoded); Console.WriteLine($"Alphabit: {charsAndProbs.Count}"); Console.WriteLine($"Symbols count: {input.Length}"); Console.WriteLine( $"Entropy of decoded text: {EntropyCalulate.GetEntropy(decoded)}" + $"{Environment.NewLine}" + $"Entropy of encoded text: {EntropyCalulate.GetEntropy(encoded)}"); Write(encoded, input); Console.ReadKey(); }
static void Main(string[] args) { DateTimeOffset start = DateTimeOffset.Now; string input = "айм блю дабуди дабудай дабуди дабудай дабуди дабудай"; var d = input.GroupBy(c => c).ToDictionary(c => c.Key, c => c.Count()).OrderBy(c => c.Value).ToDictionary(c => c.Key, c => c.Value); foreach (var e in d) { Console.WriteLine(e); } Console.ReadLine(); //string input = "aaaaabbbccd"; HuffmanTree huffmanTree = new HuffmanTree(); // Encode BitArray encoded = huffmanTree.Encode(input); //Console.Write("Encoded: "); StringBuilder builder = new StringBuilder(); foreach (bool bit in encoded) { builder.Append(Convert.ToSByte(bit)); } Console.WriteLine(builder.ToString()); Console.ReadLine(); FileStream stream = File.Create(@"C:\TOC\result_symbol.bin"); BinaryWriter writer = new BinaryWriter(stream); byte[] arr = new byte[encoded.Length / 8 + (encoded.Length % 8 == 0 ? 0 : 1)]; encoded.CopyTo(arr, 0); writer.Write(arr); stream.Flush(); writer.Flush(); stream.Close(); writer.Close(); writer.Dispose(); byte[] file = File.ReadAllBytes(@"C:\TOC\result_symbol.bin"); Console.WriteLine($"File size delenyi na count of symbols {(double)(file.Length * 8) / (double)input.Length}");; Console.WriteLine(EntropyCalulate.GetEntropy(input)); // Decode string decoded = huffmanTree.Decode(encoded); Console.WriteLine(start - DateTimeOffset.Now); Console.ReadLine(); }
static void Main(string[] args) { DateTimeOffset start = DateTimeOffset.Now; string input = File.ReadAllText(@"C:\TOC\book.txt"); //string input = "aaaaabbbccd"; if (input.Length % 2 == 1) { input += " "; } HuffmanTree huffmanTree = new HuffmanTree(); // Encode BitArray encoded = huffmanTree.Encode(input); //Console.Write("Encoded: "); //StringBuilder builder = new StringBuilder(); //foreach (bool bit in encoded) //{ // builder.Append(Convert.ToSByte(bit)); //} FileStream stream = File.Create(@"C:\TOC\result_syllable.bin"); BinaryWriter writer = new BinaryWriter(stream); byte[] arr = new byte[encoded.Length / 8 + (encoded.Length % 8 == 0 ? 0 : 1)]; encoded.CopyTo(arr, 0); writer.Write(arr); stream.Flush(); writer.Flush(); stream.Close(); writer.Close(); writer.Dispose(); byte[] file = File.ReadAllBytes(@"C:\TOC\result_syllable.bin"); Console.WriteLine($"File size delenyi na count of symbols {(double)(file.Length * 8) / (double)input.Length}"); EntropyCalulate.GetEntropy(input); // Decode string decoded = huffmanTree.Decode(encoded); //Console.WriteLine("Decoded: " + decoded); Console.WriteLine(start - DateTimeOffset.Now); Console.ReadLine(); }
static void Main(string[] args) { string input = "айм блю дабуди дабудай дабуди дабудай дабуди дабудай"; if (input.Length % 2 == 1) { input += " "; } //List<KeyValuePair<char, double>> charsAndProbs = input.GroupBy(c => c) // .Select(c => new KeyValuePair<char, double>(c.Key, c.Count())) // .OrderByDescending(k => k.Value).ToList(); List <KeyValuePair <string, double> > list = new List <KeyValuePair <string, double> >(); for (int i = 0; i < input.Length - 2; i += 2) { string key = input.Substring(i, 2); list.Add(new KeyValuePair <string, double>(key, 1)); } list = list.GroupBy(k => k.Key) .Select(c => new KeyValuePair <string, double>(c.Key, c.Count())) .OrderByDescending(k => k.Value).ToList(); Dictionary <string, string> codes = new Dictionary <string, string>(); Fano(list, codes, 0, list.Count - 1); foreach (KeyValuePair <string, string> a in codes) { Console.WriteLine($"{a.Key}: {a.Value}"); } StringBuilder builder = new StringBuilder(); for (int i = 0; i < input.Length - 2; i += 2) { string key = input.Substring(i, 2); builder.Append(codes[key]); } string encoded = builder.ToString(); Console.WriteLine("Encoded:"); Console.WriteLine(encoded); StringBuilder decoder = new StringBuilder(); for (int i = 0; i < encoded.Length; i++) { try { foreach (KeyValuePair <string, string> keyValuePair in codes) { if (encoded.Substring(i, keyValuePair.Value.Length) == keyValuePair.Value) { decoder.Append(keyValuePair.Key); i += keyValuePair.Value.Length - 1; break; } } } catch { } } string decoded = decoder.ToString(); Console.WriteLine("Decoded:"); Console.WriteLine(decoded); Console.WriteLine($"Alphabit: {list.Count}"); Console.WriteLine($"Symbols count: {input.Length}"); Console.WriteLine( $"Entropy of decoded text: {EntropyCalulate.GetEntropy(decoded)}" + $"{Environment.NewLine}" + $"Entropy of encoded text: {EntropyCalulate.GetEntropy(encoded)}"); Write(encoded, input); Console.ReadKey(); }