func loadVocab()

in TextClassificationOnMobile/iOS/TextClassificationStep2/TextClassificationStep2/ViewController.swift [49:70]


    func loadVocab(){
        // This func will take the file at vocab.txt and load it into a has table
        // called words_dictionary. This will be used to tokenize the words before passing them
        // to the model trained by TensorFlow Lite Model Maker
        if let filePath = Bundle.main.path(forResource: "vocab", ofType: "txt") {
            do {
                let dictionary_contents = try String(contentsOfFile: filePath)
                let lines = dictionary_contents.split(whereSeparator: \.isNewline)
                for line in lines{
                    let tokens = line.components(separatedBy: " ")
                    let key = String(tokens[0])
                    let value = Int(tokens[1])
                    words_dictionary[key] = value
                }
            } catch {
                print("Error vocab could not be loaded")
            }
        } else {
            print("Error -- vocab file not found")
            
        }
    }