Text proofreading guide for iOS

The MediaPipe Text Proofreader task lets you identify spelling, grammatical, and stylistic errors in a text and generate a corrected version while maintaining the original meaning. These instructions show you how to use the Text Proofreader in iOS apps.

For more information about the capabilities, models, and configuration options of this task, see the Overview.

Code example

The Text Proofreader iOS example app demonstrates the API on a physical iOS device or simulator.

You can use the app as a starting point for your own iOS app, or refer to it when modifying an existing app. You can refer to the Text Proofreader example code on GitHub.

Setup

This section describes key steps for setting up your development environment and code projects to use Text Proofreader on iOS. For general information on setting up your development environment for using MediaPipe tasks, including platform version requirements, see the Setup guide for iOS.

Dependencies

Text Proofreader uses the MediaPipeTasksText library, which must be installed using CocoaPods. The library is compatible with Swift apps and does not require any additional language-specific setup.

For instructions to install CocoaPods on macOS, refer to the CocoaPods installation guide. For instructions on how to create a Podfile with the necessary pods for your app, refer to Using CocoaPods.

Add the MediaPipeTasksText pod in the Podfile using the following code:

target 'MyTextProofreaderApp' do
  use_frameworks!
  pod 'MediaPipeTasksText'
end

Model

The MediaPipe Text Proofreader task requires a trained model that is compatible with this task. For more information on available trained models for Text Proofreader, see the task overview Models section.

Select and download a LiteRT model, and add it to your project directory using Xcode. For instructions on how to add files to your Xcode project, refer to Managing files and folders in your Xcode project.

Use the BaseOptions.modelAssetPath property to specify the path to the model in your app bundle. For a code example, see the next section.

Create the task

You can create the Text Proofreader task by calling one of its initializers. The TextProofreader(options:) initializer accepts values for the configuration options.

The following code demonstrates how to build and configure this task.

import MediaPipeTasksText

guard let modelPath = Bundle.main.path(forResource: "proofreader",
                                       ofType: "litertlm") else { return }

let options = TextProofreaderOptions()
options.baseOptions.modelAssetPath = modelPath
let textProofreader = try TextProofreader(options: options)

Configuration options

This task has the following configuration options for iOS apps:

Option Name Description Value Range / Type Default Value
maxTokens The maximum number of tokens for proofread tasks. If set, the proofread output will be truncated if the input and output exceed this value. If not set (or equal to 0), the default limit is decided by the model capacity (8k). Integer 0

Run the task

To run the proofreading inference on the input text, you can use the proofread(_:) method of TextProofreader for blocking inference, or the proofreadStreaming(_:completion:) method for asynchronous callbacks.

Synchronous

let result = try textProofreader.proofread(text)

Streaming (Asynchronous callbacks)

try textProofreader.proofreadStreaming(text) { streamResult, error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
        return
    }

    guard let streamResult = streamResult else { return }

    // Receive subsequent outputs
    print(streamResult.chunk)

    if streamResult.done {
        print("Completed proofreading!")
        // Optional: Access streamResult.corrections to see exactly what changed.
    }
}

Handle and display results

Upon running inference synchronously, the Text Proofreader returns an instance containing the complete string in the proofreadText property, alongside a detailed list of granular corrections.

let finalOutput = result.proofreadText

// You can also access the granular corrections
let corrections = result.corrections
for correction in corrections {
    // Type is .same, .insertion, or .deletion
    let type = correction.type
    let segment = correction.text
}

When running in stream mode, the callback continually spits out TextProofreaderStreamResult, containing .chunk (the next text partial), and a .done boolean determining stream completion status. When .done is true, the .corrections array will also be populated with the final list of changes.

Clean Up

Don't forget to explicitly close the proofreader engine instances when finished.

try textProofreader.close()