-
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.”Recursos
Vídeos relacionados
WWDC23
WWDC19
-
Buscar neste vídeo...
-
-
3:53 - Confidence Scores: tagHypotheses
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 } -
12:19 - Word Embedding
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 } } -
16:32 - Sentence Embedding
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) } -
18:36 - Finding nearest neighbor with sentence embedding
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 } -
22:19 - Sentence embedding compressed as custom embedding
import NaturalLanguage import CreateML let embedding = try MLWordEmbedding(dictionary: sentenceVectors) try embedding.write(to: URL(fileURLWithPath: "/tmp/Verse.mlmodel")) -
22:47 - Finding nearest neighbor with sentence embedding and custom embedding
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) } -
33:29 - WordTagger
import CreateML let modelParameters = MLWordTagger.ModelParameters(algorithm: .crf(revision: 1)) -
36:46 - Using custom word tagger
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 } }
-