Counts the number of times a word appears in an English file and sorts it by its length.

Source: Internet
Author: User
Tags sorts

Import java. Io. bufferedreader;
Import java. Io. filenotfoundexception;
Import java. Io. filereader;
Import java. Io. ioexception;
Import java. util. arrays;
Import java. util. comparator;
Import java. util. iterator;
Import java. util. Map;
Import java. util. Set;
Import java. util. stringtokenizer;
Import java. util. treemap;

/**
* Count the occurrence frequency of words in a file.
* If the first letter is the same, the output is based on the word length.
* If the length is equal, it is output alphabetically.
* @ Author TPF
*
*/

Public class wordcount {
 
Public static void main (string [] ARGs ){
String file = "1.txt"; // english file (set the file address based on your path)

// Count the number of words
Map <string, string> wordtimesmap = countwordtimes (File );

// Number of times a word is output
Display (wordtimesmap );
}

/**
* Display the number of times a word appears
*
* @ Param wordtimesmap Map
*/
Private Static void display (Map <string, string> wordtimesmap ){
// Obtain the key value in the map.
Set <string> set = wordtimesmap. keyset ();

// Replace set with an Array
String [] OBJ = set. toarray (New String [0]);

// Sort keys and use anonymous internal classes to implement the comparator Interface
Arrays. Sort (OBJ, new comparator <string> (){
Public int compare (string O1, string O2 ){

Return o1.length () = 0? O1.compareto (O2) :( o1.length ()-o2.length ());

}
});

// Output the word and number of occurrences
For (INT I = 0; I <obj. length; I ++ ){
String times = wordtimesmap. Get (OBJ [I]);
System. Out. println (OBJ [I] + "---->" + times );
}

}

 

/**
* Count the number of times a word appears in a given file
*
* @ Param Str
*/
Private Static Map <string, string> countwordtimes (string file ){
Map <string, string> wordtimes = new treemap <string, string> (); // the number of times a word appears.

// Read the file stream
Bufferedreader in = NULL;
String line = ""; // Save the read row
Stringtokenizer strline = NULL;
Try {
In = new bufferedreader (New filereader (File ));
While (line = in. Readline ())! = NULL ){
Strline = new stringtokenizer (line ,",.;:()-!? /'"); // Separate words
While (strline. hasmoreelements ()){
String word = strline. nexttoken (). tolowercase (); // retrieve each word to ignore case sensitivity.
String times = wordtimes. Get (Word); // obtain the number of times a word appears in the map.
If (Times = NULL ){
Times = "1"; // appears for the first time
} Else {
Int n = integer. parseint (times); // converts the number of times to the int type.
+ + N; // if the word already exists, add 1 to the original number of times.
Times = "" + N;
}
Wordtimes. Put (word, times); // put it back in Map
}
}
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (in! = NULL ){
Try {
In. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
In = NULL;
}
}
Return wordtimes;
}
}

 

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.