Mon Jan 21, 2013 1:48 pm
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.swing.JFrame;
public class Arithmetic {
public static void sortString(ArrayList<String> c, ArrayList<Double> p) {
for (int i = 0; i < c.size(); i++) {
String s = "";
s += c.get(i);
for (int j = i + 1; j < c.size(); j++) {
if (s.compareTo(c.get(j).toString()) > 0) {
s = "";
s += c.get(j);
c.set(j, c.get(i));
c.set(i, s);
double d = p.get(j);
p.set(j, p.get(i));
p.set(i, d);
}
}
}
}
public static void compression(String symbols) {
String temp1="";
String temp2="";
double size=0;
double counter=0;
boolean key;
ArrayList<Double> probability = new ArrayList<Double>();
ArrayList<String> symbol = new ArrayList<String>();
for(int i=0; i<symbols.length();i++) {
temp1+=symbols.charAt(i);
key=symbol.contains(temp1);
if(key){}
else{
symbol.add(temp1);
for(int j=0;j<symbols.length();j++) {
temp2+=symbols.charAt(j);
if(temp1.contains(temp2)) counter++;
temp2="";
}
size=(counter/symbols.length());
probability.add(size);
}
counter=0;
temp1="";
}
sortString(symbol, probability);
double temp=0;
ArrayList<Double> comulitive = new ArrayList<Double>();
for(int i=0; i<probability.size(); i++) {
temp+=probability.get(i);
if(i==probability.size()-1) {
comulitive.add(1.0);
}
else comulitive.add(temp);
}
ArrayList<Double> com = new ArrayList<Double>();
com.addAll(comulitive);
double L=0;
double U=0;
for (int i = 0; i < symbols.length(); i++) {
String str = "";
str += symbols.charAt(i);
int index = symbol.indexOf(str);
if (index == 0) {
} else {
L = comulitive.get(index - 1);
}
U = comulitive.get(index);
for (int k = 0; k < symbol.size(); k++) {
comulitive.set(k, L + (U - L) * com.get(k));
}
}
double result=(L+U)/2;
System.out.println(result);
Files f = new Files();
f.writeCompress(symbol, probability, result, symbols.length());
//String newTemp="";
//newTemp+=result;
//Files f = new Files();
//f.writeFile("D:\\compress.txt");
//f.addRecords(newTemp);
}
public static void decompression(ArrayList<Double> prob,
ArrayList<String> characters, Double res, int length) {
double temp=0.0;
ArrayList<Double> comulitive = new ArrayList<Double>();
for(int i=0; i<prob.size(); i++) {
temp+=prob.get(i);
if(i==prob.size()-1) {
comulitive.add(1.0);
}
else comulitive.add(temp);
}
ArrayList<Double> com = new ArrayList<Double>();
com.addAll(comulitive);
double L=0;
double U=0;
String str = "";
for (int i = 0; i < length; i++) {
int j;
for(j=0; j<characters.size(); j++) {
if(res<comulitive.get(j)) {
str+=characters.get(j);
break;
}
}
if (j == 0) {
} else {
L = comulitive.get(j - 1);
}
U = comulitive.get(j);
for (int k = 0; k < characters.size(); k++) {
comulitive.set(k, L + (U - L) * com.get(k));
}
}
System.out.println(str);
Files f = new Files();
f.writeDecompress(str);
/*Files read = new Files();
read.openFile("D:\\compress.txt");
Files write = new Files();
write.writeFile("D:\\decomp.txt");
write.addRecords(str);*/
}
public static void main(String[] args) {
//compression("acba");
/*ArrayList<String> c = new ArrayList<String>();
c.add("c");
c.add("b");
c.add("a");
ArrayList<Double> d = new ArrayList<Double>();
d.add(0.5);
d.add(0.25);
d.add(0.25);
sortString(c, d);
System.out.println(c);
System.out.println(d);*/
Gui g = new Gui();
g.createAndShowGUI();
}
}
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Formatter;
public class Files {
private String text="";
private Scanner scan;
//read symbols and codes
private Scanner scanSymbol;
private ArrayList<String> symbol;
private ArrayList<String> code;
public void openFile(String path){
try{
scan = new Scanner(new File(path));
}catch(Exception e){
System.out.println("could not find file");
}
}
public String readFile(){
while(scan.hasNext()){
text += scan.next();
}
return text;
}
//to read symbol file and put spaces
public String readFileSymbolsWithSpaces(){
while(scan.hasNext()){
text += scan.next();
text += " ";
}
return text;
}
public void readSymbol(){
String temp ="";
try{
scanSymbol = new Scanner(new File("D:\\file.txt"));
while(scanSymbol.hasNext()){
//adds symbol
temp += scanSymbol.next();
symbol.add(temp);
temp ="";
//adds code
temp+= scanSymbol.next();
code.add(temp);
temp="";
}
scanSymbol.close();
}catch(Exception e){
System.out.println("could not find file");
}
}
public ArrayList<String> getSymbol(){
return symbol;
}
public ArrayList<String> getCode(){
return code;
}
public void closeFile(){
scan.close();
}
//**************** Write file****************
private FileWriter write;
private BufferedWriter print;
public void writeFile(String path){
try{
write = new FileWriter(path);
}catch(Exception e){
System.out.println("error in writing file");
}
}
public void addRecords(String record){
print = new BufferedWriter(write);
try {
print.write(record);
print.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//////////////////////////////////////////////////////////
private static Scanner file;
private static Formatter output;
public static String readCompress(String path) {
try {
file = new Scanner(new File(path));
} catch (Exception e) {
System.out.println("File isn't found !!");
}
String res = "";
while (file.hasNext()) {
res += file.next();
}
file.close();
return res;
}
public static void writeCompress(ArrayList<String> character,
ArrayList<Double> prob, double result, int length) {
try {
output = new Formatter("D:\\Compress.txt");
} catch (Exception e) {
System.out.println("Failure !!");
}
String s = "";
s += result + " ";
output.format(s);
s = "";
s += length + " ";
output.format(s);
for (int i = 0; i < character.size(); i++) {
s = "";
s += character.get(i);
s += " ";
s += prob.get(i);
if (i == character.size() - 1) {
output.format(s);
} else {
s += " ";
output.format(s);
}
}
output.close();
}
public static void writeDecompress(String data) {
try {
output = new Formatter("D:\\Decompress.txt");
} catch (Exception e) {
System.out.println("Failure !!");
}
output.format(data);
output.close();
}
public static void readDecompress(String path, double data, int len,
ArrayList<String> c, ArrayList<Double> d) {
try {
file = new Scanner(new File(path));
} catch (Exception e) {
System.out.println("File isn't found !!");
}
data += Double.parseDouble(file.next());
len += Integer.parseInt(file.next());
while (file.hasNext()) {
c.add(file.next());
d.add(Double.parseDouble(file.next()));
}
file.close();
Arithmetic obj = new Arithmetic();
obj.decompression(d, c, data, len);
}
public static String editPath(String s) {
String res = "";
for (int i = 0; i < s.length(); i++) {
res += s.charAt(i);
if (s.charAt(i) == '\\') {
res += '\\';
}
}
return res;
}
}
import java.io.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
public class Gui extends JPanel implements ActionListener {
JButton compB, decompB;
JFileChooser f;
public Gui() {
super(new BorderLayout());
//Create a file chooser
f = new JFileChooser();
compB = new JButton("Compress");
compB.addActionListener(this);
decompB = new JButton("Decompress");
decompB.addActionListener(this);
//For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel(); //use FlowLayout
buttonPanel.add(compB);
buttonPanel.add(decompB);
//Add the buttons and the log to this panel.
add(buttonPanel, BorderLayout.PAGE_START);
}
public void actionPerformed(ActionEvent e) {
// when open is clicked
if (e.getSource() == compB) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(Gui.this);
Files f = new Files();
String s = null;
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
s = file.getPath();
s = f.editPath(s);
} else {
}
Arithmetic obj = new Arithmetic();
obj.compression(f.readCompress(s));
/*if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = f.getSelectedFile();
String comp="";
Files read = new Files();
read.openFile(file.getPath());
comp = read.readFile();
read.closeFile();
System.out.println("read "+comp);
String s = null;
Arithmetic obj = new Arithmetic();
//obj.compression(comp);
Files f = new Files();
obj.compression(f.readCompress(comp));
} else {}*/
//when save is clicked
} else if (e.getSource() == decompB) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(fc);
String s = null;
Files f = new Files();
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
s = file.getPath();
s = f.editPath(s);
} else {
}
ArrayList<String> c = new ArrayList<String>();
ArrayList<Double> d = new ArrayList<Double>();
double data = 0.0;
int len = 0;
Files obj1 = new Files();
obj1.readDecompress(s, data, len, c, d);
/*int returnVal = f.showOpenDialog(Gui.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = f.getSelectedFile();
String comp="";
Files read = new Files();
read.openFile(file.getPath());
comp = read.readFile();
read.closeFile();
System.out.println("read "+comp);
Files obj1 = new Files();
ArrayList<String> symbol = new ArrayList<String>();
ArrayList<Double> probability = new ArrayList<Double>();
double result = 0;
int length = 0;
obj1.readDecompress(comp, result, length, symbol, probability);
} else {}*/
}
}
/////////////////////
////////////////////
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Gui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new Gui());
//Display the window.
frame.pack();
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.