Test Project
blog post address : [http://www.cnblogs.com/jx8zjs/p/5862269.html]
Project Address : Https://coding.net/u/jx8zjs/p/wordCount/git
ssh://[email protected]: Jx8zjs/wordcount.git
test Case :
1.
1 My 中文版 is very very pool
2. Address [Http://www.gutenberg.org/files/2600/2600-0.txt]
Unit 1: Statistics The frequency of input files to the target file
The first four lines of code are input file and output file address, file 1 is test Case 1, File 2 is test Case 2.
1String filename1 = "D://text/pool.txt";2String filename2 = "D://text/2600-0.txt";3String filenamedes1 = "D://pooltest.txt";4String filenamedes2 = "D://2600-0test.txt";5 Private StaticFilewordutil fu =Newfilewordutil ();6 7 Public voidtestprintsortedwordgroupcounttofilebufferedstringstring () {8 fu.printsortedwordgroupcounttofile (filename1, filenamedes1);9 fu.printsortedwordgroupcounttofile (filename2, filenamedes2);Ten } One A Public voidprintsortedwordgroupcounttofile (string filename, string destinationfilename) { -list<string[]> result =getsortedwordgroupcount (filename); - if(Result = =NULL) { theSYSTEM.OUT.PRINTLN ("No Result"); - return; - } - Try { +FileWriter FR =NewFileWriter (destinationfilename); - for(string[] sa:result) { +Fr.write (Sa[1] + ":" + sa[0] + "\ r \ n"); A } at fr.close (); -}Catch(IOException e) { - e.printstacktrace (); - return; - } - in}
Core Word Frequency Statistics code (2016.9.26 optimized Edition):
1 PublicMap<string, integer>getwordgroupcountbuffered (String filename) {2 Try {3FileReader FR =Newfilereader (filename);4BufferedReader br =NewBufferedReader (FR);5StringBuffer content =NewStringBuffer ("");6map<string, integer> result =NewHashmap<string, integer>();7 Char[] ch =New Char[128];8 intBS = 0;9 intidx;Ten BooleanAdded =false; One BooleanSplit =false; ATotal = 0; - while(bs = br.read (CH)) > 0) { - for(idx = 0; idx < BS; idx++) {//Char the if(Ischaracter (ch[idx]) = = 1) { - if(Split = =false) { - content.append (Ch[idx]); -Added =false; +}Else { -String key =content.tostring (). toLowerCase (); +Split =false; Atotal++; atAdded =true; -Content =NewStringBuffer (""); - content.append (Ch[idx]); - if(Result.containskey (key)) { -Result.put (Key, Result.get (key) + 1); - Continue; in}Else { -Result.put (Key, 1); to Continue; + } - } the}Else if(Ischaracter (ch[idx]) = = 2) {//Digital * if(Added = =true) { $ Continue;Panax Notoginseng}Else { - content.append (Ch[idx]); the } +}Else{//Not char or digital ASplit =true; the Continue; + } - } $ } $String key =content.tostring (). toLowerCase (); - if(Result.containskey (key)) { -Result.put (Key, Result.get (key) + 1); the}Else { -Result.put (Key, 1);Wuyi } thetotal++; - br.close (); Wu fr.close (); - returnresult; About}Catch ( $ - FileNotFoundException e) { -System.out.println ("Failed to open file:" +filename); - e.printstacktrace (); A}Catch(Exception e) { +System.out.println ("Some expection occured"); the e.printstacktrace (); - } $ return NULL; the}
Test Results :
Pooltest.txt
2600-0test.txt
Unit 2: Count the word frequency of the input file to the console or terminal
test Case 1 Results :
Unit Test Summary :
At the time of unit testing, I stumbled across the word-breaker core function in the connection mentioned above in some cases, the last word of the article is omitted, after repeated improvement and thinking rewrite the logic of the analysis of the reading character, so that the test results can be satisfied with the expected results, but I am surprised that the efficiency of the algorithm also increased by nearly 40% (The original version in the local execution time average in 490-550ms, the new version run time in 276-343ms), the reason is also introduced a new Boolean variable to help optimize the logic, but also reduced some of the decision conditions.
Project Address : Https://coding.net/u/jx8zjs/p/wordCount/git
ssh://[email protected]: Jx8zjs/wordcount.git
Java Word frequency Statistics-improved unit testing