Statistical word Frequency

Source: Internet
Author: User

Today, the teacher arranged a task: count the frequency of the most occurrences of several words in a file.

How to say, or not, only the Internet search, through two hours of struggle, I still did not make it, but I know how to read the information from the file code and how to count the word frequency of the code; However, I can not put them together, nor understand the role of many code, just know its function, so I think, I should still borrow a book to carry, and then spend more time to learn java.

Tonight after three hours, and search for a search to think, but also some similar code for comparison, but I still can not change the code, I think I am incompetent, feel three hours is useless, nothing, also began to panic, really really should work hard to learn java.

Here is the code for my non-implemented features:

ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileReader;Importjava.util.Collections;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.TreeMap; Public classTest { Public Static voidfindenglishnum (String text) {//Find out all the wordsString[] Array = {".", "", "?", "!"};  for(inti = 0; i < Array.Length; i++) {text= Text.replace (Array[i], ","); } string[] Textarray= Text.split (","); //Traversing Recordsmap<string, integer> map =NewHashmap<string, integer>();  for(inti = 0; i < textarray.length; i++) {String key=Textarray[i]; //Convert to lowercaseString key_l =key.tolowercase (); if(!"". Equals (key_l)) {Integer num=Map.get (key_l); if(num = =NULL|| num = = 0) {map.put (key_l,1); }Else if(num > 0) {map.put (key_l, Num+1); }                }            }            //Output to consoleSYSTEM.OUT.PRINTLN ("The frequency at which each word appears:"); Iterator<String> iter =Map.keyset (). iterator ();  while(Iter.hasnext ()) {String key=Iter.next (); Integer Num=Map.get (key); SYSTEM.OUT.PRINTLN (Key+ "\n\t\t" + num + "times \ n-------------------"); }        }                                       Public Static voidMain (string[] args) {fread ("G:\\yingyu.txt"); Findenglishnum ()}//Read file:     Public Static voidfread (String fileurl) {File file=NewFile (FileURL); BufferedReader BFR=NULL; Try{BFR=NewBufferedReader (Newfilereader (file)); String tem=NULL; String value= "";  while(tem = Bfr.readline ())! =NULL) {Value= value +tem;            } System.out.println (value); //converts a read string into a character array:            Char[] C =Value.tochararray (); //define a map to store the results://hashmap<character,integer> TM = new//hashmap<character,integer> (Collections.reverseorder ());Treemap<character, Integer> TM =NewTreemap<character, Integer> (Collections.reverseorder ());//TreeMap sortable (passing in a reversal comparator)             for(inti = 0; i < c.length; i++) {                CharCHARSRC =C[i]; if(Tm.containskey (CHARSRC)) {//determines whether the value of the key exists                    intCount =Tm.get (CHARSRC); Tm.put (CHARSRC, Count+ 1); } Else{tm.put (CHARSRC,1); }            }            //remove the keys and values from the mapIterator<map.entry<character, integer>> titer =Tm.entryset (). iterator ();  while(Titer.hasnext ()) {Map.entry<character, integer> map =Titer.next (); CharKey =Map.getkey (); intValu =Map.getvalue (); SYSTEM.OUT.PRINTLN (Key+ "appeared" + Valu + "Times!"); }        } Catch(Exception e) {System.err.println ("File read error"); } finally {            Try {                if(BfR! =NULL) {bfr.close (); }            } Catch(Exception E2) {System.err.println ("File Close Error"); }        }     }}
ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileReader;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Scanner; Public classTongji { Public StaticString txt2string (file file) {StringBuilder result=NewStringBuilder (); Try{bufferedreader br=NewBufferedReader (NewFileReader (file));//constructs a BufferedReader class to read the fileString s =NULL;  while((s = br.readline ())! =NULL){//use the ReadLine method to read one line at a timeResult.append (System.lineseparator () +s);                } br.close (); }Catch(Exception e) {e.printstacktrace (); }            returnresult.tostring (); }     //method for reading file information.                                        Public Static voidfindenglishnum (String text) {//Find out all the wordsString[] Array = {".", "", "?", "!"};  for(inti = 0; i < Array.Length; i++) {text= Text.replace (Array[i], ","); } string[] Textarray= Text.split (","); //Traversing Recordsmap<string, integer> map =NewHashmap<string, integer>();  for(inti = 0; i < textarray.length; i++) {String key=Textarray[i]; //Convert to lowercaseString key_l =key.tolowercase (); if(!"". Equals (key_l)) {Integer num=Map.get (key_l); if(num = =NULL|| num = = 0) {map.put (key_l,1); }Else if(num > 0) {map.put (key_l, Num+1); }               }           }           //Output to consoleSYSTEM.OUT.PRINTLN ("The frequency at which each word appears:"); Iterator<String> iter =Map.keyset (). iterator ();  while(Iter.hasnext ()) {String key=Iter.next (); Integer Num=Map.get (key); SYSTEM.OUT.PRINTLN (Key+ "\n\t\t" + num + "times \ n-------------------"); }       }                     Public Static voidMain (string[] arg) {File file=NewFile ("G:/yingyu.txt");                                            System.out.println (txt2string (file)); String ABC= "Welcome Welcome to Adempiere, a commons-based peer-production of Open Source ERP applications. This Wiki was for the global community to contribute and share know-how and domain expertise. We hope you can find as much open information and participate in making it most usable for everyone. This project have a bazaar of citizens with a Community council team which work in thefunctional team and Technical team AL Ong the software development Procedure supported and funded by the Foundation Adempiere ";                    Findenglishnum (ABC); }}

Statistical word Frequency

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.