Calculate the number of occurrences of a word--linq

Source: Internet
Author: User

1, directly to the code: the Declaration of data, can also be a TXT file, through the static method of the file class to read the text, and then converted to list<string> array.

         Private Staticlist<string> words =Newlist<string>             { "there"," is","a",                 "Great"," House"," and",                 " an","Amazing","Lake",                 "there"," is","a",                 "Computer","Running","a",                 "New","Query","there",                 " is","a","Great",                 "Server"," Ready"," to",                 "Process",                 "Map"," and","Reduce"};

2, the conversion of data into lookup<t,k>,lookup and dictionary dictionary a bit like, one of the important difference is that the lookup of key-value pairs can be repeated. For example, you can have key key= "key01" value value= "01" for two or more times. and dictionary in the keys can only appear once, like a child used in the same dictionary, to check the Chinese characters are always unique. The following code generates a key-value pair that consists of a word and 1.

ilookup<stringint1);

3. Extract the results, Word stores the words, count stores the number of word occurrences.

        varfrom igrouping<stringint in map                          Select  New                         {                             = Wordmap.key,                             = wordmap.count ()                         };

Reduce is the number of words to count each word, you can also add filter conditions, select the data that meets the needs. For example, if the number of words is greater than 1 and the word length is greater than 2, simply add the Where condition (red part) and the code is as follows.

           varreduce = fromigrouping<string,int> WordmapinchMapwhere(wordmap.count () >1&&wordmap.key.length>2)                         Select New{Word=Wordmap.key, Count=wordmap.count ()}; foreach(varIteminchreduce) {Console.WriteLine (item. Word+"\ t"+item.            Count); }

4, the output results. You can look at the results of the final output.

Calculate the number of occurrences of a word--linq

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.