Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2020 AI & Machine Learning

WWDC20 · 41 min · AI & Machine Learning

Make apps smarter with Natural Language

Explore how you can leverage the Natural Language framework to better analyze and understand text. Learn how to draw meaning from text using the framework’s built-in word and sentence embeddings, and how to create your own custom embeddings for specific needs. We’ll show you how to use samples to train a custom text classifier or word tagger to extract important pieces of information out of text— all powered by the transfer learning algorithms in Natural Language. Find out how you can create apps that can answer user questions, recognize similarities in text, and find relevant documents, images, and more. To get the most out of this session, you should have a basic understanding of the Natural Language framework. For an overview, watch “Introducing Natural Language Framework” and “Advances in Natural Language Framework.” You can also brush up on model training using Create ML through “Introducing the Create ML App.”

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 8 snippets

Confidence Scores: tagHypotheses swift · at 3:53 ↗
import NaturalLanguage 

let tagger = NLTagger(tagSchemes: [.nameType])
let str = "Tim Cook is very popular in Spain."
let strRange = Range(uncheckedBounds: (str.startIndex, str.endIndex))
tagger.string = str
tagger.setLanguage(.english, range: strRange)
 
tagger.enumerateTags(in: str.startIndex..<str.endIndex, unit: .word, scheme: .nameType, options: .omitWhitespace) { (tag, tokenRange) -> Bool in
    let (hypotheses, _) = tagger.tagHypotheses(at: tokenRange.lowerBound, unit: .word, 
                                               scheme: .nameType, maximumCount: 1)
    print(hypotheses)
    return true
}
Word Embedding swift · at 12:19 ↗
import NaturalLanguage

if let embedding = NLEmbedding.wordEmbedding(for: .english) {
    let word = "bicycle"
    
    if let vector = embedding.vector(for: word) {
        print(vector)
    }
    
    let dist = embedding.distance(between:word, and: "motorcycle")
    print(dist)
    
    embedding.enumerateNeighbors(for: word, maximumCount: 5) { neighbor, distance in
        print("\(neighbor): \(distance.description)")
        return true
    }
}
Sentence Embedding swift · at 16:32 ↗
import NaturalLanguage

if let embedding = NLEmbedding.sentenceEmbedding(for: .english) {
    let sentence = "This is a sentence."
    
    if let vector = sentenceEmbedding.vector(for: sentence) {
        print(vector)
    }
    
    let dist = sentenceEmbedding.distance(between: sentence, and: "That is a sentence.")
    print(dist)
}
Finding nearest neighbor with sentence embedding swift · at 18:36 ↗
func answerKey(for string: String) -> String? {
        guard let embedding = NLEmbedding.sentenceEmbedding(for: .english) else { return nil }
        guard let queryVector = embedding.vector(for: string) else { return nil }

        var answerKey: String? = nil
        var answerDistance = 2.0

        for (key, vectors) in self.faqEmbeddings {
            for (vector) in vectors {
                let distance = self.cosineDistance(vector, queryVector)
                if (distance < answerDistance) {
                    answerDistance = distance
                    answerKey = key
                }
            }
        }

        return answerKey
    }
Sentence embedding compressed as custom embedding swift · at 22:19 ↗
import NaturalLanguage
import CreateML


let embedding = try MLWordEmbedding(dictionary: sentenceVectors)

try embedding.write(to: URL(fileURLWithPath: "/tmp/Verse.mlmodel"))
Finding nearest neighbor with sentence embedding and custom embedding swift · at 22:47 ↗
func answerKeyCustom(for string: String) -> String? {
        guard let embedding = NLEmbedding.sentenceEmbedding(for: .english) else { return nil }
        guard let queryVector = embedding.vector(for: string) else { return nil }

        guard let (nearestLineKey, _) = self.customEmbedding.neighbors(for: queryVector, maximumCount: 1).first else { return nil }

        return self.poemKeyFromLineKey(nearestLineKey)
    }
WordTagger swift · at 33:29 ↗
import CreateML

let modelParameters = MLWordTagger.ModelParameters(algorithm: .crf(revision: 1))
Using custom word tagger swift · at 36:46 ↗
func findTags(for string: String) {
        let model = try! NLModel(contentsOf: Bundle.main.url(forResource: "Nosh", withExtension: "mlmodelc")!)
        let tagger = NLTagger(tagSchemes: [NoshTags])

        tagger.setModels([model], forTagScheme: NoshTags)
        tagger.string = string

        tagger.enumerateTags(in: string.startIndex..<string.endIndex, unit: .word, scheme: NoshTags, options: .omitWhitespace) { (tag, tokenRange) -> Bool in

            let name = String(string[tokenRange])

            switch tag {
                case NoshTagRestaurant:
                    self.noteRestaurant(name)
                case NoshTagFood:
                    self.noteFood(name)
                case NoshTagFromCity:
                    self.noteFromCity(name)
                case NoshTagToCity:
                    self.noteToCity(name)
                default:
                    break
            }
            return true
        }
    }