c# - get word frequency (count) as property in Linq Object using Regex -


so i'm trying take this post , tweak own purposes can't figure out how.

here starting query:

     string input = sb.tostring();             string[] keywords = new[] { "i","be", "with", "are", "there", "use", "still", "do","out", "so", "will", "but", "if", "can", "your", "what", "just", "from", "all", "get", "about", "this","t", "is","and", "the", "", "a", "to", "http" ,"you","my", "for", "in", "of", "ly" , "com", "it", "on","s", "that", "bit", "at", "have", "m", "rt",  "an", "was", "as", "ll", "not", "me" };             regex regex = new regex("\\w+"); var stuff = regex.matches(input)                 .oftype<match>()                 .select(c => c.value.tolowerinvariant())                 .where(c => !keywords.contains(c))                 .groupby(c => c)                 .orderbydescending(c => c.count())                 .thenby(c => c.key); 

but able count (frequency) of each key value value can store in database.

foreach (var item in stuff)             {                 string query = string.format("insert sg_top_words (sg_word, sg_count) values ('{0}','{1}')", item.key, item.count???);                 cmdins = new sqlcommand(query, conn);                 cmdins.commandtype = commandtype.text;                 cmdins.executenonquery();                 cmdins.dispose();             } 

thanks

assuming query nearly you're after, tweak should it:

var stuff = regex.matches(input)     .cast<match>() // we're confident match!     .select(c => c.value.tolowerinvariant())     .where(c => !keywords.contains(c))     .groupby(c => c)     .select(g => new { word = g.key, count = g.count() })     .orderbydescending(g => g.count)     .thenby(g => g.word); 

now sequence of anonymous type, key , count properties.

do need order results though, if you're inserting them database? use this:

var stuff = regex.matches(input)     .cast<match>() // we're confident match!     .select(c => c.value.tolowerinvariant())     .where(c => !keywords.contains(c))     .groupby(c => c)     .select(g => new { word = g.key, count = g.count() }); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -