Second week-word frequency statistics update

Source: Internet
Author: User

The word Frequency statistics feature adds:

Https:https://git.coding.net/li_yuhuan/wordfrequency.git

Ssh:[email Protected]:li_yuhuan/wordfrequency.git

Code:

Static voidMain (string[] args) {            stringstr =""; intLength =args.                        Length; Switch(length) { Case 0:                    {                        stringline =Console.ReadLine ();                        Frequency (line);  Break; }                 Case 1: {str= M_workpath + args[0] +". txt"; if(file.exists (str)) {LoadFile (str);                        Dictionarysort (m_wordlist); }                         Break; }                 Case 2:                    {                        if("- S"= = args[0]) {str= args[1]; if(file.exists (str)) {LoadFile (str);                            Dictionarysort (m_wordlist); }                        }                        Else if("dir"= = args[0])                        {                            if(Directory.Exists (args[1]) {m_top=Ten; M_pathlist.addrange (Directory.GetFiles (args[1],"*.txt")); intindex; foreach(stringPathinchm_pathlist) {Index= path. LastIndexOf ("\\"); if(Index >0)                                    {                                        stringName = path. Substring (Index +1, Path. Length-index-1);                                    Console.WriteLine (name);                                    } LoadFile (path);                                Dictionarysort (m_wordlist); }                            }                        }                         Break; }                default:                    {                         Break; }            }        }

Determine the number of parameters to be passed to the main function via the console;

1. When there are no parameters, the total number of words in the input text is counted and the frequency is sorted;

2. There is a parameter to determine whether the current working directory exists as the name of the TXT file, the existence of the total number of words in the statistical file and frequency and sequencing;

3. There are two parameters:

1) When the parameter is-S + file, determine whether the file exists, count the number of words and order;

2) When the parameter is dir + path, determine whether the path exists, respectively, the total number of words in all txt files in the path, frequency, and sorting;

--------------------------------------------------------------------------------------------------------------- -----------

        Static Private voidLoadFile (stringfilepath) {            stringline =string.                        Empty; using(StreamReader reader =NewStreamReader (filepath)) { Line=Reader.                ReadLine ();  while(Line! =NULL) {Frequency (line); Line=Reader.                ReadLine (); }            }        }

Read the file by line and make statistics on the line;

--------------------------------------------------------------------------------------------------------------- -----------

Static Private voidFrequency (stringLine ) {List<string> words =Newlist<string>(); stringWord =string.            Empty; Char[] split = {' ',',','?','? ','.','. ','-','-','"',':',':','\ r','\ n','(',')','"','"' }; Words. AddRange (line.            Split (split)); foreach(stringStrinchwords) {                if(str! =""&& str! =NULL) {Word=Str.                    ToLower (); if(M_wordlist.containskey (Word)) {M_wordlist[word]+=1; }                    Else{m_wordlist.add (Word,1); }                }            }        }

The incoming row is divided into a list, the list is traversed to compare statistics, data deposited dictionary;

--------------------------------------------------------------------------------------------------------------- -----------

Static Private voidDictionarysort (dictionary<string,int>dictionary) {            if(Dictionary. Count >0) {List<KeyValuePair<string,int>> LST =Newlist<keyvaluepair<string,int>>(dictionary); Lst. Sort (Delegate(keyvaluepair<string,int> S1, keyvaluepair<string,int>S2) {                    returnS2.Value.CompareTo (S1.                Value);                }); Dictionary.                Clear (); Console.WriteLine (" Total"+ LST. Count +"words\n"); intK =0; foreach(keyvaluepair<string,int> kvpinchlst) {                    if(K <m_top) {Console.WriteLine (KVP). Key+":"+kvp.                        Value); K++; }} Console.WriteLine ("----------------------------\ n"); }        }    }

The key value pairs in the dictionary are stored in list and sorted by list;

--------------------------------------------------------------------------------------------------------------- -----------

To run the example:

Function 1:

--------------------------------------------------------------------------------------------------------------- -----------

Function 2:

--------------------------------------------------------------------------------------------------------------- -----------

Function 3:

--------------------------------------------------------------------------------------------------------------- -----------

Feature 4: (not completed)

Second week-word frequency statistics update

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.