Word Segmentation without Spaces in Swift

59 views Asked by At

I have disabled users who typically are writing without a space. I want to help with this and do word segmentation in a SwiftUI app. I can't find a natty library to do this, so I figured I would use the NaturalLanguage API to give it a try. I feel this doesn't work because of a lack of spaces.. oh the irony

import NaturalLanguage

func insertSpaces(in continuousText: String) -> String {
    let tokenizer = NLTokenizer(unit: .word)
    tokenizer.string = continuousText

    var spacedText = ""
    tokenizer.enumerateTokens(in: continuousText.startIndex..<continuousText.endIndex) { range, _ in
        spacedText += continuousText[range] + " "
        return true
    }

    return spacedText.trimmingCharacters(in: .whitespaces)
}

And running it, eg.

let spacedSentence = insertSpaces(in: 'HELLOMYFRIENDS')

Doesnt work. Am I right that this is impossible and I need to build my own dict to look at boundaries?

0

There are 0 answers