Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

character freqency

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
frizzy1234
Asked:
frizzy1234
1 Solution
 
CEHJCommented:
0
 
rrzCommented:
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
 
funnyveryfunnyCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
JakobACommented:
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
 
rama_krishna580Commented:
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
 
gnoonCommented:
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
 
CEHJCommented:
:-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now