How to group words in a string by the first N letters in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Program
{
    static Dictionary<string, List<string>> GroupByFirstNLetters(string s, int n = 3) {
        var groups = new Dictionary<string, List<string>>();
        var regex = new Regex("[A-Za-z]+");

        foreach (Match m in regex.Matches(s.ToLower())) {
            string word = m.Value;
            if (word.Length >= n) {
                string prefix = word.Substring(0, n);

                if (!groups.ContainsKey(prefix))
                    groups[prefix] = new List<string>();

                groups[prefix].Add(word);
            }
        }

        return groups;
    }

    static void Main()
    {
        string s = "The lowly inhabitants of the lowland were surprised to see " +
                   "the lower branches of the trees.";

        var groups = GroupByFirstNLetters(s, 3);

        // Print version 1
        foreach (var kv in groups)
            Console.WriteLine($"{kv.Key} : [{string.Join(", ", kv.Value)}]");

        Console.WriteLine();

        // Print version 2
        foreach (var kv in groups)
            Console.WriteLine($"{kv.Key}: {string.Join(", ", kv.Value)}");
    }
}


/*
run:

the : [the, the, the, the]
low : [lowly, lowland, lower]
inh : [inhabitants]
wer : [were]
sur : [surprised]
see : [see]
bra : [branches]
tre : [trees]

the: the, the, the, the
low: lowly, lowland, lower
inh: inhabitants
wer: were
sur: surprised
see: see
bra: branches
tre: trees

*/

 



answered Mar 13 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Program
{
    static Dictionary<string, List<string>> GroupByFirstNLetters(string s, int n = 3) {
        var regex = new Regex("[A-Za-z]+");

        var words = regex.Matches(s.ToLower())
                         .Select(m => m.Value)
                         .Where(w => w.Length >= n);

        return words
            .GroupBy(w => w.Substring(0, n))
            .ToDictionary(g => g.Key, g => g.ToList());
    }

    static void Main()
    {
        string s = "The lowly inhabitants of the lowland were surprised to see " +
                   "the lower branches of the trees.";

        var groups = GroupByFirstNLetters(s, 3);

        foreach (var kv in groups)
            Console.WriteLine($"{kv.Key}: {string.Join(", ", kv.Value)}");
    }
}



/*
run:

the: the, the, the, the
low: lowly, lowland, lower
inh: inhabitants
wer: were
sur: surprised
see: see
bra: branches
tre: trees

*/

 



answered Mar 13 by avibootz
...