Kick off your book project in 2 hours, get started with GhostAI in 2 hours, or do both! Free live workshops, on Zoom. You’ll leave with a real book project and a clear plan to keep going. Saturday, June 27, 2026.

Leanpub Header

Skip to main content

Artificial Intelligence Fundamentals - Natural Language Processing

Sentence Similarity

Have you ever wondered how AI knows that "Which company designs iPhone?" is best answered by "Apple designs iPhone" — and not "I love to eat apple" or "Both Apple and Orange are fruits"? This book takes you on a visual, step-by-step journey to discover how artificial intelligence reads the meaning behind words — not just the words themselves. By the end, you'll understand the elegant idea powering modern AI chatbots, and write your own Python programs to find the best answer to any question from a list of sentences.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

How long does it take for you to learn English? How could you know that Apple can mean a fruit and not the company which designs iPhone. Imagine how you could teach a young child that Apple can either mean a fruit or a brand. How about teaching a computer? That's exactly what this book is about.

You'll discover how AI figures out that two completely different sentences, "I love to eat Apple" and "Apple designs iPhone" can lead to different meanings for the same word, "Apple". We start with something as simple as counting words, then gently build up to the same powerful technique used inside today's advanced AI systems — with clear, friendly explanations at every step.

Along the way, you'll see why understanding sentence similarity is at the heart of everything from AI Search to AI Chatbots and AI Agents. By the last page, you'll see the world of AI differently — and realise that understanding AI begins with something as simple as counting words.

Author

About the Author

Howard Pong Yuen LAM, PhD

Howard Pong Yuen Lam, PhD has spent over three decades turning bold ideas into measurable results across the boardroom and the classroom. A scholar, educator, and marketing strategist, he combines rigorous thinking, creative innovation, and hands-on leadership to lasting effect.

He holds a BSc in Computer Science, an MBA, and a PhD in Marketing from The Chinese University of Hong Kong (CUHK), an MSc in Financial Management from the University of London, and the Amazon Web Services (AWS) Certified AI Practitioner credential. He has passed all required examinations of the Association of Chartered Certified Accountants (ACCA) and is an alumnus of Harvard Business School, having completed The General Manager Program (TGMP) executive education. He also completed three Stanford University AI courses (2024–2025): Natural Language Processing, Machine Learning, and Business Opportunities and Applications of Generative AI.

He served as EMBA Programme Director and Associate Professor of Practice in Marketing at CUHK (2020–2023), taught part-time at CityU (2025–26), and developed and taught AI for Business (CUHK, 2024) and AI for Marketing (CityU, 2026). His research appears in leading journals including the Journal of Marketing, and his cases through Harvard Business Impact and Ivey Publishing — one recognised among Ivey's top cases of 2023, another winning the 2023 EFMD Case Writing Competition. He is the author of the following books:
Marketing Research with R and Python (World Scientific, 2023) https://a.co/d/05vm1F4F and
co-author of Master Communication Through Voice, Face and Body (2026). https://a.co/d/0aFlnopB

Before academia, Howard held senior roles at Procter & Gamble, McDonald's, and Coca-Cola. At Coca-Cola China, he created Minute Maid juice drink with pulp — the first such product from a developing country to achieve global success — earning Coca-Cola's Best New Product Award worldwide. A patent holder of the iconic Minute Maid bottle shape, he is known for cross-functional leadership and moving seamlessly from strategy to execution.

A passionate AI educator, Howard shares free videos on Xiaohongshu, Douyin, and Tencent Video, reaching over 100,000 total views as of May 2026.

Research articles, cases, books are available at https://lampongyuen.github.io/

Contents

Table of Contents

Cover image

Preface

About the author

Chapter 1 - Introduction

Chapter 2 - Sentence Similarity

  1. Examples
  2. Dot Product
  3. Pythagorean Theorem
  4. Dot Product and Cosine Similarity

Chapter 3 - Python Programs for Sentence Similarity

  1. Program 1 Cosine similarity
  2. Program 2 Cosine similarity function
  3. Program 3 Cosine similarity function with numpy distance
  4. Program 4 Use all-MiniLM-L6-v2 model for sentences with “Apple”
  5. Program 5 Use all-MiniLM-L6-v2 model for sentences about “age”
  6. Program 6 Use all-MiniLM-L6-v2 model for sentences about “where are you”
  7. Program 7 Use BAAI/bge-m3 model for sentences about “age”
  8. Program 8 Use BAAI/bge-m3 model for sentences about “age” in Chinese
  9. Program 9 Use BAAI/bge-m3 model for a sequence of sentences
  10. Program 10 Use BAAI/bge-m3 model for Q&A from a sequence of sentences
  11. Program 11 Use BAAI/bge-m3 model for Q&A from sentences.txt
  12. Program 12 Use BAAI/bge-m3 model for Q&A from sentences.txt with Gradio interface

Summary - Key Takeaways

  1. Word / sentence similarity
  2. AI understood language like humans
  3. Meaning of a word depended on the words around it
  4. Breakthrough idea for natural language processing (NLP)
  5. NLP was fundamental to AI development
  6. Chatbots and agentic AI

Appendix 1 - Visual Studio Code

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.

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