Skip to content

Instantly share code, notes, and snippets.

@olecksamdr
Last active October 27, 2016 21:32
Show Gist options
  • Save olecksamdr/ae80f41aaa8b8c865b6d70eab2770b84 to your computer and use it in GitHub Desktop.
Save olecksamdr/ae80f41aaa8b8c865b6d70eab2770b84 to your computer and use it in GitHub Desktop.
// Необхідність стиску зображень
//C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace arithmeticCompession
{
class arithmeticCompession
{
// ставить у відповідність літері її індекс у списку
static SortedDictionary<string, int> simbolIndex = new SortedDictionary<string, int>();
// містить число входжень в текст кожної літери
static SortedDictionary<string, float> simbolsCount = new SortedDictionary<string, float>();
// містить імовірності входження кожної літери
static SortedDictionary<string, float> possibilities = new SortedDictionary<string, float>();
// права межа інтервалу кожної літери
static List<float> frecuency = new List<float>();
// fulll text characters count
static int fullTextLength = 0;
static void printDictionary(SortedDictionary<string, float> d)
{
Console.WriteLine("in Dictionary");
foreach (KeyValuePair<string, float> item in d)
{
Console.WriteLine(item.Key + " " + item.Value);
}
}
static void printIntervals()
{
Console.WriteLine();
Console.WriteLine("possibilities");
foreach (KeyValuePair<string, float> item in possibilities)
{
Console.WriteLine(item.Key + " " + item.Value);
}
Console.WriteLine();
Console.WriteLine("Intervals");
for (int i = 0; i < frecuency.Count - 1; i++)
{
Console.WriteLine(simbolIndex.ElementAt(i).Key + " [ " + frecuency[i] + ", " + frecuency[i + 1] + " )");
}
}
static SortedDictionary<string, float> countCharacters(string pathToFile)
{
StreamReader fileStream = new StreamReader(pathToFile);
// Dictionary<string, int> simbolsCount = new Dictionary<string, int>();
string simbol;
fullTextLength = 1;
int intSimbol = fileStream.Read();
while (intSimbol != -1)
{
fullTextLength++;
simbol = Convert.ToString((Convert.ToChar(intSimbol)));
if (simbolsCount.ContainsKey(simbol))
simbolsCount[simbol] += 1;
else
simbolsCount[simbol] = 1;
intSimbol = fileStream.Read();
}
simbolsCount["я"] = 1;
fileStream.Close();
return simbolsCount;
}
static void addToList(SortedDictionary<string, float> d)
{
int index = 1;
foreach (KeyValuePair<string, float> item in d)
{
simbolIndex[item.Key] = index;
index++;
}
}
static void calcPossibilities(SortedDictionary<string, float> simbolsCount, int fullTextLength)
{
float posible = 0;
foreach (KeyValuePair<string, float> item in simbolsCount)
{
posible = item.Value / fullTextLength;
Console.WriteLine("item.Key = " + item.Key + " item.Value = " + item.Value);
possibilities.Add(item.Key, posible);
}
}
static List<float> getFrecuency(SortedDictionary<string, float> possibilities)
{
float leftBoundary = 0;
frecuency.Add(0);
int index = 1;
foreach (KeyValuePair<string, float> item in possibilities)
{
leftBoundary += item.Value;
frecuency.Add(leftBoundary);
index++;
}
return frecuency;
}
static float encode(string inputFilePath, string outputFilePath)
{
countCharacters(inputFilePath);
addToList(simbolsCount);
calcPossibilities(simbolsCount, fullTextLength);
getFrecuency(possibilities);
printIntervals();
float low = 0.0f;
float high = 1.0f;
float range;
StreamReader fileStream = new StreamReader(inputFilePath);
string simbol;
int intSimbol = fileStream.Read();
simbol = Convert.ToString(Convert.ToChar(intSimbol));
//Console.WriteLine("simbol " + simbol);
while (intSimbol != -1)
{
range = high - low;
simbol = Convert.ToString(Convert.ToChar(intSimbol));
high = low + range * frecuency[simbolIndex[simbol]];
low = low + range * frecuency[simbolIndex[simbol] - 1];
intSimbol = fileStream.Read();
Console.WriteLine();
Console.WriteLine("Processed simbol: " + simbol);
Console.WriteLine("interval: [" + low + "; " + high + ")");
}
float code = (low + high) / 2;
writeToFile(outputFilePath, possibilities, code);
Console.WriteLine();
Console.WriteLine("Code: " + code);
return code;
}
static float readEncodedFile(string pathToFile)
{
possibilities = new SortedDictionary<string, float>();
FileStream fs = new FileStream(pathToFile, FileMode.OpenOrCreate);
BinaryReader br = new BinaryReader(fs);
int count = br.ReadInt32();
Console.WriteLine("COUNT " + count);
string key;
float value;
// try {
for (; count >= 1; count--)
{
key = Convert.ToString(br.ReadChar());
value = br.ReadSingle();
Console.WriteLine("key: " + key + " value: " + value);
possibilities.Add(key, value);
}
//} catch(Exception e) {
//}
float code = br.ReadSingle();
br.Close();
fs.Close();
return code;
}
static void decode(string inputFilePath, string outputFilePath)
{
string r = "";
float code = readEncodedFile(inputFilePath);
addToList(possibilities);
getFrecuency(possibilities);
float low = 0.0f;
float heigh = 1.0f;
float range = 0;
StreamWriter outputStream = new StreamWriter(outputFilePath);
string simbol = simbolIndex.ElementAt(0).Key;
int index = 0;
//Console.WriteLine("SIMBOL: " + simbol);
// @ - EOF character
while (simbol != "я")
{
do
{
simbol = simbolIndex.ElementAt(index).Key;
Console.WriteLine();
Console.WriteLine("try simbol: " + simbol);
Console.WriteLine(frecuency[simbolIndex[simbol] - 1] + " <= " + (code - low) / (heigh - low) + " && " + (code - low) / (heigh - low) + " < " + frecuency[simbolIndex[simbol]]);
Console.WriteLine();
index++;
} while (((frecuency[simbolIndex[simbol] - 1] <= (code - low) / (heigh - low)) && ((code - low) / (heigh - low) > frecuency[simbolIndex[simbol]])));
Console.WriteLine();
Console.WriteLine("code: "+ code);
Console.WriteLine();
//code = (code - low) / (heigh - low);
range = heigh - low;
heigh = low + range * frecuency[simbolIndex[simbol]];
low = low + range * frecuency[simbolIndex[simbol] - 1];
// heigh = low + range * frecuency[simbolIndex[simbol] - 1];
// low = low + range * frecuency[simbolIndex[simbol]];
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("SIMBOL: " + simbol);
Console.ResetColor();
Console.WriteLine("HIGHT: " + heigh);
Console.WriteLine("Low: " + low);
index = 0;
r += simbol;
//Console.WriteLine(r);
}
outputStream.Write(r);
outputStream.Close();
}
static void writeToFile(string path, SortedDictionary<string, float> possibilities, float code)
{
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(Convert.ToInt32(possibilities.Count()));
foreach (KeyValuePair<string, float> item in possibilities)
{
bw.Write(Convert.ToChar(item.Key[0]));
bw.Write((float)item.Value);
}
bw.Write((float)code);
bw.Close();
fs.Close();
}
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Ussage: acc.exe {d[ecode]|e[ncode]} <infile> [<outfile>]");
return;
}
string toDo = args[0];
string readFrom = args[1];
string writeTo = (args.Length > 2) ? args[2] : args[1] + ".out";
if (toDo[0] == 'd')
decode(readFrom, writeTo);
else
encode(readFrom, writeTo);
//printDictionary(possibilities);
//foreach (double i in frecuency)
//{
// Console.WriteLine(i);
//}
//Console.WriteLine("fffff " + frecuency[1]);
//foreach (KeyValuePair<string, int> i in simbolIndex)
//{
// Console.WriteLine(i.Key + " " + i.Value);
//}
// encode(readFrom, writeTo);
//
// Console.WriteLine(Convert.ToString(code);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment