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;
}
}