2012-01-26 55 views
0

這可以使用C#Linq完成嗎?獲取文字中出現的字數

例如:

吹笛人彼得挑選醃辣椒的包,辣椒是甜蜜和播種者彼得,彼得認爲

結果:

peter 3 
peppers 2 
picked 1 
... 

我可以用嵌套的for循環做,但是認爲使用Linq有一個更簡潔,資源更輕的方式。

回答

6

可以使用的GroupBy:

string original = "peter piper picked a pack of pickled peppers, the peppers were sweet and sower for peter, peter thought"; 

var words = original.Split(new[] {' ',','}, StringSplitOptions.RemoveEmptyEntries); 
var groups = words.GroupBy(w => w); 

foreach(var item in groups) 
    Console.WriteLine("Word {0}: {1}", item.Key, item.Count()); 
1
"peter piper picked a pack of pickled peppers,the peppers 
were sweet and sower for peter, peter thought" 
.Split(' ', ',').Count(x=>x == "peter"); 

這是爲「彼得」,其他人重複。

4

這應該做的伎倆:

var str = "peter piper picked a pack of pickled peppers, the peppers were sweet and sower for peter, peter thought"; 
var counts = str 
    .Split(' ', ',') 
    .GroupBy(s => s) 
    .ToDictionary(g => g.Key, g => g.Count()); 

現在,詞典,counts包含你的句子單詞數對。例如,counts["peter"]爲3

+0

你可以使用'Regex.Split(str,@「\ W」)。GroupBy ...'來簡化分詞。收縮將是一個麻煩,所以也許'[。,;:??「」\ s-]'或什麼的。 – user7116

+1

你會得到'string.Empty'的計數,因爲你有「,」在原始字符串中的部分... –

+0

@ReedCopsey由於OP說他可以在循環中做到這一點,我幾乎肯定他在'Split(...)'調用之後正在尋找該部分。不過謝謝你提到空串。 – dasblinkenlight

1

我不知道這是否是更有效或 「資源輕」,但你可以這樣做:

string[] words = "peter piper picked a pack of pickled peppers, the peppers were sweet and sower for peter, peter thought".Split(" "); 
int peter = words.Count(x=>x == "peter"); 
int peppers = words.Count(x=>x == "peppers"); 
// etc 
0
const string s = "peter piper picked a pack of pickled peppers, the peppers were sweet and sower for peter, peter thought"; 

var wordFrequency = 
     from word in s.Split(' ') 
     group word by word 
     into wordGrouping 
     select new {wordGrouping.Key, Count = wordGrouping.Count()}; 
相關問題