Wednesday, November 23, 2011

program that read a text file and calculate the frequency of the word and display them in sorted order according their frequency.


import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class FileFrequence implements ActionListener
{
StringTokenizer st,st1;
FileInputStream fr,fr1;
DataInputStream din,din1;
String str,item;
int count;
String word[];
int wordf[];
int num;
String uniqueValues[];
int uniqueFrequence[];
JFrame frame;
JButton b1,b2;
JTextField tf;
JPanel p1,p2,p3,p4,p5,p6;
JFileChooser fileopen;
File file;
String path;
JTextArea jTextArea;
JScrollPane jScrollPane ;


FileFrequence()
{
try{
frame= new JFrame("Text File Word Frequency Calculation");
b1= new JButton("Browes");
b2= new JButton("Calculate");

b1.addActionListener(this);
b2.addActionListener(this);

jTextArea= new JTextArea(" ",20,40);
jScrollPane=new JScrollPane(jTextArea);

fileopen= new JFileChooser();
 
  tf=new JTextField();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
p6=new JPanel();

  p1.setLayout(new GridLayout(2,1));
p2.setLayout(new FlowLayout());
p3.setLayout(new BorderLayout());
p4.setLayout(new BorderLayout());
p5.setLayout(new GridLayout(2,8));
     
p2.add(b2);
p2.add(b1);
p3.add(p2,BorderLayout.CENTER);
p6.add(jScrollPane);

p1.add(tf);
p1.add(p3);

frame.add(p1,BorderLayout.NORTH);
frame.add(p6,BorderLayout.CENTER);

frame.setVisible(true);
frame.setBounds(100,100,500,500);
}
catch(Exception er)
{}

}

public void actionPerformed(ActionEvent ee)
{
   if(ee.getSource()==b1)
{
fileopen.showDialog(null,"Open File");
path=fileopen.getSelectedFile().getPath();
tf.setText(path);

}
    else if(ee.getSource()==b2)
{
file= new File(path);
if(file.isFile())
find();
}
     }

void find()
{
int n= wordCount();

word= new String[n];
wordf= new int[n];

try{

fr= new FileInputStream(path);
         din = new DataInputStream(fr);

while(true)
        {
            str=din.readLine();

    if(str==null)
break;

st= new StringTokenizer(str," ");

while(st.hasMoreTokens())
  {
item=st.nextToken();

tokenTravel(item,count);
                 count++;

}

}


num = getNumUniqueValues();
uniqueValues = new String[num];
uniqueFrequence= new int[num];


for(int j=0,k=0;j<word.length;j++)
{
if(!containsValue(uniqueValues, word[j]))
{
                      uniqueValues[k] = word[j];
uniqueFrequence[k]=wordf[j];
k++;
}

}
sortString();
int maxf= maxFrequence();
int frecounter=1;
int start=0;

System.out.println("Maximum FRequencu is "+maxf);

          for(int i=0;i<uniqueFrequence.length;i++)
{
if(uniqueFrequence[i]==frecounter)
{
i= i+1;
}
         else
{
wordSorting(start,i);
start=i;
frecounter++;
}
}

for(int i=0;i<num;i++)
{
  System.out.println(uniqueValues[i]+"--"+uniqueFrequence[i]);
  jTextArea.setText(jTextArea.getText()+"\n"+uniqueValues[i]+"--:"+Integer.toString(uniqueFrequence[i]));
}

 }
 catch(Exception ee)
{}

}


void wordSorting(int str,int end)
{

 String temp="";
   int t=0;

           end=end-1;
for (int i=str;i<=end;i++)
{
for (int j=i+1;j<=end;j++)
{
if (uniqueValues[i].compareTo(uniqueValues[j])>0 )
{
       temp=uniqueValues[i];
uniqueValues[i]=uniqueValues[j];                
               uniqueValues[j]=temp;

        t= uniqueFrequence[i];
uniqueFrequence[i]= uniqueFrequence[j];
uniqueFrequence[j]=t;
}
}
}

}

        int getNumUniqueValues()
{
        String[] values = new String[word.length];
        int count = 0;

        for(int j = 0; j < word.length; j++)
{
            if(!containsValue(values, word[j]))
                values[count++] = word[j];
        }
        return count;
        }

boolean containsValue(String[] array, String target)
{
      for(int j = 0; j < array.length; j++)
{
           if(array[j] != null && array[j].equals(target))
              return true;
        }
       return false;
}

void tokenTravel(String item,int n)
{
 int count=0;

try{
FileInputStream fr1= new FileInputStream(path);
DataInputStream din1 = new DataInputStream(fr1);
String str1="",item1="";
StringTokenizer st1;

while(true)
{
str1=din1.readLine();
   
               if(str1==null)
break;

st1=new StringTokenizer(str1," ");

while(st1.hasMoreTokens())
{
  item1=st1.nextToken();
if(item.equals(item1))
{
count=count+1;
word[n]=item1;
wordf[n]=count;
}

}

}
   }
catch(Exception e)
{
}

}

int wordCount()
{
StringTokenizer st;
int wordc=0;
try
  {
FileInputStream fr= new FileInputStream("File.txt");
       DataInputStream din = new DataInputStream(fr);
       String s="";

       while(true)
       {
           s=din.readLine();

if(s==null)
break;
     
st= new StringTokenizer(s," ");

while(st.hasMoreTokens())
  {
  st.nextToken();
wordc++;
  }

}

  }
 catch(Exception e)
{}

  return wordc;
}

void sortString()
{
 int n= uniqueFrequence.length;
 int t=0;
 String str="";

try{
for ( int i = 0 ; i <= n-2 ; i++ )
{
for ( int j = i + 1 ; j <= n-1 ; j++ )
{
if (uniqueFrequence[i] > uniqueFrequence[j] )
{
t = uniqueFrequence[i] ;
uniqueFrequence[i] = uniqueFrequence[j] ;
uniqueFrequence[j] = t;

str=uniqueValues[i];
uniqueValues[i]=uniqueValues[j];
uniqueValues[j]=str;
}
}
}
                             

for(int i=0;i<n;i++)
{
  System.out.println(uniqueFrequence[i]+"--"+uniqueValues[i]);
}
      }
catch(Exception e)
{
System.out.println(e);
}

 }

int maxFrequence()
{
 int max=0;

for(int i=0;i<uniqueFrequence.length;i++)
{
 if(uniqueFrequence[i]>=max)
{
max=uniqueFrequence[i];
}
}
return max;

}

public static void main(String ...a)
{
FileFrequence f= new FileFrequence();

}
}

/*******************************program pic on running mod************************/

/**************************pic of file that is read by program*********************/



/**************************Download Zip file of Above Program*********************
Download Zip File of Above Program

No comments:

Post a Comment