Kick off your book project in 3 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Saturday, May 16, 2026. Learn more…

Leanpub Header

Skip to main content

Artificial Intelligence Using Swift

CoreML, NLP, Deep Learning, Semantic Web and Linked Data, Knowledge Graphs, Knowledge Representation

This book is 100% completeLast updated on 2026-05-02

Dive into NLP, deep learning, knowledge representation, and semantic web technologies.

All of my Leanpub books, including this book, can be read for FREE on my web site: https://markwatson.com/

Minimum price

$7.99

$10.00

You pay

Author earns

$
PDF
EPUB
WEB
About

About

About the Book

This book focuses on the author's professional interests: NLP, deep learning, knowledge representation, knowledge graphs, and semantic web technologies. The reader is assumed to have some knowledge of Swift but the book examples describe the parts of the Swift language that are used. This is not an introduction to Swift programming nor is it a broad general book on AI.

In order to make this book as broadly useful as possible for developers wanting to take a deep dive into NLP, deep learning, knowledge representation, and semantic web technologies all examples will be plain text console (command line) applications. Much of the code is useable on macOS (and iOS and iPadOS) and Linux except for the CoreML examples and Apple's NLP libraries that are Apple platforms only.

All of my Leanpub books, including this book, can be read for FREE on my web site: https://markwatson.com/

Part 1: Introduction and Short Examples

We start this book with a sufficient introduction for Swift to understand the programming examples. After introducing the language we will look at a few short examples:

  • - Creating Swift Projects
  • - Writing command line utilities.
  • - Web scraping.

Part 2: Apple's CoreML and NLP Libraries

  • - Introduction of CoreML
  • - Examples using CoreML
  • - Introduction of NLP
  • - Examples using NLP libraries

Part 3: Knowledge Representation and Data Acquisition

  • - Introduction to the semantic web and linked data.
  • - A general discussion of Knowledge Representation with linked data
  • - Knowledge Graph Explorer application

Part 4: SwiftUI Example Applications

  • Knowledge Graph Explorer (uses SPARQL, deep learning models for NLP and question answering)

Share this book

Author

About the Author

Mark Watson

Mark Watson is a consultant specializing in LLMs, deep learning, machine learning, knowledge graphs, and general artificial intelligence software development. He uses Common Lisp, Clojure, Python, Java, Haskell, and Ruby for development.

He is the author of 20+ published books on Artificial Intelligence, Deep Learning, Java, Ruby, Machine Learning, Common LISP, Clojure, JavaScript, Semantic Web, NLP, C++, Linux, and Scheme. He has 55 US Patents.

Mark's consulting customer list includes: Google, Capital One, Olive AI, CompassLabs, Disney, Sitescout.com, Embed.ly, and Webmind Corporation.

Mark wrote ten traditional published books for McGraw Hill, Springer Verlag, J Wiley, and Morgan Kaufman publishers before adopting the LeanPub self-publishing platform.

Leanpub Podcast

Episode 253

An Interview with Mark Watson

Contents

Table of Contents

Cover Material, Copyright, and License

Preface

  1. Requests from the Author
  2. Parts of this Book are Specific for macOS and iOS, with Some Support for Linux
  3. Code for this Book
  4. Author’s Background
  5. Cover Art
  6. Swift 3rd Party Libraries
  7. Acknowledgements

Part 1: Getting Started with Swift for AI Development

Setting Up Swift for Command Line Development

  1. Installing Swift Packages
  2. Creating Swift Packages
  3. Accessing Libraries that You Write in Other Projects

Background Information for Writing Swift Command Line Utilities

  1. Using Shell Processes
  2. FileIO Examples
  3. Mono-Repo Umbrella Package
  4. Swift REPL

Web Scraping

  1. Running in the Swift REPL

Part 2: Large Language Models

Using the OpenAI LLM APIs

  1. Core Architecture
  2. Key Features
  3. Technical Implementation Details
  4. Running Tests

Using Ollama to Run Local LLMs

  1. Running the Ollama Service
  2. The OllamaService Actor Library
  3. Tool Definitions
  4. Example Tests
  5. Ollama Wrap Up

Running Local LLMs with Apple’s MLX Framework

  1. Background: MLX and Apple Silicon
  2. Choosing a Model
  3. Project Structure
  4. Package.swift
  5. main.swift — Full Walkthrough
  6. Running the Example
  7. Swapping Models
  8. Key Takeaways
  9. Summary

Using the AnyLanguageModel Package with OpenAI, Gemini, and Ollama

  1. OpenAI Example
  2. Google Gemini Example with Tool Use
  3. Ollama Local Example
  4. AnyLanguageModel Package Wrap Up

Part 3: Deep Learning, Natural Language Processing, and Retrieval-Augmented Generation

Deep Learning Introduction

  1. Simple Multi-layer Perceptron Neural Networks
  2. Deep Learning

Natural Language Processing Using Apple’s Natural Language Framework

  1. Using Apple’s NaturalLanguage Swift Library
  2. NLP Utility Library
  3. Running the Example
  4. Chapter Wrap Up

Document Question Answering Using Gemini APIs and a Local Embeddings Vector Database

  1. Gemini API Client
  2. Text Chunking
  3. In-Memory Vector Store
  4. Running the Example
  5. Chapter Wrap Up

Part 4: The Semantic Web, Knowledge Graphs, and Linked Data

Linked Data and the Semantic Web

  1. Understanding the Resource Description Framework (RDF)
  2. Frequently Used Resource Namespaces
  3. Understanding the SPARQL Query Language
  4. LLMs and the Semantic Web
  5. Semantic Web and Linked Data Wrap Up

Querying Knowledge Graphs with SPARQL and Swift

  1. What We Are Building
  2. Project Structure
  3. The Library: Full Source Listing
  4. Running SPARQL Queries
  5. The Test Suite
  6. Tips for Writing SPARQL Queries
  7. Wrap Up

Part 5: Apple Intelligence — On-Device LLMs with FoundationModels

Using Apple Intelligence’s Default System Model To Build a Chat Command Line Tool

Using Apple Intelligence to Build a Coding Assistant

  1. Requirements
  2. Project Layout
  3. Package.swift
  4. CodingCLI.swift — Full Listing
  5. Walking Through the Code
  6. Running the Tool
  7. Ideas for Extension
  8. Wrap Up

Part 6: Applied AI Projects

Knowledge Base Navigator: Building an AI-Powered Information System

  1. Project Overview
  2. Project Structure
  3. Core Implementation
  4. Running the Application
  5. Key Takeaways
  6. Environment Setup

Anomaly Detection

  1. Motivation
  2. The Gaussian Model
  3. Swift Implementation Overview
  4. AnomalyDetection Class
  5. Preprocessing the Wisconsin Data
  6. Feature Histograms
  7. Running the Example
  8. Interpreting the Metrics
  9. Summary

AutoContext: Prepare Effective Prompts with Context for LLM Queries

  1. Project Structure
  2. Implementing Vectorization of Text and Semantic Similarity
  3. Core AutoContext Implementation
  4. The Interactive CLI (main.swift)
  5. Example Session
  6. Key Takeaways
  7. Wrap Up

Book Wrap Up

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub