<!--
{
  "availability" : [
    "iOS: 12.0.0 -",
    "iPadOS: 12.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.14.0 -",
    "tvOS: 12.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 5.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "NaturalLanguage",
  "identifier" : "/documentation/NaturalLanguage/NLModel",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Natural Language"
    ],
    "preciseIdentifier" : "c:objc(cs)NLModel"
  },
  "title" : "NLModel"
}
-->

# NLModel

A custom model trained to classify or tag natural language text.

```
class NLModel
```

## Overview

With [`Natural Language`](/documentation/NaturalLanguage), you can create text classifier (<doc://com.apple.documentation/documentation/CreateML/MLTextClassifier>) or word tagger (<doc://com.apple.documentation/documentation/CreateML/MLWordTagger>) models. Use [`NLModel`](/documentation/NaturalLanguage/NLModel) to integrate those models into your app. This integration ensures that your tokenization and tagger configurations are identical when you train your model and use it in your app.

If you create a text classifier as described in <doc:creating-a-text-classifier-model>, you can integrate that model into your app and use it to make predictions like this:

```swift
let text = "I am very happy."

do {
    let mlModel = try SentimentClassifier(configuration: MLModelConfiguration()).model
        
    let customModel = try NLModel(mlModel: mlModel)
    
    // Use the text classifier model to get the most likely label.
    if let label = customModel.predictedLabel(for: text) {
        print("Most likely label: \(label)")
    }
    
    // Get multiple possible labels with their associated confidence scores.
    let labelHypotheses = customModel.predictedLabelHypotheses(for: text, maximumCount: 3)
    print("Label confidence scores: \(labelHypotheses)")
    
} catch {
    print(error)
}
```

If you create a custom word tagger as described in <doc:creating-a-word-tagger-model>, you can integrate that model into your app and generate tags for new text input like this:

```swift
let text = "The iPad is my favorite Apple product."

do {
    let mlModel = try AppleTagger(configuration: MLModelConfiguration()).model
        
    let customModel = try NLModel(mlModel: mlModel)
    let customTagScheme = NLTagScheme("Apple")
    
    let tagger = NLTagger(tagSchemes: [.nameType, customTagScheme])
    tagger.string = text
    tagger.setModels([customModel], forTagScheme: customTagScheme)
    
    tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, 
                         scheme: customTagScheme, options: .omitWhitespace) { tag, tokenRange  in
        if let tag = tag {
            print("\(text[tokenRange]): \(tag.rawValue)")
        }
        return true
    }
} catch {
    print(error)
}
```

## Topics

### Creating a model

[`+  modelWithMLModel:error:`](/documentation/NaturalLanguage/NLModel/init(mlModel:)-9tpjr)

Creates a new natural language model based on the given Core ML model instance.

[`+  modelWithContentsOfURL:error:`](/documentation/NaturalLanguage/NLModel/init(contentsOf:))

Creates a new natural language model based on a compiled Core ML model at the given URL.

### Making predictions

[`-  predictedLabelForString:`](/documentation/NaturalLanguage/NLModel/predictedLabel(for:))

Predicts a label for the given input string.

[`-  predictedLabelsForTokens:`](/documentation/NaturalLanguage/NLModel/predictedLabels(forTokens:))

Predicts a label for each string in the given array.

[`predictedLabelHypotheses(for:maximumCount:)`](/documentation/NaturalLanguage/NLModel/predictedLabelHypotheses(for:maximumCount:))

Predicts multiple possible labels for the given input string.

[`-  predictedLabelHypothesesForString:maximumCount:`](/documentation/NaturalLanguage/NLModel/predictedLabelHypothesesForString:maximumCount:)

Predicts multiple possible labels for the given input string.

[`predictedLabelHypotheses(forTokens:maximumCount:)`](/documentation/NaturalLanguage/NLModel/predictedLabelHypotheses(forTokens:maximumCount:))

Predicts multiple possible labels for each string in the given array.

[`-  predictedLabelHypothesesForTokens:maximumCount:`](/documentation/NaturalLanguage/NLModel/predictedLabelHypothesesForTokens:maximumCount:)

Predicts multiple possible labels for each string in the given array.

### Inspecting a model

[`configuration`](/documentation/NaturalLanguage/NLModel/configuration)

A configuration describing the natural language model.

[`NLModelConfiguration`](/documentation/NaturalLanguage/NLModelConfiguration)

The configuration parameters of a natural language model.

### Related Documentation

  <doc://com.apple.documentation/documentation/CreateML/MLTextClassifier>

  <doc://com.apple.documentation/documentation/CreateML/MLWordTagger>

## Relationships

### Conforms To

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

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

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

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

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

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

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)