<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 10.14.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CreateML",
  "identifier" : "/documentation/CreateML/MLTextClassifier",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Create ML"
    ],
    "preciseIdentifier" : "s:8CreateML16MLTextClassifierV"
  },
  "title" : "MLTextClassifier"
}
-->

# MLTextClassifier

A model you train to classify natural language text.

```
struct MLTextClassifier
```

## Overview

Use a text classifier to train a machine learning model you can include in
your app to classify natural language text. The model learns to associate
labels with features of the input text, which can be sentences, paragraphs,
or even entire documents.

After you train a text classifier, you save it to a Core ML model file. You
then use an instance of the
<doc://com.apple.documentation/documentation/NaturalLanguage/NLModel> class
from the <doc://com.apple.documentation/documentation/NaturalLanguage>
framework to read the model file into your app.

## Topics

### Creating and training a text classifier

[`init(trainingData:parameters:)`](/documentation/CreateML/MLTextClassifier/init(trainingData:parameters:))

Creates a text classifier.

[`init(trainingData:textColumn:labelColumn:parameters:)`](/documentation/CreateML/MLTextClassifier/init(trainingData:textColumn:labelColumn:parameters:))

Creates a text classifier.

[`enum DataSource`](/documentation/CreateML/MLTextClassifier/DataSource)

A data source for a text classifier.

[`struct ModelParameters`](/documentation/CreateML/MLTextClassifier/ModelParameters-swift.struct)

Parameters that specify model training parameters and validation data.

[`let modelParameters: MLTextClassifier.ModelParameters`](/documentation/CreateML/MLTextClassifier/modelParameters-swift.property)

The configuration parameters that the text classifier used for training during initialization.

### Evaluating a text classifier

[`func evaluation(on:)`](/documentation/CreateML/MLTextClassifier/evaluation(on:))

Computes evaluation metrics.

[`func evaluation(on:textColumn:labelColumn:)`](/documentation/CreateML/MLTextClassifier/evaluation(on:textColumn:labelColumn:))

Computes evaluation metrics.

[`let trainingMetrics: MLClassifierMetrics`](/documentation/CreateML/MLTextClassifier/trainingMetrics)

Measurements of the classifier’s performance on the training data set.

[`let validationMetrics: MLClassifierMetrics`](/documentation/CreateML/MLTextClassifier/validationMetrics)

Measurements of the classifier’s performance on the validation data set.

### Testing a text classifier

[`func prediction(from: String) throws -> String`](/documentation/CreateML/MLTextClassifier/prediction(from:))

Classifies a string with a label.

[`func predictions(from:)`](/documentation/CreateML/MLTextClassifier/predictions(from:))

Classifies an array of strings with labels.

[`func predictionWithConfidence(from: String) throws -> [String : Double]`](/documentation/CreateML/MLTextClassifier/predictionWithConfidence(from:))

Predicts multiple possible labels and their confidence scores for the specified string.

[`func predictionsWithConfidence(from:)`](/documentation/CreateML/MLTextClassifier/predictionsWithConfidence(from:))

Predicts multiple possible labels and their confidence scores for each string in the specified array.

### Saving a text classifier

[`func write(to: URL, metadata: MLModelMetadata?) throws`](/documentation/CreateML/MLTextClassifier/write(to:metadata:))

Exports the text classifier as a Core ML model file at the specified URL.

[`func write(toFile: String, metadata: MLModelMetadata?) throws`](/documentation/CreateML/MLTextClassifier/write(toFile:metadata:))

Exports the text classifier as a Core ML model file at the specified file path.

### Describing a text classifier

[`var model: MLModel`](/documentation/CreateML/MLTextClassifier/model)

The underlying Core ML model of the text classifier.

[`var description: String`](/documentation/CreateML/MLTextClassifier/description)

A text representation of the text classifier.

[`var debugDescription: String`](/documentation/CreateML/MLTextClassifier/debugDescription)

A text representation of the text classifier that’s suitable for output
during debugging.

[`var playgroundDescription: Any`](/documentation/CreateML/MLTextClassifier/playgroundDescription)

A description of the text classifier in a playground.

### Supporting types

[`enum FeatureExtractorType`](/documentation/CreateML/MLTextClassifier/FeatureExtractorType)

The text feature extractor type.

[`enum ModelAlgorithmType`](/documentation/CreateML/MLTextClassifier/ModelAlgorithmType)

The type of algorithm that a text classifier uses.

### Default Implementations

[CustomDebugStringConvertible Implementations](/documentation/CreateML/MLTextClassifier/CustomDebugStringConvertible-Implementations)

[CustomPlaygroundDisplayConvertible Implementations](/documentation/CreateML/MLTextClassifier/CustomPlaygroundDisplayConvertible-Implementations)

[CustomStringConvertible Implementations](/documentation/CreateML/MLTextClassifier/CustomStringConvertible-Implementations)

## Relationships

### Conforms To

[`Escapable`](/documentation/Swift/Escapable)

[`CustomPlaygroundDisplayConvertible`](/documentation/Swift/CustomPlaygroundDisplayConvertible)

[`Sendable`](/documentation/Swift/Sendable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Copyable`](/documentation/Swift/Copyable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)