Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Donate

Use the button below to make one time or recurring donations to help generate income. Thank you very much for your generosity. Please continue to enjoy ObiokusThoughts.com


Template for Huffman Encoding in Java (some bugs may occur)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package huffman;

import java.io.*;
import java.util.*;
import huffman.AbstractMap;
import static javafx.scene.input.KeyCode.V;

/**
 *
 * @author obobotette0
 */
public class Huffman {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
     
        //create variables
        System.out.println("Please enter the path and/or name of file");
        Scanner name = new Scanner(System.in);
        String file_name = name.nextLine();
        PrintWriter output = new PrintWriter(new FileWriter(new String("output.txt")));
        AbstractMap freq = new AbstractMap<>('0', 0);
        AbstractMap mix = new AbstractMap<>("null", 0);
        AbstractMap huffman = new AbstractMap<>("null", '0');
     
        char[] array;
        int nodes = 0;
        int min1, min2;
        File input = new File(file_name);
     
        Scanner read = new Scanner(input);
     
        //read each line and prse string
        while (read.hasNextLine()) {
            array = read.nextLine().replace(" ", "").replace("  ", "").replace("-", "").replace("!", "").replace(".", "").replace(",", "").replace("?", "").replace("/", "").replace(";", "").replace(":", "").replace("[", "").replace("]", "").toLowerCase().toCharArray();
         
            //for length of line
            //if key is found increase value as frequency
            //else its first instance
            for (int i = 0; i < array.length; i++) {
                if (freq.findIndex(array[i]) > -1) {
                    freq.input(array[i], freq.addFreq(array[i]) + 1);
                } else {
                    freq.input(array[i], 1);
                }
            }
        }
     
        //close reading
        read.close();

        //Print frequency table
        System.out.println("Printing frequency");
        output.append("Printing Frequency");
        output.println();
        freq.sort();      
        freq.print();
     
        output.append(freq.filePrint().toString());
        output.println();
        output.println();
     
        for (int i = 0; i < freq.size(); i++) {
            mix.input(freq.getKey(i).toString(), freq.getValue(i));
        }
     
        //while there is more than one node
        //begin combining
        while (mix.size() > 1) {
            //The first two nodes are combined with a binary value assigned as the key
            //The value is the sum of the frequencies
            mix.input(mix.mapping(mix.getKey(0), '0').toString() + " " + mix.mapping(mix.getKey(1), '1').toString(), mix.getValue(0) + mix.getValue(1));
            //Delete the first two nodes and resort list
            mix.remove(0);
            mix.remove(0);
            mix.sort();
         
        }
     
        //Print form of map
        System.out.println("Printing map");
        output.append("Printing map");
        output.println();
        mix.print();
     
        output.append(mix.filePrint().toString());
        output.println();
        output.println();
     
        //Parse the entire map into sections
        String[] chars = mix.getKey(0).split(" ");
     
        for (int a = 0; a < chars.length; a++) {
         
            //remove leading 0's and 1's from string
            array = "abcdefghijklmnopqrstuvwxyz".toCharArray();
            String c = chars[a].replace("0", "");
            c = c.replace("1", "");
            String[] temp;
            for (int b = 0; b < array.length; b++) {
             
                //separate binary from char
                temp = chars[a].split(c);
             
                //condition is true only if reduced string is a letter of the alphabet  
                if (c == null ? String.valueOf(array[b]) == null : c.equals(String.valueOf(array[b]))) {
                 
                    //input as binary key and char value
                    huffman.input(temp[0], c.charAt(0));
                }
            }
        }

        //Print binary map
        System.out.println("Printing binary");
        output.append("Printing binary");
        output.println();
     
        huffman.print();
        output.append(huffman.filePrint().toString());
        output.println();
        output.println();
     
        //Begin from top of file
        read = new Scanner(input);
     
        //Print encoded message
        System.out.println("Printing encoded message");
        output.append("Printing encoded message");
        output.println();
     
        while (read.hasNextLine()) {
            array = read.nextLine().replace(" ", "").toLowerCase().toCharArray();
            for (int i = 0; i < array.length; i++) {
                //return binary string for each found character
                if (huffman.findValue(String.valueOf(array[i]).charAt(0))) {
                    System.out.print(huffman.retValue(String.valueOf(array[i]).charAt(0)) + " ");
                    output.append(huffman.retValue(String.valueOf(array[i]).charAt(0)) + " ");
     
                }
            }
            System.out.println();
            output.println();
        }
     
        input = new File(new String("output.txt"));
     
        read = new Scanner(input);
        System.out.println("Printing decoded message");
        output.append("Printing decoded message");
        output.println();
        read.findInLine("Printing encoded message");
        while (read.hasNextLine()) {
            array = read.nextLine().replace(" ", "").toLowerCase().toCharArray();
            System.out.print(String.valueOf(array) + " " + array.length);
             
            for (int i = 0; i < array.length; i++) {
                 
                if (huffman.findKey(String.valueOf(array[i]))) {
                    //System.out.print(String.valueOf(array[i]) + " ");
                    System.out.print(huffman.retKey(String.valueOf(array[i])) + " ");
                    output.append(huffman.retKey(String.valueOf(array[i])) + " ");
     
                }
            }
            System.out.println();
            output.println();
        }
     
     
     
        output.flush();
     
    }
 
}

No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!

Note: Only a member of this blog may post a comment.