android和ios中的单词库

时间:2018-04-28 11:15:34

标签: java android ios swift

我试图找到有关单词设备库的任何信息,检查字符串是否是实际单词,当我们使用键盘自动更正假设从某处获取单词时,是否有人知道如何访问此库?

1 个答案:

答案 0 :(得分:0)

UITextChecker类可能是您在iOS上寻找的内容 https://developer.apple.com/documentation/uikit/uitextchecker

示例实施:

func isActualWord(wordToCheck: String) -> Bool {

    let textChecker = UITextChecker()
    let language = "en"
    let range = NSRange(location: 0, length: wordToCheck.utf16.count)
    let query = textChecker.rangeOfMisspelledWord(in: wordToCheck, range: range, startingAt: 0, wrap: false, language: language)
    return query.location == NSNotFound
}

isActualWord(wordToCheck: "Swift") //true
isActualWord(wordToCheck: "Swifty") //false

func suggestedWords(wordToCheck: String) -> [String]? {

    let textChecker = UITextChecker()
    let language = "en"
    let range = NSRange(location: 0, length: wordToCheck.utf16.count)
    let suggestedWords = textChecker.guesses(forWordRange: range, in: wordToCheck, language: language)
    return suggestedWords
}

suggestedWords(wordToCheck: "Swifty") // ["Swiftly", "Swifts", "Shifty", "Swift"]