-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion1.java
455 lines (313 loc) · 13.3 KB
/
Question1.java
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author Ahsan
*
*/
public class Question1 {
public static void main(String args[]) throws IOException {
//Calling the Histogram method to print the histogram of the character occurence
Question1.HistogramAndProbability("cityData.dat");
// assinging the character with their bits obtained from the shano fano algorithn
char[] letters = {'r' , 'd' , 'e' , 'i' , 'a' ,'s' ,'o' ,'b' ,'l' ,'n' ,'f' ,'h' ,'t' ,'m' ,'g'};
String[] SFAssign = {"000", "001", "010", "0110", "01111","1000","1001","1010","1011","1100","1101","11100","11101","11110","11111"};
// String to encode / compress
String origStr = "abilondonibabirminghamronnleedsransheffieldrinbradfordsetbristoltesaleedsabirsheffieldmirbradfordbagirlondondinobirminghamhalleedssibsheffielddinaleedsharsheffieldmooresbradfordrambristolforestlondonthesabirminghambastileedsmattsheffielddaarbradfordtonibristoldresbradford";
char[] charOrigStr = origStr.toCharArray();
// Encoded sequence
String SFEncOrigStr = Encode(origStr, letters, SFAssign);
// To decompress the coded String and print out to the file
Question1.Decode(SFEncOrigStr, letters,SFAssign);
}
/**
*
* Method for Printing the occurence of string in a histogram with the probability
*
* @param file
* @throws java.io.FileNotFoundException
**/
public static void HistogramAndProbability(String file) throws FileNotFoundException, IOException {
// To read from input file and write to output file.
/**
* Buffer reader used to read the contents of the file which is being read
* PrintWrite is used to write to the output file saved as testing.dat
**/
BufferedReader fileIn = new BufferedReader(new
FileReader(file));
PrintWriter writeOut = new PrintWriter("HistogramWithProbability.dat");
//A try block used to catch any exceptions/
try{
// Constructing string builder for the file
StringBuilder stTextbd = new StringBuilder();
//assinging the String builder to the first line of the file being read
String stline = fileIn.readLine();
// while the stringbuilder is not null meaning that there is still contents to
// reed exectue the while loop;
// inside the while loop stTextbd appends the text i.e joins them together when the line is read
// insert a new line
while (stline != null) {
stTextbd.append(stline);
stTextbd.append("\n");
stline = fileIn.readLine();
}
// Verify that string from the input file has been assigned to
// variable stText
String readFile = stTextbd.toString();
readFile = removeAll(readFile);
// Convert strng to array of characters for sorting
// then user the sort method to sort the characters
char[] charArray = readFile.toCharArray();
Arrays.sort(charArray);
// going to be used to calculate the probability distribution
double dis = 0;
double Tdis = 0;
// Define two Array Lists to record unique characters
// and their occurrence
// we use this arraylist becasue we dont need to
// define a fixed size which is important as we dont know the amount of characters
List<Character> uniqueCharList = new ArrayList<Character>();
List<Integer> uniqueCharCount = new ArrayList<Integer>();
// Identify by going through the array of characters
// use this int variable to go throught the index of
// of the array
int k = 0;
// while the value of k is less than the value of the length of the file
// execute the loop
while (k < readFile.length()){
// Start with character at index k
char temp = charArray[k];
// add the character to the uniqueCharList Array
uniqueCharList.add(temp);
// Find the number of occurrence
// count is equal the length of the file - the length of the file when the
// value of temp is replaced with ""
// replace temp with "" in the readFile then get the length
// minus the readFile.length with the readFile with the replace temp length
int count = readFile.length() - readFile.replace(String.valueOf(temp), "").length();
uniqueCharCount.add(count);
Tdis += (double)count / (double)readFile.length();
dis = (double)count / (double)readFile.length();
writeOut.write(temp +" Occurs " + count + " Times " + star(count)+ "\n");
writeOut.write("Probability Disribution : " + String.format( "%.3f", dis ) + "\n");
writeOut.write("\n");
// Why do we increment k by count?
// we increment k by k + count becuase the char are in order
// and count is the number of those characters therefore we have to
// incremnt using the count to get the next value of the char
// for example a b b b c c in an array k is at index b
// count is equal 3 when we increment by k + count we move to
// the next unkown value
k = k + count;
}
writeOut.write("Total Probability Distribution: " + String.format( "%.3f", Tdis ));
}
finally{
// closing the filein and writeOut
fileIn.close();
writeOut.close();
}
}
/**
*
* the star method used to print out the astrix for the histogram
*
* @param count
* @return
**/
public static String star(int count){
String s = "";
for(int i=0; i< count; i++){
s += "*";
}
return s;
}
/**
*
* method used to remove the \n and - from the string.
*
* @param string
* @return
**/
public static String removeAll(String string) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
// Allow only letters
if (Character.isLetterOrDigit(ch)) {
stringBuilder.append(ch);
}
}
return stringBuilder.toString();
}
/**
*
* METHOD TO ENCODE USING SHANNON FANO
*
* @param str
* @param letters
* @param bits
* @return
* @throws java.io.FileNotFoundException
*/
public static String Encode(String str, char[] letters, String[] bits) throws FileNotFoundException{
PrintWriter writeOut = new PrintWriter("compRes.dat");
try {
/**
* creating a char array to store the incoming string into characters.
**/
char[] charStr = str.toCharArray();
/** creating a String variable to store the encoded string. */
String SFEncOrigStr = "";
/** Looping through the Char array
* if the integer i is less than the length of the char array increment i
* this loop will continue to execute until i is not less than the length
* of the char array.
*/
for(int k = 0; k < charStr.length; k++){
/** Calling the searchChar method and assign its return value to the int variable r. */
int r = searchChar(letters, charStr[k]);
/**The string value is equal the string value plus the value of the element in the bist array at index r. */
SFEncOrigStr = SFEncOrigStr + bits[r];
}
writeOut.print(SFEncOrigStr);
return SFEncOrigStr;
}
/**Returning the encoded String value. */
finally {
writeOut.close();
}
}
/******* END OF THE ENCODING METHOD. ******/
/**
*
* METHOD TO DECODE USING SHANNON FANO
*
* @param encBits
* @param letters
* @param bits
*
* @throws java.io.FileNotFoundException
*/
public static void Decode(String encBits, char[] letters, String[] bits) throws FileNotFoundException{
PrintWriter Out = new PrintWriter("decompRes.dat");
try{
/**Declaring a String value to hold the decoded String. */
String decompStr = "";
/**declaring a int variable k which will be used for the while loop condition. */
int k = 0;
/** declaring a while loop with the condition of whether the value of k is less than the
* length of the encoded String while K is less than the length execute the while loop.
*/
while(k < encBits.length()){
// asinging temp to the value of the encbits with substring of k too k+3
// this means temp is == the value k too k+3 character.
String temp = encBits.substring(k,k+3);
// r is equal to the value return value of the searchString method
int r = searchString(bits, temp);
//
// assing x to the value of r to be used later
int x = r;
//if the value of r < than 0 execute the contents of the if stamtent
if (r < 0){
// temp is equal the charactes of encbit at k to k+ 4 posistion
temp = encBits.substring(k,k+4);
// assing the return value of searchString to r
r = searchString(bits, temp);
// x is == r
x = r;
// if the value of x is less than 0 exeute
// this means than the value of k to k+4 was not found
if(x < 0) {
// temp is equal the charactes of encbit at k to k+ 5 posistion
temp = encBits.substring(k,k+5);
// assing the return value of searchString to r
r = searchString(bits, temp);
// the decompString is = the decomStr + the value index r from
// letters array
decompStr = decompStr + String.valueOf(letters[r]);
// increment k by k + 5
// we increment by k + 5 as the bit length was 5 therefore
// to move to the next bit we have to incremtent by k + 5.
k = k + 5;
}
// if x is not less than 0 which means the value of temp was found
// execute the contents of the else
else{
// the decompString is = the decomStr + the value index r from
// letters array
decompStr = decompStr + String.valueOf(letters[r]);
// we increment by k + 4 as the bit length was 4 therefore
// to move to the next bit we have to incremtent by k + 4.
k = k + 4;
}
}
// if r is not less than 0 execute the contents of the else
// this means that temp was equal encBits.substring(k,k+3);
else {
// the decompString is = the decomStr + the value index r from
// letters array
decompStr = decompStr + String.valueOf(letters[r]);
// we increment by k + 3 as the bit length was 3 therefore
// to move to the next bit we have to incremtent by k + 3.
k = k + 3;
}
}
Out.write(decompStr);
}
finally {
Out.close();
}
}
/******* END OF THE DECODING METHOD. ******/
/**
*
* METHOD TO SEARCH FOR STRING
*
* @param array
* @param val
* @return
*
**/
// Method to search a particular string inside of array of strings
// return the first index of the string found
// otherwise return -1
public static int searchString(String[] array, String val) {
int ind = -1;
for(int i=0; i<array.length; i++) {
if(array[i].equals(val)){
ind = i;
break;
}
}
return ind;
}
/******* END OF THE SEARCH STRING METHOD ******/
/**
*
* METHOD TO SEARCH FOR CHAR
*
* @param array
* @param val
* @return
*/
// Method to search a particular char inside of array of characters
// return the first index of the character found
// otherwise return -1
public static int searchChar(char[] array, char val) {
int ind = -1;
for(int i=0; i<array.length; i++) {
if(array[i] == val){
ind = i;
break;
}
}
return ind;
}
}