Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 11 of 12
Module 4 Practice
Apply what you learned about embeddings and semantic similarity with a hands-on practice project.
Mini Project: Simple Semantic Classifier
Mini Project: Simple Semantic Classifier
# Mini Project: Use embedding-like similarity to classify text
# Demonstrates the core concept of embedding-based classification
import math
def cosine_similarity(v1, v2):
dot = sum(a * b for a, b in zip(v1, v2))
m1 = math.sqrt(sum(a**2 for a in v1))
m2 = math.sqrt(sum(b**2 for b in v2))
return dot / (m1 * m2) if m1 and m2 else 0
# Category "prototype" embeddings (in production: use a real embedding model)
# We simulate with manually defined vectors on two dimensions:
# Dimension 0: technical (positive) vs general (negative)
# Dimension 1: positive sentiment (positive) vs negative (negative)
category_prototypes = {
"Technical Support": [0.9, 0.1],
"Billing Question": [0.3, 0.1],
"Complaint": [-0.2, -0.9],
"Compliment": [-0.2, 0.9],
"General Inquiry": [-0.5, 0.0],
}
def classify_text(text):
"""
Classify text by finding the most similar category prototype.
In production: use a real embedding model to get the text embedding.
"""
# Simulate embedding (in reality: call embedding API with the text)
# Simple heuristic to create a fake embedding for demo purposes
tech_words = ["error", "bug", "crash", "broken", "fix", "install", "password", "reset"]
positive_words = ["great", "excellent", "love", "perfect", "helpful", "amazing"]
negative_words = ["terrible", "awful", "horrible", "worst", "useless", "broken"]
text_lower = text.lower()
dim0 = sum(1 for w in tech_words if w in text_lower) * 0.4 - 0.2
dim1 = sum(1 for w in positive_words if w in text_lower) * 0.5
dim1 -= sum(1 for w in negative_words if w in text_lower) * 0.5
text_embedding = [max(-1, min(1, dim0)), max(-1, min(1, dim1))]
# Find most similar category
best_category = None
best_similarity = -1
for category, prototype in category_prototypes.items():
sim = cosine_similarity(text_embedding, prototype)
if sim > best_similarity:
best_similarity = sim
best_category = category
return best_category, best_similarity, text_embedding
# Test examples
test_messages = [
"My account password is not working and I can't log in",
"Your service is absolutely amazing, thank you so much!",
"This is the worst product I have ever used, completely useless",
"How much does the premium plan cost?",
"What are your office hours?",
]
print("Semantic Text Classification Demo")
print("=" * 50)
print()
for message in test_messages:
category, sim, embedding = classify_text(message)
print(f"Message: '{message[:50]}...' " if len(message) > 50 else f"Message: '{message}'")
print(f" Classified as: {category} (similarity: {sim:.2f})")
print(f" Text embedding: {[round(v, 2) for v in embedding]}")
print()Diagram
Loading diagram…
Practice Diagram
Key Takeaways from Module 4
- An embedding is a list of numbers (vector) that represents text in a way that captures its meaning
- Text with similar meaning gets similar embedding vectors — their numbers are close in the high-dimensional space
- Cosine similarity measures how similar two embedding vectors are (1.0 = identical meaning, 0.0 = unrelated)
- Sentence embeddings represent entire sentences as vectors, capturing meaning in context
- Semantic search uses embeddings to find documents that mean the same thing, not just share keywords
- Embeddings power RAG systems, recommendation engines, classification, duplicate detection, and more
- You do not need to train embeddings — APIs like OpenAI embeddings provide high-quality vectors from text
Key Takeaways
- Apply what you learned about embeddings and semantic similarity with a hands-on practice project.
- An embedding is a list of numbers (vector) that represents text in a way that captures its meaning
- Text with similar meaning gets similar embedding vectors — their numbers are close in the high-dimensional space
- Cosine similarity measures how similar two embedding vectors are (1.0 = identical meaning, 0.0 = unrelated)
Finished this lesson?
0 of 12 in Module 4