Ken'ichi Matsuyama
Ken'ichi Matsuyama

Reputation: 369

C# File to Dictionary, but taking pairs of words

I am thinking about making a dictionary that contains words pairs as well as single words from a file.

Standard "single word" looks like:

private Dictionary<string, int> tempDict = new Dictionary<string, int>();
private void GetWords(string[] file)
{ 
   tempDict = file
   .SelectMany(i => File.ReadLines(i)
   .SelectMany(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)))
   .GroupBy(word => word)                    
   .ToDictionary(g => g.Key, g => g.Count());
}

And the string:

Adam likes coffee

will be:

Adam ; likes ; coffee

But I want to make it so it matches pairs as well (but only the neighbouring ones) so it would look like:

Adam ; Adam likes ; likes ; likes coffee ; coffee

I am not sure if it is manageable to do, and need some help with this one.

Upvotes: 2

Views: 232

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

MoreLINQ has a Enumerable.Pairwise which takes the current and the predecessor value and a projections function.

Returns a sequence resulting from applying a function to each element in the source sequence and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element.

Concatenating that with the original split value array would output:

var sentence = "Adam likes coffee";
var splitWords = sentence.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var pairWise = splitWords.Pairwise((first, second) => string.Format("{0} {1}", first,
                                                                               second))
                         .Concat(splitWords)
                         .GroupBy(x => x)
                         .ToDictionary(x => x.Key, x => x.Count())

Would result in:

Output

Upvotes: 3

Related Questions