Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can LINQ be used to pull keywords out of a string?

Tags:

string

c#

linq

If I have a long-ish string of text and wanted to pull out words which are greater than 4 characters in length and found more than 4 times in the string, can LINQ do that?

like image 735
Chaddeus Avatar asked Dec 06 '22 08:12

Chaddeus


1 Answers

You may be able to tighten this up, but I believe it will be something to the effect of

var results = inputstring.Split()
                .Where(word => word.Length > 4)
                .GroupBy(word => word)
                .Where(grp => grp.Count() > 4)
                .Select(grp => grp.Key);

You will, of course, need to decide how you wish to deal with any punctuation that might be present.

So given the input

var inputstring = @"The quick brown fox jumped over the lazy dog 
               The quick brown fox jumped over the lazy dog 
               The quick fox jumped over the lazy dog 
               The quick fox jumped over the lazy dog 
               The quick brown fox jumped over the lazy dog";

The results contain "quick" and "jumped" because the only other word greater than 4 characters ("brown") appeared just 3 times.

like image 169
Anthony Pegram Avatar answered Dec 14 '22 23:12

Anthony Pegram