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