Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.*;
- public class Compress {
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- String text="";
- try {
- text = new Scanner( new File("testdata.txt"), "UTF-8" ).useDelimiter("\\A").next();
- System.out.println(text+"");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- char ch[] = new char[37];
- for(int i=0;i<10;i++)
- ch[i]=(char)(i+48);
- for(int i=0;i<26;i++)
- ch[i+10]=(char)(i+65);
- ch[36]=(char)32;
- System.out.println("CHARACTERS IN THE TEXT FILE ARE : \n"+Arrays.toString(ch));
- int count[] = new int[37];
- for(int j=0;j<37;j++) {
- for(int i=0;i<text.length();i++) {
- if(ch[j]==text.charAt(i))
- count[j]=count[j]+1;
- }
- }
- double d=text.length();
- System.out.println("THE COUNT OF EACH CHARACTER IS : \n"+Arrays.toString(count));
- System.out.println("TOTAL NO. OF CHARACTERS : "+text.length());
- for(int i=0;i<count.length;i++) {
- System.out.println("THE PROBABLITY OF "+ch[i]+" IS "+(count[i]/d));
- }
- double entropy=0.0;
- for(int i=0;i<37;i++) {
- if(count[i]==0)
- continue;
- double probability=count[i]/d;
- double entropy1=probability*(Math.log(probability)/Math.log(2));
- entropy+=entropy1;
- }
- System.out.println("THE ENTROPY IS "+(-1)*entropy);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment