Social Icons

Sunday, April 4, 2010

Text Analysis : my approach


The purpose of this Analysis code are as follows. I intend to design a GUI for this soon still it’s in command line state.

  • Application reads a line of text from the keyboard and prints a table indicating
    the number of occurrences of each letter of the alphabet in the text. For example,
    the phrase.   “To be, or not to be: that is the question:” contains one “a,” two “b’s,” no “c’s,” and so on.
  • Application that reads a line of text and prints a table indicating the number
    of one-letter words, two-letter words, three-letter words, and so on, appearing in the
    text.
  • Application that reads a line of text and prints a table indicating the number  of occurrences of each different word in the text. application should include the words in the table in the same order in which they appear in the text.

So I did. there may be several way of accomplish above tasks. one can use switch statements but incidentally I did not like the idea . Collections came to mind because this application required to sort the data , find unique words and count them. (yes java.util.TreeSet<E> you got it). and i created two version of the code. one being simply use System,console to print the result and the other writes to a file. both class files , runner code and the Docs can be downloaded from here.

Sample Report :

Untitled

Here is the sample Runner code :

package textanalysis;
import java.io.*;
class Runner {
    public static void main(String[] a){
        try{
            String source="<Enter file path here>";
            File readme = new File(source);
            FileReader reader = new FileReader(readme);
            BufferedReader buf = new BufferedReader(reader);
            String s="" , text="";
            while( (s=buf.readLine()) != null ){
                text+=s+"\n";
            }
            buf.close();
            String report = "<Enter file path here>";
            readme = new File(report);
            PrintWriter pw = new PrintWriter(readme);
            AnalyserIO aa =new AnalyserIO();
            aa.analyseText(text,pw);
            aa.analyseWords(text,pw);
            pw.flush();
            pw.close();
            
            System.out.println(" File "+readme.getName()+" created "+readme.length()+" bytes");
            
        }
        catch(IOException e){System.err.print(e.getMessage());}
    }
}
 
 
Blogger Templates http://slots.to/