-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncode.java
More file actions
224 lines (183 loc) · 6.63 KB
/
Encode.java
File metadata and controls
224 lines (183 loc) · 6.63 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import java.util.ArrayList ;
import java.util.Scanner ;
import java.util.Collections ;
import java.util.Comparator ;
public class Encode
{
private ArrayList<Node <Character, Integer>> __frequencies = new ArrayList<Node <Character, Integer>> () ;
public static void main(String[] args)
{
Encode encode = new Encode () ;
FileOperations operation = new FileOperations () ;
Scanner reference = operation.openFile("file.txt");
String line = null;
/**
* This loop read original file line by line and passes the line to calculateFrequency
* function to find frequency for each character.
*/
while ((line = operation.readFile (reference)) != null)
{
encode.__calculateFrequency(line);
}
encode.__sort();
encode.__calculatePossibilities();
encode.__generateCodes (encode.__frequencies) ;
/**
* After the generating codes operation encoding function encodes file
* and write codes to the encoded file.
*/
reference = operation.openFile ("file.txt") ;
while ((line = operation.readFile (reference)) != null)
{
operation.writeFile (encode.__encoding (line) , "encoded.txt") ;
}
// Frequency list is written to the codes file as array list.
operation.writeCodes (encode.__frequencies , "codes.bin") ;
System.out.println("File was encoded.!");
}
private void __calculateFrequency(String line)
{
/**
* These two loops goes through the line and compares each character with
* the other character in the array list. The character's value is increased
* if this character in array list already or the character is created and added to the
* array list if is not in the array list yet.
*/
for (int i = 0; i < line.length(); i++)
{
boolean flag = true;
for (Node <Character, Integer> n : this.__frequencies)
{
if (n.getKey ().equals (line.charAt (i)))
{
n.setValue (n.getValue () + 1) ;
flag = false;
break;
}
}
if (flag)
{
this.__frequencies.add(new Node<Character, Integer>(line.charAt(i), 1));
}
}
}
private void __sort()
{
Collections.sort
(
this.__frequencies,
new Comparator <Node <Character, Integer>> ()
{
@Override
public int compare(Node <Character, Integer> first, Node <Character, Integer> second)
{
if (first.getValue () > second.getValue ())
{
return -1;
}
else if (first.getValue () < second.getValue ())
{
return 1;
}
else
{
return 0;
}
}
}
);
}
private void __calculatePossibilities ()
{
/**
* This function calculates possiblity for each character in array list.
*/
int amount_of_letter = 0;
boolean counter = true;
for (int i = 0; i < 2; i++)
{
for (Node<Character, Integer> n : this.__frequencies)
{
if (counter)
{
amount_of_letter += n.getValue();
}
else
{
n.setPossibility((double) n.getValue() / amount_of_letter);
}
}
counter = false;
}
}
private void __generateCodes (ArrayList <Node <Character , Integer>> list)
{
/**
* This function creates coding tree recursively and then generate codes according to
* each character's possibility.
*/
if (list.size () > 1)
{
ArrayList <Node <Character , Integer>> left = new ArrayList <Node <Character , Integer>> () ;
ArrayList <Node <Character , Integer>> right = new ArrayList <Node <Character , Integer>> () ;
class Helper
{
public double sum (ArrayList <Node <Character , Integer>> rest_of_list , int start)
{
double result = 0d ;
for (int i = start ; i < rest_of_list.size () ; i ++)
{
result += rest_of_list.get (i).getPossibility () ;
}
return result ;
}
}
Helper helper = new Helper () ;
int stop = 1 ;
while (stop < list.size ())
{
double total_possibility = 0d ;
for (int i = 0 ; i < stop ; i ++)
{
total_possibility += list.get (i).getPossibility () ;
}
if (Math.abs (total_possibility - helper.sum (list , stop)) <= 0.2)
{
for (int i = 0 ; i < stop ; i ++)
{
list.get (i).setCode (list.get (i).getCode () + '0') ;
left.add (list.get (i)) ;
}
for (int i = stop ; i < list.size () ; i ++)
{
list.get (i).setCode (list.get (i).getCode () + '1') ;
right.add (list.get (i)) ;
}
break ;
}
stop ++ ;
}
this.__generateCodes (left) ;
this.__generateCodes (right) ;
}
}
private String __encoding (String line)
{
/**
* This function encodes line according as codes that generated before
* and return enocded line for writing to the encoded file.
*/
String encoded = "" ;
for (int i = 0 ; i < line.length () ; i ++)
{
for (Node <Character , Integer> n : this.__frequencies)
{
if (line.charAt (i) == n.getKey ())
{
encoded += n.getCode () ;
}
}
}
return encoded ;
}
}