$29
In this homework, you are going to write a program to implement the key word encoding algorithm for text compression and decompression.
First, scan the file “basketball.txt” and replace the four most frequent words by the four special characters: '%', '$', '#', and '*'. Then save the compressed text to another file.
In the compressed text, you do not see the most frequent words. Instead, you see the special symbols. Then please write a method to read the compressed file and decompress it to the original text. Note capital letters are converted to lower case during compression. This information cannot be restored during decompression. For this assignment, ignore this problem.
Hints:
Declare an instance variable:
private Map <String, Character dictionary=new HashMap<String, Character();
This map variable can find the character/code for a given word as long as the word exists in the map. Say you have save the following key-value pairs in the map:
Key
Value
"and" ------------
'#'
"basketball" ---
'$'
"is" ---------------
'*'
"the" -------------
'%'
Then you can find the character/code for "and" by the following method call:
String word = “and”;
dictionary.get(word); // returns a Character reference that contains '#'
The previous dictionary variable can be used in compressing. For decompressing, you need to “reverse” the dictionary.
You are given an incomplete program “FileIO.java”. Please add your code to this Java program.
Grading Policies:
Find the most frequent words: 33%
Compression: 33%
Decompression: 34%