Implementing a Natural Language Classifier in iOS with Keras + Core ML

(Jeff_L) #1

Sample iOS App


Once imported, the Swift wrappers les above, using this SwiftNLC


Core ML model, will be easy to execute, as in following code:


1 2 3 4 5 6 7 8 9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

import Foundation
import CoreML

class SwiftNLCModel {
lazy var bagOfWords: BagOfWords = {
return try! JSONDecoder().decode(BagOfWords.se
}()

lazy var intents: [String] = {
return try! JSONDecoder().decode(Array<String>
}()

var lemmatizer = Lemmatizer()

func predict(_ utterance: String) -> (String, Floa
let lemmas = lemmatizer.lemmatize(text: uttera
let embedding = bagOfWords.embed(arrayOfWords:

let model = SwiftNLC()

let size = NSNumber(value: embedding.count)
let mlMultiArrayInput = try! MLMultiArray(shap

for i in 0 ..<size.intValue {
mlMultiArrayInput[i] = NSNumber(floatLiter
}

let prediction = try! model.prediction(input:

1 2 3 4 5 6 7 8

let model = SwiftNLCModel()

@IBAction func go(_ sender: Any) {
if let prediction = model.predict(commandField
intentLabel.text = "\(prediction.0) (\(Str
}
else {
i t tL b lt t " "
Free download pdf