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

2 Answers

0 votes
import Foundation

func groupByFirstNLetters(_ s: String, n: Int = 3) -> [String: [String]] {
    let regex = try! NSRegularExpression(pattern: "[A-Za-z]+")
    let lower = s.lowercased()
    let range = NSRange(lower.startIndex..<lower.endIndex, in: lower)

    var groups: [String: [String]] = [:]

    for match in regex.matches(in: lower, range: range) {
        if let r = Range(match.range, in: lower) {
            let word = String(lower[r])
            if word.count >= n {
                let prefix = String(word.prefix(n))
                groups[prefix, default: []].append(word)
            }
        }
    }

    return groups
}

let s = "The lowly inhabitants of the lowland were surprised to see the lower branches of the trees."

let groups = groupByFirstNLetters(s, n: 3)

// Print version 1
for (prefix, words) in groups {
    print("\(prefix) :", words)
}

print()

// Print version 2
for (prefix, words) in groups {
    print("\(prefix): \(words.joined(separator: ", "))")
}



/*
run:

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

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

*/

 



answered Mar 13 by avibootz
0 votes
import Foundation

func groupByFirstNLetters(_ s: String, n: Int = 3) -> [String: [String]] {
    let regex = try! NSRegularExpression(pattern: "[A-Za-z]+")
    let lower = s.lowercased()
    let range = NSRange(lower.startIndex..<lower.endIndex, in: lower)

    let words = regex.matches(in: lower, range: range)
        .compactMap { Range($0.range, in: lower).map { String(lower[$0]) } }
        .filter { $0.count >= n }

    return Dictionary(grouping: words) { String($0.prefix(n)) }
}

let s = "The lowly inhabitants of the lowland were surprised to see the lower branches of the trees."

let groups = groupByFirstNLetters(s, n: 3)

// Print version 1
for (prefix, words) in groups {
    print("\(prefix) :", words)
}

print()

// Print version 2
for (prefix, words) in groups {
    print("\(prefix): \(words.joined(separator: ", "))")
}



/*
run:

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

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

*/

 



answered Mar 13 by avibootz
...