Structure

MLGazetteer

A collection of terms and their labels, which augments a tagger that analyzes natural language text.

Declaration

struct MLGazetteer

Overview

Use an MLGazetteer to configure a gazetteer and save it to a file, which you then add to your app in Xcode. Your app uses the gazetteer file at runtime to create an instance of NLGazetteer, which augments an NLTagger to tag specific terms with a label.

You configure a gazetteer with a dictionary, keyed by labels. Each value in the dictionary is an array of terms (words or phrases) for each label. For example, you can store the names of real and fictional planets in a gazetteer.

let planets = [
    "real planet": ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"],
    "fictional planet" : ["Arrakis", "Hoth", "Vulcan", "Pandora", "Tatooine", "Bajor", "Alderaan", "Romulus"]
]

let parameters = MLGazetteer.ModelParameters(language: .english)
let planetGazetteer = try! MLGazetteer(dictionary: planets, parameters: parameters)

Once you’ve configured an MLGazetteer, save it to an .mlmodel file to include in your app.

try planetGazetteer.write(toFile: "~/Desktop/PlanetGazetteer.mlmodel")

A gazetteer file can efficiently store many labels, and many terms for each label.

Topics

Creating a Gazetteer

init(dictionary: [String : [String]], parameters: MLGazetteer.ModelParameters)

Creates a gazetteer from a set of labels for terms represented by a dictionary.

init(labeledData: MLDataTable, textColumn: String, labelColumn: String, parameters: MLGazetteer.ModelParameters)

Creates a gazetteer from a set of labels for terms represented by a data table.

struct MLGazetteer.ModelParameters

The parameters that configure a gazetteer.

let modelParameters: MLGazetteer.ModelParameters

The configuration parameters that the gazetteer used during initialization.

Testing a Gazetteer

func prediction(from: String) -> String

Retrieves the label for the given term.

func predictions(from: [String]) -> [String]

Retrieves the labels for the given terms.

func predictions(from: MLDataColumn<String>) -> MLDataColumn<String>

Retrieves the labels for the terms represented in a data table.

Saving a Gazetteer

func write(to: URL, metadata: MLModelMetadata?)

Exports the gazetteer contained within a Core ML model file at the given URL.

func write(toFile: String, metadata: MLModelMetadata?)

Exports the gazetteer contained within a Core ML model file at the given file path.

Describing a Gazetteer

var model: MLModel

The gazetteer contained within a Core ML model file stored in memory.

var description: String

A text representation of the gazetteer.

var debugDescription: String

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

var playgroundDescription: Any

A description of the gazetteer shown in a playground.

See Also

Natural Language Processing

Creating a Text Classifier Model

Train a machine learning model to classify natural language text.

struct MLTextClassifier

A model you train to classify natural language text.

struct MLWordTagger

A word tagging model you train to classify natural language text at the word level.

struct MLWordEmbedding

Creates and saves a map of strings to vectors, enabling your app to find neighboring, similar strings.

Beta

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software