?
Solved

character freqency

Posted on 2004-04-20
7
Medium Priority
?
511 Views
Last Modified: 2013-12-03
I need to write a program which can calculate the freqency of (ignoring case) characters in the file.  I know much of how to get the files and stoe it in arrays. But, i cannot figure out how to use the tokenizer and store the value and calculate the frequencies.  Basically i need to write a program to ouput the frequency of characters to the screen from any input file.  any hints would be extremely helpful.  
Thanks
0
Comment
Question by:frizzy1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 10871706
0
 
LVL 28

Expert Comment

by:rrz
ID: 10872038
Here is some code that came from the JSP topic area.

import java.util.*;
public class CharFreq {
     public static void main(String[]args){
                 if(args.length != 1){
                                      System.out.println("Enter one String");
                                      System.exit(-1);
                 }
                 char[] charArray = args[0].toCharArray();
                 Arrays.sort(charArray);
                 String sortedData = new String(charArray);
                 int pivot = 0;
                 int index = 0;
                 char c;
                 Integer freq;
                 ArrayList charList;
                 HashMap freqMap = new HashMap();
                 while(pivot<sortedData.length()){
                                     c = sortedData.charAt(pivot);
                                     index = sortedData.lastIndexOf(c)+1;
                                     freq = new Integer(index - pivot);
                                     if(freqMap.containsKey(freq)){
                                                   charList = (ArrayList)freqMap.get(freq);
                                                   charList.add(new Character(c));
                                     }else{
                                           charList = new ArrayList();
                                           charList.add(new Character(c));
                                           freqMap.put(freq,charList);
                                      }
                                     pivot = index;
                 }
                 TreeSet freqs = new TreeSet(Collections.reverseOrder());
                 freqs.addAll(freqMap.keySet());
                 Iterator iter = freqs.iterator();
                 while(iter.hasNext()){
                              freq = (Integer)iter.next();
                              charList = (ArrayList)freqMap.get(freq);
                              for(int j=0;j<charList.size();j++){
                                 System.out.println((Character)charList.get(j) + " = " + freq);
                              }
                 }
     }
}  
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 10873040
Hi,

Above code demonstrates the use of Hash but not character manipulation. You will need to use a streamreader, refer to Java reference for specific type, to read each character.

Two essential tools here are: a Streamreader and a Hashtable.

Did you read "Code Breaker" by Simon Singh, I read the book and thought about writing a little program but never got round to it so it'll be nice if you can post the code when finish so I can play with it?

bye.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 15

Expert Comment

by:JakobA
ID: 10873761
Tokenizer is overkill for this. you are handeling individual characters so just read the chacters one at at time and process then as they come.

If you are CERTAIN your file do not contain any 16-bit unicode characters the easiest way is to use a counting array with one int cell for each of 256 character codes. then for each character you read from the file you increment the corresponding cell in the array. if there is a risk of 16-bit character codes you would use the hashtable mentioned by funnyveryfunny as an associative array.
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10874285
Hi check this below, it may help you...

import java.util.*;

//Class to store result information
class ResultClass
{
      boolean found;
      int matchDocID;
      int count;
}

public class QueryMatcher
{
      private static ArrayList foundList;
      private static ArrayList resultList;
      private static ArrayList queryTerm=new ArrayList();
      private static ResultClass result;

      public QueryMatcher(ArrayList queryTermList)
      {
            queryTerm=queryTermList;
      }

      //get the list of keys of word(term) list that match query string
      public static void MatchQuery(ArrayList list)
      {
            ArrayList tfArray      = new ArrayList();
            TermFrequency tf      = new TermFrequency();

            for(int count=0;count<list.size();count++)
            {
                  tf = (TermFrequency) list.get(count);
                  tfArray.add(count,tf.word);
            }

            foundList=new ArrayList();
            int index=0;
            int key;

            for(int i=0;i<queryTerm.size();i++)
            {
                  key = Collections.binarySearch(tfArray, queryTerm.get(i));
                  if (key>-1)
                  {
                        Integer wKey=new Integer(key);
                        foundList.add(index++,wKey);
                  }
            }

            if (foundList.size()!=queryTerm.size())
            {
                  System.out.println("Sorry! There is no matching document.");

                  //Instantiate a new QueryParser and
                  //Call ParseQuery method
                  QueryParser q = new QueryParser();
                  q.ParseQuery();
            }

      }

      //Extracting all the document IDs that contains all query terms
      public static void extractMatchDocument()
      {
            TermFrequency tf      = new TermFrequency();
            TermFrequency keytf = new TermFrequency();
            WordCount wordCount = new WordCount();

            WordCount keywordCount      = new WordCount();
            ArrayList tfArray            = new ArrayList();
            result                              = new ResultClass();
            ArrayList keyResultList = new ArrayList();
            HashMap keyResultMap      = new HashMap();
            resultList                        = new ArrayList();
            ResultClass resultLoop      = new ResultClass();

            int key            = 0;
            int index;
            int listIndex=0;

            index = Integer.parseInt(foundList.get(0).toString());
            keytf = (TermFrequency) QueryProcessor.getIndex().get(index);

            for(int j=0;j<foundList.size();j++)
            {
                  if (j == 0)
                  {
                        for (int i = 0; i < keytf.count.size(); i++)
                        {
                              result                  = new ResultClass();
                              keywordCount      = new WordCount();
                              keywordCount      = (WordCount) keytf.count.get(i);
                              result.found      = true;
                              result.matchDocID = keywordCount.docID;
                              result.count      = result.count + 1;

                              keyResultList.add(result);
                              keyResultMap.put(new Integer(key), result);
                        }
                  }
                  else
                  {
                        resultList = new ArrayList();
                        index = Integer.parseInt(foundList.get(j).toString());
                        tf = (TermFrequency) QueryProcessor.getIndex().get(index);

                        for (int k = 0; k < tf.count.size(); k++)
                        {
                              wordCount      = new WordCount();
                              result            = new ResultClass();
                              listIndex      = 0;

                              wordCount = (WordCount) tf.count.get(k);

                              for (int y = 0; y < keyResultList.size(); y++)
                              {
                                    if (wordCount.docID == ( (ResultClass) keyResultList.get(y)).matchDocID)
                                    {
                                          result.found = true;
                                          result.matchDocID = wordCount.docID;
                                          result.count = result.count + 1;
                                          resultList.add(listIndex++,result);
                                    }
                              }
                        }
                        keyResultList = resultList;
                  }
            }
            resultList=keyResultList;
            //sort the results
            Collections.sort(resultList, new ResultComparator());
      }



      //display the matching document path and name
      public static void diplay()
      {
            ResultClass printResult=new ResultClass();
            ResultClass printResultMap=new ResultClass();
            String filename;
            int key;

            System.out.println();
            System.out.println(resultList.size() + " Matching Document(s) found. ");
            System.out.println();

            System.out.println("Filename(s) are : ");
            System.out.println();

            for(int i=0;i<resultList.size();i++)
            {
                  printResult= (ResultClass) resultList.get(i);
                  key=printResult.matchDocID-1;
                  filename= QueryProcessor.getFileList().get(key).toString();
                  System.out.println(i+1+". "+filename);
                  System.out.println();
            }
      }

}

best of luck....
R.K.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10874897
A sample one

    import java.io.*;
    import java.util.*;
    import javax.swing.JFileChooser;

    public class CharFreq {
        public static void main(String[] args) throws Exception {
            JFileChooser jfc = new JFileChooser();
            int returned = jfc.showOpenDialog(null);
            if( returned == JFileChooser.APPROVE_OPTION ) {

                StreamTokenizer st = new StreamTokenizer(new InputStreamReader(new FileInputStream(jfc.getSelectedFile())));
                st.ordinaryChars(33,255);

                Map freq = new HashMap();

                while (st.nextToken() != StreamTokenizer.TT_EOF ) {
                    String key = (((char)st.ttype)+"").toUpperCase();
                    Object i = freq.get(key);
                    int count = i == null ? 1 : ((Integer)i).intValue()+1 ;
                    freq.put(key,new Integer(count));
                }

                printFreq(freq);

            }
            System.exit(0);
        }

        public static void printFreq(Map freq) {
            System.out.println(freq);
        }
    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10876534
:-)
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question