Minimize LINQ string token counter

后端 未结 4 1922
有刺的猬
有刺的猬 2020-12-22 04:08

Followup on answer to an earlier question.

Is there a way to further reduce this, avoiding the external String.Split call? The goal is an associative

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 04:39

    Getting rid of String.Split doesn't leave many options on the table. One option is Regex.Matches as spender demonstrated, and another is Regex.Split (which doesn't give us anything new).

    Rather than grouping you could use either of these approaches:

    var target = src.Split(new[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
    var result = target.Distinct()
                       .Select(s => new { Word = s, Count = target.Count(w => w == s) });
    
    // or dictionary approach
    var result = target.Distinct()
                       .ToDictionary(s => s, s => target.Count(w => w == s));
    

    The Distinct call is needed to avoid duplicate items. I went ahead and expanded the characters to split on to get the actual words devoid of punctuation. I found the first approach to be the quickest using spender's benchmarking code.

    Back to the requirement to order the results from your previously referenced question, you could easily extend the first approach as follows:

    var result = target.Distinct()
                       .Select(s => new { Word = s, Count = target.Count(w => w == s) })
                       .OrderByDescending(o => o.Count);
    
    // or in query form
    
    var result = from s in target.Distinct()
                 let count = target.Count(w => w == s)
                 orderby count descending
                 select new { Word = s, Count = count };
    

    EDIT: got rid of the Tuple since the anonymous type was close at hand.

提交回复
热议问题