计算重复的单词/句子

时间:2014-01-11 23:02:03

标签: asp.net c#-2.0

更新

抱歉,我的英语很少。

我想用字符串计算短语。

我的字符串在下面;

  

Lorem ipsum dolor 坐下来,奉献精神。法无   venenatis, lorem ipsum augue vel pellentesque sit amet lorem ipsum dolor egestas lacus,   et ipsum dolor nulla。

我想在下面;

  
      
  • 3x Lorem ipsum

  •   
  • 2x 坐下来

  •   

2 个答案:

答案 0 :(得分:1)

首先,它不太清楚“重复的单词”是什么意思,但我猜你需要将逗号分隔的单词或短语列表分成单个单词,单独测试每个单词或单词。如果是这样的话:

string words = "I love red color. He loves red color. She love red kit. ";
myWordString = myWordString .Replace(" ", ",");
myWordString  = myWordString .Replace(".", "");

string[] words = s.Split(',');
foreach (string theWord in words)
{
    // now do something with them individually
}

使用词典

Dictionary<string, Int32> wordList= new Dictionary<string, Int32>();

然后一旦你得到单词字符串,循环你的字符串中的单词,并在每个循环中,你可以添加到字典,或增加计数

-- psuedo loop code from above 

if (wordList.ContainsKey(theWord)) {

    wordList[theWord] = wordList[theWord] + 1;

} else {

    wordList.Add(theWord, 1);

}

-- end psuedo loop code from above

依此类推。当你的循环完成后,列表中的所有单词......你可以像这样翻阅字典:

foreach(var pair in wordList)
{
    var key = pair.Key;
    var value = pair.Value;
}

答案 1 :(得分:0)

如何使用LINQ?

var wordList = words.Split(new[] { " ", ".", "," }, StringSplitOptions.RemoveEmptyEntries)
                    .GroupBy(x => x)
                    .ToDictionary(g => g.Key, g => g.Count());