(second week) New English word frequency statistics

Source: Internet
Author: User
Tags net time

Demand analysis

1. Two methods of reading files (two classes):

Small text input, command line input file name

2, the word frequency statistics

3, the results are sorted and output

Https:https://git.coding.net/yanzouzhe/ywcptj.git

Ssh:[email Protected]:yanzouzhe/ywcptj.git

function implementation

1, small text input read file

 Public classArticle {/**     * @paramargs*/String content;//Save the contents of an articleString[] Rawwords;//save delimited collection of wordsString[] words;//save a collection of words after statistics    int[] wordfreqs;//Save Word frequency for words//Total Statistics    intTotal = 0; //constructor: Enter the contents of the article     Publicarticle () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter the contents of the small file:"); String nr=Sc.nextline (); Content=nr; }

2. command line input filename Read file

 Public classArticle2 {/**     * @paramargs*/String content;//Save the contents of an articleString[] Rawwords;//save delimited collection of wordsString[] words;//save a collection of words after statistics    int[] wordfreqs;//Save Word frequency for wordsString FileName;//file name//Total Statistics    intTotal = 0; //constructor: Enter the contents of the article     PublicArticle2 ()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 content=NewStringBuffer ();//Dynamic string ArrayString temp =Bf.readline ();  while(Temp! =NULL) {content.append (temp+" ");//adding data to a dynamic string arraytemp =Bf.readline (); if(temp = =NULL){               Break; }        }         This. Content =content.tostring (); }

3, two classes in the same word-breaker method

    // to save the result in    a rawwords array by breaking the word by delimiter  Public void Splitword () {        //  participle, because punctuation is not involved, so all symbols are replaced with a space        finalChar SPACE = ';         = Content.replace (' \ ' ', space). Replace (', ', space). Replace ('. ', space).         = Content.replace (' (', Space) '. Replace (') ', space). Replace ('-', space)        ; = Content.split ("\\s+"); // all spaces separated by a word, the above replaced ', so I ' ve was divided into two words    }

Methods of the same statistic words in 4 and two classes

    // statistical words, convenient arrays     Public list<string> Sort () {        //  put all occurrences of the string into a unique list, not map, because the map search efficiency is too low         New arraylist<string>();          for (String word:rawwords) {            list.add (word);        }        Collections.sort (list);         return list;    }

The same method of word frequency sorting in 5 and two classes

    //sort the word array and frequency array in descending order according to the frequency of words     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; }

6, the method of output results in small text input

     Public voidrun () {//Split TextSplitword (); //Statistical Frequencylist<map.entry<string,integer>> list =Countwordfreq (); //Print the total number of frequenciesSYSTEM.OUT.PRINTLN ("Total word frequency:" + This. Total); System.out.println ("Frequency Statistics Information:"); //Print Statistic Frequency         for(map.entry<string,integer>mapping:list) {System.out.println (Mapping.getkey ()+" : "+Mapping.getvalue ()); }    }    //function of the test class     Public Static voidMain (string[] args) {//TODO auto-generated Method StubArticle A =Newarticle ();        A.run (); }

7, the command line input file name similar output results method, because of cmd execution, so can not display all the word frequency, only keep the first 10 of the high frequency of the display

     Public voidrun () {//Split TextSplitword (); //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{Article2 article2=NewArticle2 ();        Article2.run (); }

Run results

1. Small text input

2. Enter the file name by command line

Word Frequency Statistics PSP table

Project: English word frequency statistics

Project Type: Individual project

Completion of project: Completed

Project improvements: Changed changes

Project Date: 2016.9.11-2016.9.12

Category C C content s start time St End Time I interrupt Time T net time
Analysis requirements, design 10:00 12:30 0:30 120
Coding Java Authoring 14:00 22:00 4:30 210
Document Essay 19:30 20:40 0:20 50
Discuss Check the essay format 20:40 21:00 0 20

PSP2.1 Task Time (Hours)
Planning Plan: Complete input small text statistics word frequency Plan 2 hours
Schedule: Complete command line input filename statistics Word frequency Plan 3 hours
Analysis Demand analysis 2 hours
Coding Specific code 3.5 hours
Test Test function (test, modify code) 1 hours

Teacher, I can only understand the extent of your homework requirements, if there is a problem, please forgive me.

(second week) New English word frequency statistics

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.