Text classification is task of categorising text according to its content. It is the fundamental problem in the field of Natural Language Processing(NLP). More general applications of text classifications are in email spam detection, sentiment analysis and topic labelling etc. For this post we restrict our text classification to only two classes also know as binary classification e.g into positive or negative text. For integrating text classification in iOS we will be using a pre-trained model on IMDB database. We will be using as out underlaying machine learning inference engine. tensorflowlite In its simplest form our app will take a sentence as input from user, feeds into classification client, that will classify text as either positive or negative depending on text content. Lets name our classification client as . Here is an overview of the client class TextClassificationClient { ?(modelFileInfo: , labelsFileInfo: , vocabFileInfo: ) { } } final class TextClassificationClient // other stuff init FileInfo FileInfo FileInfo // we will initialise the tensorflowlite here The initialiser takes three parameters of type FileInfo which is a tuple defined below modelFileInfo = (name: , let FileInfo "text_classification" : " ") = ( : " ", : " ") = ( : " ", : " ") extension tflite let labelsFileInfo FileInfo name labels extension txt let vocabFileInfo FileInfo name vocab extension txt Model file is the trained model file which tensorflowlite takes to perform inference. Labels file is plain text file consisting of classes of classification e.g Positive and Negative. Vocab is again a plain text file containing the collection of some words and their corresponding embedding, these words were used during model training and also will be used during inference. Model training is outside of scope of this post and as I mentioned in start we will use a pre-trained IMDB model from . For detail overview about model training please see . here here In order to use tensorflowlite in iOS app we have to integrate it. For this purpose we will be using cocoapods. is the most commonly used dependency management tool used for managing third party dependencies in iOS apps. Cocopods Add pod ‘ ’ in your project pod file and run pod install to install and integrate tensorflowlite in iOS app. TensorFlowLiteSwift Let's get back to our class. In this class we have a method classify that will actually classify the text. Below is the implementation of this method TextClassificationClient -> [ ] { input = tokenizeInputText(text: text) data = (copyingBufferOf: input[ ]) { interpreter.copy(data, toInputAt: ) interpreter.invoke() outputTensor = interpreter.output(at: ) outputTensor.dataType == .float32 { outputArray = [ ](unsafeData: outputTensor.data) ?? [] output = [ ]() (index, label) labels.enumerated() { output.append( (id: , title: label, confidence: outputArray[index])) } output. (by: >) output } } { (error) } [] } func classify (text: String) Result let let Data 0 do try 0 try let try 0 if let Float var Result for in Result "" sort return catch print return It takes a string as parameter and return an array of Result type. The first line takes string and converts it into tokens in order to feed it into inference engine(interpreter instance variable in this case). We run interpreter on tokenised text get the output from interpreter and return it. The Result type is defined as below { id: title: confidence: } struct Result let String let String let Float Below is the screenshot of an inference The complete working code of app can be found at Github repository . here