您可以创建方法,建立从给出的单词短语。效率不是很高(因为跳过),但简单的实现:
private static IEnumerable<string> CreatePhrases(string[] words, int wordsCount)
{
for(int i = 0; i <= words.Length - wordsCount; i++)
yield return String.Join(" ", words.Skip(i).Take(wordsCount));
}
休息很简单 - 分割你的串入的话,建立短语,并获得原始字符串每个短语的出现:
var my_string = "In the end this is not the end";
var words = my_string.Split();
var result = from p in CreatePhrases(words, 2)
group p by p into g
select new { g.Key, Count = g.Count()};
结果:
[
Key: "In the", Count: 1,
Key: "the end", Count: 2,
Key: "end this", Count: 1,
Key: "this is", Count: 1,
Key: "is not", Count: 1,
Key: "not the", Count: 1
]
创建项目的连续组(更有效的方法适用于任何我枚举):
public static IEnumerable<IEnumerable<T>> ToConsecutiveGroups<T>(
this IEnumerable<T> source, int size)
{
// You can check arguments here
Queue<T> bucket = new Queue<T>();
foreach(var item in source)
{
bucket.Enqueue(item);
if (bucket.Count == size)
{
yield return bucket.ToArray();
bucket.Dequeue();
}
}
}
而且所有的计算可以在一个行完成:
var my_string = "In the end this is not the end";
var result = my_string.Split()
.ToConsecutiveGroups(2)
.Select(words => String.Join(" ", words))
.GroupBy(p => p)
.Select(g => new { g.Key, Count = g.Count()});
谢谢,我错过了。 – tribe84 2014-10-28 20:38:51
@GrantWinney - 不,这两个问题是相似的,但不一样。 – tribe84 2014-10-28 20:39:39
你想如何分割'input'? – 2014-10-28 20:49:59