Este codigo faz a compressao de um ficheiro txt.
O Bwt faz a rotaçao das frases e ordena por ordem lexicográfica e no fim retorna a ultima coluna.
O rle faz o seguinte:
aaabbbcc
3a3b2c
O seguinte codigo funciona mas no entanto demora demasiado tempo. Aceito sujestoes para o optimizar ..
Obrigado
O Bwt faz a rotaçao das frases e ordena por ordem lexicográfica e no fim retorna a ultima coluna.
O rle faz o seguinte:
aaabbbcc
3a3b2c
O seguinte codigo funciona mas no entanto demora demasiado tempo. Aceito sujestoes para o optimizar ..
Código:
import java.util.Scanner;
import java.io.*;
public class compressor2
{
static String rle (String mod)
{
int k,count = 1, flag =0;
char c = 'e';
String modrle;
modrle = "";
for (k=0; k<mod.length();k++){
if (k != mod.length()-1){
if (mod.charAt(k)== mod.charAt(k+1))
{
count = count+1;
c = mod.charAt(k);
flag = 1;
}
else if (flag == 0){
c = mod.charAt(k);
modrle = modrle + '1' + c;
count= 1;
flag=0;}
else if (k==mod.length()-1 & flag == 0){
c=mod.charAt(k);
modrle = modrle + '1' + c;
count=1;
flag = 0;
}
else {
c=mod.charAt(k);
while (count>9)
{
modrle = modrle + '9' + c;
count = count - 9;
}
modrle = modrle + count + c;
count=1;
flag = 0;
}
}
else{
if (flag == 0){
c = mod.charAt(k);
modrle = modrle + '1' + c;
count= 1;
flag=0;}
else if (k==mod.length()-1 & flag == 0){
c=mod.charAt(k);
modrle = modrle + '1' + c;
count=1;
flag = 0;
}
else {
c=mod.charAt(k);
while (count>9)
{
modrle = modrle + '9' + c;
count = count - 9;
}
modrle = modrle + count + c;
count=1;
flag = 0;
}
}
}
return modrle;
}
static String bwt (String[] frase, int nc)
{
int k,i,j,x,o;
String v, v2, aux, mod;
String [][] matrix = new String[nc][nc];
//BWT ROTACOES
for (k=0; k<nc;k++){
if (k == nc-1)
matrix[0][k]=String.valueOf('ß');
else
matrix[0][k]=frase[k];
}
for(i=0;i<matrix.length-1;i++){
for (j=0;j<matrix.length;j++){
if (j==matrix.length-1)
matrix[i+1][0]=matrix[i][j];
else
matrix[i+1][j+1]=matrix[i][j];
}
}
//BWT A ORDENAR
v="";
v2="";
for (i=0;i<nc - 1;i++){
for (k=i+1;k < nc; k++){
for (j=0;j<nc;j++){
v=v+matrix[i][j];
v2=v2+matrix[k][j];
}
x = v.compareTo(v2);
if (x > 0)
{
aux = v;
for (o=0;o<nc;o++){
matrix[i][o] = matrix[k][o];
matrix[k][o] = String.valueOf(aux.charAt(o));
}
}
v="";
v2="";
}
}
//BWT RESULTADO
mod = "";
for (i=0; i<matrix.length;i++){
mod = mod + matrix[i][nc -1];
}
return mod;
}
public static void main(String[] args) throws IOException
{
String frase=null, mod = "", rlemod = "";
int i = 0, j = 0, nl = 0, nc = 0, prog = 0, prog2 = 0; //nl = number of lines; nc = number of columns
File file = new File("input.txt");
Scanner inputFile = new Scanner(file);
PrintWriter outputFile = new PrintWriter("compressed.txt");
while (inputFile.hasNext())
{
nl ++;
frase = inputFile.nextLine(); /**Lê as linhas do ficheiro "input.txt"**/
if (frase.length() > nc)
nc = frase.length();
}
inputFile.close();
inputFile = new Scanner(file);
String[][] texto = new String [nl][nc];
while (inputFile.hasNext())
{
while (i < nl)
{
frase = inputFile.nextLine();
j=0;
while (j < nc)
{
if (j < frase.length())
{
texto [i][j] = String.valueOf(frase.charAt(j));
j++;
}
else
{
texto [i][j] = null;
j++;
}
}
i++;
}
}
i=0;
int [] a =new int [nl];
int x = 0;
while(i<nl)
{
j=0;
while(j!=nc)
{
if (texto[i][j]!=null)
{
j++;
x = j;
}
else
{
x = j;
j=nc;
}
}
a[i]=x + 1;
i++;
}
i=0;
System.out.println();
System.out.println();
System.out.println("Text Files Compressor - Version 1.0");
System.out.println();
System.out.println();
System.out.println();
System.out.println("Please be patient... compression is in progress.");
System.out.println("This could take a few minutes based on the size and line width of your text file.");
System.out.println();
System.out.println();
System.out.println("Compression Progress:");
System.out.println();
while (i < nl)
{
mod = bwt (texto[i], a[i]);
rlemod = rle (mod);
prog = ((i+1)*100)/nl;
if (prog != prog2)
System.out.print("|");
outputFile.println(rlemod);
i++;
prog2 = prog;
}
System.out.println(" " + prog + "%");
System.out.println();
//Fechar os ficheiros
inputFile.close();
outputFile.close();
}
}
Obrigado
Última edição: