PackageSearch;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Scanner;ImportJava.util.TreeMap; Public classUpdatewordsearch {/*** Input File Save separated words collection after saving statistics*/String article;//Save the contents of an articlestring[] rwords; String[] words; int[] wordfreqs;//Save Word frequency for wordsString filename;//file name//Total Statistics intTotal = 0; //constructor: Enter the contents of the article PublicUpdatewordsearch ()throwsIOException {Scanner sc=NewScanner (system.in); System.out.println ("Please enter the file name:"); FileName=Sc.nextline (); File File=NewFile (filename); if(!file.exists ()) {System.out.println ("File does not exist!" "); return; } BufferedReader BF=NewBufferedReader (Newfilereader (file)); StringBuffer article=NewStringBuffer ();//Dynamic string ArrayString temp =Bf.readline (); while(Temp! =NULL) {article.append (temp+ " ");//adding data to a dynamic string arraytemp =Bf.readline (); if(temp = =NULL) { Break; } } This. Article =article.tostring (); } //participle and statistic the corresponding vocabulary Public voidSWord () {//participle, because punctuation is not involved, so all symbols are replaced by spaces Final CharSPACE = '; Article= Article.replace (' \ ' ', SPACE). Replace (', '), SPACE). Replace ('. ', SPACE). replace (' \ '), SPACE); Article= Article.replace (' (', SPACE) '. Replace (') ', SPACE). Replace (‘-‘, SPACE); Rwords= Article.split ("\\s+");//all spaces separated by a word, the above replaced ', so I ' ve was divided into two words } PublicList<string>sort () {//put all occurrences of the string into a unique list, no map, because the map search efficiency is too lowlist<string> list =NewArraylist<string>(); for(String word:rwords) {list.add (word); } collections.sort (list); returnlist; } //Vocabulary Sorting PublicList countwordfreq () {//Statistical Frequency Informationmap<string, integer> wordsinfo =NewTreemap<string, integer>(); String Word= "";//Word frequency name intCount = 0;//number of Word frequency//count the total number of words intTotal = 0; List<String> wordList =sort (); Word= Wordlist.get (0); for(inti = 0; I <= wordlist.size (); i++) { if(i = =wordlist.size ()) {Wordsinfo.put (Word, count); Total++; Break; } if(Wordlist.get (i). Equals (Word)) {count++; } Else{wordsinfo.put (Word, count); Total++; Word=Wordlist.get (i); Count= 1; } } //Word frequency information sortinglist<map.entry<string, integer>> list =NewArraylist<map.entry<string, integer>>(Wordsinfo.entryset ()); Collections.sort (list,NewComparator<map.entry<string, integer>>() {@Override Public intCompare (Entry<string, integer>O1, Entry<string, integer>O2) { //TODO auto-generated Method Stub returnO2.getvalue (). CompareTo (O1.getvalue ()); } }); This. Total =Total ; returnlist; } Public voidrun () {//Split TextSWord (); //Statistical Frequencylist<map.entry<string, integer>> list =Countwordfreq (); //Print the total number of frequenciesSYSTEM.OUT.PRINTLN ("Total word frequency:"); System.out.println ("Total:" + This. Total); System.out.println ("Frequency Statistics Information:"); //Print Statistic Frequency intm = 0; for(Map.entry<string, integer>mapping:list) { if(M < 10) {System.out.println (Mapping.getkey ()+ " : " +Mapping.getvalue ()); M++; } Else Break; } } //function of the test class Public Static voidMain (string[] args)throwsIOException {updatewordsearch W=NewUpdatewordsearch (); W.run (); }}
Word Frequency Improved version