UITextChecker tells me that completely ridiculous things have no spelling mistakes

57 views Asked by At

I am trying to check if something is a valid word by spellchecking it with UITextChecker. Here's my method for checking:

func isReal(word: String) -> Bool {
    let checker = UITextChecker()
    let range = NSMakeRange(0, word.utf16.count)
    let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en_US")
    return misspelledRange.location == NSNotFound
}

This is returning that things like these are spelled correctly/are valid words:

  • aabjjf
  • aabolt
  • aabqtz
  • aaeupp
  • aafoxm
  • aafwkk

What is going wrong? How can I fix this?

EDIT: This seems to be a duplicate of another question, but the other question doesn't actually have an answer how to fix this, it just identifies the problem. I just thought about it a bit and came up with a possible solution that seems to work by filtering out gibberish text by using the UITextChecker guesses method. See updated code below:

func isReal(word: String) -> Bool {
    let checker = UITextChecker()
    let range = NSMakeRange(0, word.utf16.count)
    let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en_US")

    if misspelledRange.location == NSNotFound {
        //will hit this if it is a real word OR it can't determine that it's a mispelled word, so for a correctly spelled real word, guesses will have suggestions, for nonsense it will return nothing
        if let guesses = checker.guesses(forWordRange: range, in: word, language: "en_US"){
            if guesses.count > 0 {
                return true
            }
            else {
                return false
            } //gobblyguk
        }
        //nil guesses
        return false
    }
    else {
        return false
    }
    //return misspelledRange.location == NSNotFound  //doing it this way will treat gobblyguk like it's a word
}

EDIT 2: it seems this still doesn't work, because some words that ARE real will have suggestions e.g.:

  • guesses for twin, = ["win", "thin", "twit", "twins", "tin", "twine", "twig", "twi", "twain"]

but words that come back NOT MISPELLED, but are nonsense will also still have suggestions e.g.:

  • guesses for nntp, = ["ninth"]
  • guesses for frcp, = ["force", "farce", "frap", "rcp"]

not sure how to continue.. I tried only saying something is a valid word if when it is considered NOT MISPELLED to check if its guesses contains itself, but that doesn't always work...

works for full:

  • guesses for full, = ["Full", "fill", "fall", "fell", "pull", "fuel", "fully", "dull", "bull", "flu", "hull", "cull", "null", "lull", "gull", "mull", "fulls", "furl", "fula"]

but not for fume:

  • guesses for fume, = ["fame", "fuse", "fumes", "fumed", "flume", "fumet", "fuze"]
0

There are 0 answers