-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTextEncoder.cpp
More file actions
124 lines (110 loc) · 3.14 KB
/
TextEncoder.cpp
File metadata and controls
124 lines (110 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "TextEncoder.h"
node::node(string p_char, int p_freq)
{
freq = p_freq;
m_char = p_char;
left = right = NULL;
}
void Encoder::ConstructMap(node *n, string s)
{
if (n->left == NULL && n->right == NULL)
{
codeMap[n->m_char[0]] = s;
s = "";
return;
}
ConstructMap(n->left, s + "0");
ConstructMap(n->right, s + "1");
}
string Encoder::encodeStringFromFile(string inputPath, string outputPath)
{
StringCompressor compressor;
string inpuText;
ifstream uncompressedFile;
uncompressedFile.open(inputPath);
if (uncompressedFile.is_open())
{
char c;
while (uncompressedFile.get(c))
inpuText.push_back(c);
}
else
{
cout << "Couldn't open the file" << endl;
return 0;
}
uncompressedFile.close();
// priority queue custom compare function
// cout << inpuText;
struct compare
{
bool operator()(node *l, node *r)
{
return (l->freq > r->freq);
}
};
string copyText = inpuText;
priority_queue<node *, vector<node *>, compare> HuffTree;
string encodedBits;
int textLen = inpuText.length();
//cout << "Text : " << inpuText << endl;
//cout << "Number of text bits : " << textLen << endl;
//Getting frequencies of chars
for (int i = 0; i < inpuText.length(); i++)
{
int freq = 1;
for (int j = i + 1; j < inpuText.length(); j++)
{
if (inpuText[i] == inpuText[j])
{
freq++;
inpuText.erase(inpuText.begin() + j);
j--;
}
}
string tmp;
tmp.push_back(inpuText[i]);
HuffTree.push(new node(tmp, freq));
tmp.clear();
}
// cout << "Tree : " << endl;
//Contructing Huffman Tree
int n = HuffTree.size();
for (int i = 0; i < n - 1; i++)
{
node *n1 = HuffTree.top();
HuffTree.pop();
node *n2 = HuffTree.top();
HuffTree.pop();
node *new_node = new node(n1->m_char + n2->m_char, n1->freq + n2->freq);
new_node->left = n1;
new_node->right = n2;
// cout << new_node->freq << " " << new_node->m_char << endl;
// cout << "Left->" << new_node->left->m_char << new_node->left->freq << "\t" <<
// "Right->" << new_node->right->m_char << new_node->right->freq << endl;
HuffTree.push(new_node);
}
// cout << "Map : " << endl;
// Traversing Huffman Tree
ConstructMap(HuffTree.top(), "");
ofstream compressedFile(outputPath);
// Getting encoded bits
for (int i = 0; i < textLen; i++)
encodedBits += codeMap[copyText[i]];
//Writing encoded bits to a file
//compressedFile<<encodedBits<<endl;
compressedFile << compressor.binaryStreamtoASCII(encodedBits);
compressedFile << "END" << endl;;
//Writing encoding map to a file
for (std::map<char, string>::iterator it = codeMap.begin(); it != codeMap.end(); ++it)
{
if (it->first != '\n') compressedFile << it->first << '\t';
else compressedFile << "\\n" << '\t';
compressedFile << it->second <<endl;
// cout << it->first << " => " << it->second << '\n';
}
compressedFile.close();
// cout << "encoded Bits : " << endl << encodedBits << endl;
// cout << "Number of encoded bits : " << encodedBits.length() << endl;
return encodedBits;
}