Second week: statistical improvement of Word frequency

Source: Internet
Author: User

Demand:

1. Enter commands in the console;

2. The input must be a path or a file that already exists on the hard disk;

3. Enter the directory name to count the directories;
4. Enter the file in the file to count the words inside.

Analysis:
1. Read the file using the BufferedReader class to read by line;
2. Define a regular expression to filter the string symbol;
3. Create HashMap;
4. Use StringTokenizer to participle;
5. Add the words to the haspmap stored key value pairs;
6, traverse hashmap output result;
7. Let the user enter a path in the console;
8. The user input path to judge if it is a directory to traverse the directory, in the statistics inside the word;
9. Otherwise, the file is traversed, and the word in the statistics file is sorted using the Collections.sort () method.

Specific code implementation:

1. Determine whether the input is a directory, directory traversal and statistics.

1File File =NewFile (URL);//Create a File object2         if(File.isdirectory ()) {//determine if the file object is a directory3string[] names = File.list ();//gets the file name of all files under the directory4              for(String name:names) {5 System.out.println (name);6                 if(!hashmap.containskey (name)) {//determine if there is a word in the HashMap7Hashmap.put (Name,NewInteger (1)); 8                     //if the value of Word is not treated as a key, the key value is recorded once9                     Ten}Else{     One                     intK=hashmap.get (name). Intvalue () +1;//If you have the value of Word as a key, the key value is added on the original basis AHashmap.put (Name,NewInteger (k));  -                      -                 }     the                       for(String key:hashMap.keySet ()) { -                             //print words and the number of words -SYSTEM.OUT.PRINTLN (key + ": \ T" +Hashmap.get (key)); -                         } +                  -              +             } A              at              -}

2. If the input is a file, read the word inside the file.

        //The words in the statistics file at the time of the document            Else{bufferedreader br=NewBufferedReader (Newfilereader (URL));                   String value;  while((Value=br.readline ())! =NULL){//Read the text and read a line until it is finished readingValue=value.replaceall (Fregex, "");//use spaces instead of some special symbols in text//use StringTokenizer to participle (the application class StringTokenizer used to separate strings)StringTokenizer Tokenizer =NewStringTokenizer (value);  while(Tokenizer.hasmoretokens ()) {String Word=Tokenizer.nexttoken (); if(!hashmap.containskey (Word)) {//determine if there is a word in the HashMapHashmap.put (Word,NewInteger (1));//if the value of Word is not treated as a key, the key value is recorded once}Else{                                     intK=hashmap.get (Word). Intvalue () +1;//If you have the value of Word as a key, the key value is added on the original basisHashmap.put (Word,NewInteger (k)); }                                               }                     }                 

3. Count the words and sort them.

arraylist<map.entry<string, integer>> Com =NewArraylist<map.entry<string, integer>>(Hashmap.entryset ()); //sort using the Sort method inside the Collections tool classCollections.sort (Com,NewComparator<map.entry<string, integer>>()                    {                             Public intCompare (map.entry<string, integer> O1, map.entry<string, integer>O2) {                                return(O2.getvalue ()-O1.getvalue ());                        };                    }); System.out.println ("Total:" + hashmap.size ());//traverse HashMap, Output results                    intCount = 50;  for(Map.entry<string, integer>comp:com) {                            if(count--< 0) Break; System.out.println (Comp.getkey ()+ ": \ T" +Comp.getvalue ()); }                                                      }           }

4. Enter the command in the console.

/**      * Console input command     * can be a directory or a file     *      * */public    static  String Scan () {        new  Scanner (system.in);          System.out.print ("Please enter directory or file:");          = Sc.next ();           return text;    }}

Operation Result:

1. Read the directory.

2. Read the files in the directory.

Second week: statistical improvement of Word frequency

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.