UPDATE计数重复的单词/句子
对不起,我有一点英语。
我想对字符串中的短语进行计数。
我的字符串在下面;
Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。法无 venenatis,Lorem存有 augue德维尔pellentesque 坐阿梅德,Lorem存有悲拉克丝egestas, 等存有悲法无。
我想在下面;
3倍Lorem存有
2X 坐阿梅德
UPDATE计数重复的单词/句子
对不起,我有一点英语。
我想对字符串中的短语进行计数。
我的字符串在下面;
Lorem存有悲坐阿梅德,consectetur adipiscing ELIT。法无 venenatis,Lorem存有 augue德维尔pellentesque 坐阿梅德,Lorem存有悲拉克丝egestas, 等存有悲法无。
我想在下面;
3倍Lorem存有
2X 坐阿梅德
第一,它不是很清楚你所说的 “重复的话” 的意思,但我”猜测你需要将逗号分隔的单词或短语列表拆分为单个单词,并对每个单词进行测试WN。如果多数民众赞成的情况下:
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;
}
谢谢。我更新了我的问题。 – user3186216
有关使用LINQ如何?
var wordList = words.Split(new[] { " ", ".", "," }, StringSplitOptions.RemoveEmptyEntries)
.GroupBy(x => x)
.ToDictionary(g => g.Key, g => g.Count());
谢谢。我尝试过但包含了一个词,例如“lorem”。我想“lorem ipsum”。 – user3186216
发布您尝试过的代码可能会让其他人建议更好的方法(而不是浪费时间建议您尝试并丢弃的方法)或改进您的功能。然而,我认为一个正则表达式和计数匹配的次数应该可以正常工作。 – Tim
我更新了问题。 – user3186216
你是否提前知道你在找什么短语?或者你是否需要逻辑来查出每个可能的多字词?如果是后者,最多是两个字吗?最小值是多少? – erikrunia