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

(Jeff_L) #1

SwiftNLCModel.swift


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

29

import Foundation

struct BagOfWords: Codable {
let sortedArrayOfWords: [String]

init(setOfWords: Set<String>) {
sortedArrayOfWords = Array(setOfWords).sorted(
}

internal func binarySearch(_ word: String) -> Int?
var lowerIndex = 0
var upperIndex = sortedArrayOfWords.count - 1

while true {
let currentIndex = (lowerIndex + upperInde
if sortedArrayOfWords[currentIndex] == wor
return currentIndex
}
else if lowerIndex > upperIndex {
return nil
}
else {
if sortedArrayOfWords[currentIndex] >
upperIndex = currentIndex - 1
}
else {
lowerIndex = currentIndex + 1
}
}
Free download pdf