{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.5"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Recitation 7: NLP & Image Analysis\n",
    "### Data Science for Entrepreneurship-Economy | Spring 2026\n",
    "\n",
    "**Today's goal**: Turn unstructured data (text and images) into numbers — then use your existing toolkit on them.\n",
    "\n",
    "**Part 1 — NLP (~45 min)**: Preprocess text → Bag-of-Words → text similarity → classification\n",
    "**Part 2 — Images (~45 min)**: Pixels as arrays → convolution filters → edge detection, blur, sharpen\n",
    "\n",
    "By the end you'll have coded every concept from Lecture 7 that was previously just diagrams.\n",
    "\n",
    "**What's new**: `CountVectorizer`, NLTK, `scipy.signal.convolve2d`. New libraries, familiar logic."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# NLP imports\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "from sklearn.metrics.pairwise import cosine_similarity\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay\n",
    "import nltk\n",
    "nltk.download('stopwords', quiet=True)\n",
    "nltk.download('punkt', quiet=True)\n",
    "nltk.download('punkt_tab', quiet=True)\n",
    "nltk.download('wordnet', quiet=True)\n",
    "from nltk.corpus import stopwords\n",
    "from nltk.tokenize import word_tokenize\n",
    "from nltk.stem import WordNetLemmatizer\n",
    "\n",
    "# Image imports\n",
    "from scipy.signal import convolve2d\n",
    "\n",
    "sns.set_style(\"whitegrid\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "df = pd.read_csv(\"cheffi_meals.csv\")\n",
    "print(f\"Loaded {len(df)} meal listings across {df['cuisine'].nunique()} cuisines\\n\")\n",
    "print(df[\"cuisine\"].value_counts())"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **Cheffi** (שפי) is an Israeli home-chef marketplace where amateur and semi-professional cooks list homemade meals for neighborhood delivery. Each listing has a **text description** and a **food photo**.\n",
    ">\n",
    "> Lior, the Head of Product, has two problems:\n",
    "> 1. **Text**: Auto-tag listings by cuisine (Israeli, Asian, Italian, Middle Eastern, Vegan) from descriptions alone\n",
    "> 2. **Image**: Understand image processing so the team can score and enhance photo quality\n",
    ">\n",
    "> Lior's first goal: auto-tag every listing by cuisine using only the text description."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Show 5 sample descriptions, one per cuisine\n",
    "for cuisine in df[\"cuisine\"].unique():\n",
    "    sample = df[df[\"cuisine\"] == cuisine].iloc[0]\n",
    "    print(f\"[{cuisine:15s}] {sample['description'][:80]}...\")\n",
    "    print()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 📝 Section 2: Text Preprocessing\n",
    "*Raw text → clean tokens. The NLP assembly line you saw in Lecture 7 — now in Python.*\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 📖 Text Preprocessing Pipeline\n",
    "\n",
    "In Lecture 7 you traced through the pipeline by hand: lowercase → remove punctuation → tokenize → remove stopwords → lemmatize. Now you code each step. The good news: scikit-learn's `CountVectorizer` can do most of this automatically — but first, let's do it manually so you understand what's happening under the hood."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "sample_text = df.iloc[0][\"description\"]\n",
    "print(f\"Original: {sample_text}\\n\")\n",
    "\n",
    "# Step 1: Lowercase\n",
    "text = sample_text.lower()\n",
    "print(f\"Lowercase: {text}\\n\")\n",
    "\n",
    "# Step 2: Tokenize\n",
    "tokens = word_tokenize(text)\n",
    "print(f\"Tokens ({len(tokens)}): {tokens}\\n\")\n",
    "\n",
    "# Step 3: Remove stopwords and non-alphabetic tokens\n",
    "stop_words = set(stopwords.words(\"english\"))\n",
    "tokens_clean = [t for t in tokens if t.isalpha() and t not in stop_words]\n",
    "print(f\"After stopwords ({len(tokens_clean)}): {tokens_clean}\\n\")\n",
    "\n",
    "# Step 4: Lemmatize\n",
    "lemmatizer = WordNetLemmatizer()\n",
    "tokens_lemma = [lemmatizer.lemmatize(t) for t in tokens_clean]\n",
    "print(f\"Lemmatized ({len(tokens_lemma)}): {tokens_lemma}\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "def preprocess(text):\n",
    "    \"\"\"Full preprocessing pipeline for one text string.\"\"\"\n",
    "    tokens = word_tokenize(text.lower())\n",
    "    tokens = [t for t in tokens if t.isalpha() and t not in stop_words]\n",
    "    return \" \".join([lemmatizer.lemmatize(t) for t in tokens])\n",
    "\n",
    "df[\"clean_text\"] = df[\"description\"].apply(preprocess)\n",
    "print(\"Before:\", df.iloc[0][\"description\"][:80])\n",
    "print(\"After: \", df.iloc[0][\"clean_text\"][:80])\n",
    "print(f\"\\nAvg words before: {df['description'].str.split().str.len().mean():.1f}\")\n",
    "print(f\"Avg words after:  {df['clean_text'].str.split().str.len().mean():.1f}\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 🎯 Exercise 1: Preprocess a New Description\n",
    "\n",
    "- Take this description: `\"The BEST homemade Pasta with Fresh Basil and Creamy Tomato Sauce! Absolutely delicious.\"`\n",
    "- Apply the `preprocess()` function to it\n",
    "- How many words before vs. after?\n",
    "- Which words were removed? Were any important words lost?"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "##### Your code goes here #####\n",
    "\n",
    "new_desc = \"The BEST homemade Pasta with Fresh Basil and Creamy Tomato Sauce! Absolutely delicious.\"\n",
    "\n",
    "# Apply preprocessing\n",
    "\n",
    "# Words before vs after?\n",
    "\n",
    "# Which words were removed? Any important ones lost? (comment)\n",
    "\n",
    "###### End of Your Code ######"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>✅ Solution</b></summary>\n",
    "\n",
    "```python\n",
    "new_desc = \"The BEST homemade Pasta with Fresh Basil and Creamy Tomato Sauce! Absolutely delicious.\"\n",
    "\n",
    "clean = preprocess(new_desc)\n",
    "print(f\"Before ({len(new_desc.split())} words): {new_desc}\")\n",
    "print(f\"After  ({len(clean.split())} words): {clean}\")\n",
    "\n",
    "# Removed: \"The\", \"with\", \"and\" (stopwords), \"!\" and \".\" (punctuation)\n",
    "# \"BEST\" → \"best\" (lowercased). Key content words preserved.\n",
    "# ~25% reduction — all the informative words remain.\n",
    "```\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 🔢 Section 3: Bag-of-Words\n",
    "*Turn clean text into a numeric matrix — the bridge from words to models.*\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# CountVectorizer does tokenization + counting in one step\n",
    "bow_vectorizer = CountVectorizer(max_features=200)\n",
    "X_bow = bow_vectorizer.fit_transform(df[\"clean_text\"])\n",
    "\n",
    "print(f\"Document-Term Matrix shape: {X_bow.shape}\")\n",
    "print(f\"  → {X_bow.shape[0]} documents × {X_bow.shape[1]} vocabulary words\")\n",
    "print(f\"  → Sparsity: {1 - X_bow.nnz / (X_bow.shape[0] * X_bow.shape[1]):.1%}\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "vocab = bow_vectorizer.get_feature_names_out()\n",
    "print(f\"Top 20 words in vocabulary:\\n{list(vocab[:20])}\")\n",
    "print(f\"\\nLast 20 words:\\n{list(vocab[-20:])}\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Convert first document to a dense array and show non-zero entries\n",
    "doc0 = pd.Series(X_bow[0].toarray()[0], index=vocab)\n",
    "nonzero = doc0[doc0 > 0].sort_values(ascending=False)\n",
    "print(f\"Listing 0 — non-zero word counts ({len(nonzero)} words):\\n\")\n",
    "print(nonzero)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 🎯 Exercise 2: Find Similar Meals\n",
    "\n",
    "- Pick listing 0 (or any listing you find interesting)\n",
    "- Compute cosine similarity between listing 0's BoW vector and ALL other listings\n",
    "- Print the **top 5 most similar** meals with their names, cuisines, and similarity scores\n",
    "- Are they the same cuisine? Does the similarity make sense?"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "##### Your code goes here #####\n",
    "\n",
    "# Compute cosine similarity between listing 0 and all others\n",
    "\n",
    "# Top 5 most similar (skip self)\n",
    "\n",
    "# Are they the same cuisine? (comment)\n",
    "\n",
    "###### End of Your Code ######"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>✅ Solution</b></summary>\n",
    "\n",
    "```python\n",
    "# Cosine similarity between listing 0 and all others\n",
    "sim_scores = cosine_similarity(X_bow[0], X_bow)[0]\n",
    "\n",
    "# Get top 6 (including self), then skip self (highest similarity = 1.0)\n",
    "top_idx = sim_scores.argsort()[::-1][1:6]\n",
    "\n",
    "print(f\"Listing 0: {df.iloc[0]['description'][:60]}... [{df.iloc[0]['cuisine']}]\\n\")\n",
    "print(\"Top 5 most similar:\")\n",
    "for i in top_idx:\n",
    "    print(f\"  Sim={sim_scores[i]:.3f} | [{df.iloc[i]['cuisine']:15s}] {df.iloc[i]['description'][:55]}...\")\n",
    "\n",
    "# Most similar listings likely share the same cuisine and key vocabulary.\n",
    "# Cross-cuisine matches (e.g., Israeli ↔ Middle Eastern) are possible due to shared words.\n",
    "```\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 💡 Insight\n",
    "\n",
    "Text similarity using BoW + cosine is exactly how simple search engines work. When Lior's users search for 'spicy noodles,' the platform can rank listings by cosine similarity to the query — no machine learning model needed, just vector math."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 🏷️ Section 4: Text Classification\n",
    "*BoW gives you features. Classification gives you labels. Connect the dots.*\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 📖 From Text to Classifier\n",
    "\n",
    "The document-term matrix IS a feature matrix — same structure as Recitations 3–4. Rows = documents, columns = word features, values = word counts. We can feed it directly into any classifier from Lecture 3. Today: Logistic Regression on BoW features to predict cuisine type."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "y = df[\"cuisine\"]\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X_bow, y, test_size=0.2, random_state=42, stratify=y\n",
    ")\n",
    "print(f\"Train: {X_train.shape[0]} | Test: {X_test.shape[0]}\")\n",
    "print(f\"Classes: {list(y.unique())}\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "clf = LogisticRegression(max_iter=1000, random_state=42)\n",
    "clf.fit(X_train, y_train)\n",
    "y_pred = clf.predict(X_test)\n",
    "\n",
    "print(classification_report(y_test, y_pred))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "cm = confusion_matrix(y_test, y_pred, labels=clf.classes_)\n",
    "ConfusionMatrixDisplay(cm, display_labels=clf.classes_).plot(cmap=\"Blues\", xticks_rotation=45)\n",
    "plt.title(\"Cuisine Classification — BoW + Logistic Regression\")\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 📖 Reading the Results\n",
    "\n",
    "Look at the confusion matrix: where does the model make mistakes? Israeli and Middle Eastern likely get confused (shared vocabulary like hummus, pita). That makes business sense — these cuisines genuinely overlap in ingredients and descriptions."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "feature_names = bow_vectorizer.get_feature_names_out()\n",
    "for i, cuisine in enumerate(clf.classes_):\n",
    "    top_idx = clf.coef_[i].argsort()[-8:][::-1]\n",
    "    top_words = [feature_names[j] for j in top_idx]\n",
    "    print(f\"{cuisine:18s}: {', '.join(top_words)}\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 📖 Most Important Words\n",
    "\n",
    "These are the words the model relies on most for each cuisine. The model learned vocabulary-based rules from word counts — exactly what you'd expect from Bag-of-Words features."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 🎯 Exercise 3: Classify a New Listing\n",
    "\n",
    "- Write a new meal description (1–2 sentences) for a cuisine of your choice — but DON'T make it too obvious\n",
    "- **Preprocess** it with `preprocess()`, then transform using `bow_vectorizer.transform([clean_text])`\n",
    "- Predict the cuisine using the trained classifier\n",
    "- Does the model get it right? If not, why might it fail?"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "##### Your code goes here #####\n",
    "\n",
    "new_listing = \"...\"  # Write your own description\n",
    "\n",
    "# Transform with BoW vectorizer\n",
    "\n",
    "# Predict cuisine\n",
    "\n",
    "# Did it get it right? Why or why not? (comment)\n",
    "\n",
    "###### End of Your Code ######"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>✅ Solution</b></summary>\n",
    "\n",
    "```python\n",
    "new_listing = \"Creamy lentil soup with a squeeze of lemon and warm pita on the side\"\n",
    "\n",
    "# Preprocess, then transform\n",
    "clean_new = preprocess(new_listing)\n",
    "X_new = bow_vectorizer.transform([clean_new])\n",
    "\n",
    "predicted = clf.predict(X_new)[0]\n",
    "proba = clf.predict_proba(X_new)[0]\n",
    "\n",
    "print(f\"Description: {new_listing}\")\n",
    "print(f\"Predicted cuisine: {predicted}\\n\")\n",
    "for cls, p in zip(clf.classes_, proba):\n",
    "    print(f\"  {cls:18s}: {p:.1%}\")\n",
    "\n",
    "# This description is tricky — lentil/pita overlap Israeli, Middle Eastern, and Vegan.\n",
    "# The model might misclassify because the vocabulary doesn't clearly signal one cuisine.\n",
    "```\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 💡 Insight\n",
    "\n",
    "From raw text to classified labels in 4 steps: preprocess → BoW → train → predict. The document-term matrix is the bridge. Every NLP classification task — spam detection, sentiment analysis, topic tagging — follows this exact pattern."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 🖼️ Part 2: Image Analysis\n",
    "*A photo is just a grid of numbers. Filters extract patterns from that grid.*\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 📖 Images as Arrays\n",
    "\n",
    "In Lecture 7 you learned that an image is a 2D array of pixel values (0=black, 255=white for grayscale). You traced convolutions by hand on an 8×8 grid. Now you'll create images as numpy arrays, visualize them, and apply real filters in code."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Create a 24x24 grayscale image: bright circle (plate) on dark background (table)\n",
    "img = np.full((24, 24), 30, dtype=float)   # dark table (value 30)\n",
    "\n",
    "# Draw a bright circular plate in the center\n",
    "center_row, center_col, radius = 12, 12, 8\n",
    "for row in range(24):\n",
    "    for col in range(24):\n",
    "        if (row - center_row)**2 + (col - center_col)**2 <= radius**2:\n",
    "            img[row, col] = 230   # bright plate\n",
    "\n",
    "print(f\"Image shape: {img.shape}\")\n",
    "print(f\"Min pixel: {img.min():.0f}, Max pixel: {img.max():.0f}\")\n",
    "\n",
    "plt.figure(figsize=(5, 5))\n",
    "plt.imshow(img, cmap=\"gray\", vmin=0, vmax=255)\n",
    "plt.title(\"Synthetic Food Photo: Plate on Table\")\n",
    "plt.colorbar(label=\"Pixel Value\")\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Show a 6x6 patch at the plate edge (where the interesting transition happens)\n",
    "patch = img[6:12, 6:12]\n",
    "print(\"6×6 patch at the plate edge:\\n\")\n",
    "print(patch.astype(int))\n",
    "\n",
    "plt.figure(figsize=(4, 4))\n",
    "plt.imshow(patch, cmap=\"gray\", vmin=0, vmax=255)\n",
    "for row in range(6):\n",
    "    for col in range(6):\n",
    "        plt.text(col, row, f\"{int(patch[row,col])}\", ha=\"center\", va=\"center\",\n",
    "                 color=\"red\" if patch[row,col] < 128 else \"blue\", fontsize=8)\n",
    "plt.title(\"Zoomed In: Each Cell Is a Pixel Value\")\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The transition from 30 (dark table) to 230 (bright plate) is the plate edge. This is what edge detection filters look for."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Add random noise to simulate a bad phone camera\n",
    "np.random.seed(42)\n",
    "noise = np.random.normal(0, 25, img.shape)\n",
    "img_noisy = np.clip(img + noise, 0, 255)\n",
    "\n",
    "# Apply heavy blur to simulate an out-of-focus photo\n",
    "blur_kernel = np.ones((5, 5)) / 25\n",
    "img_blurry = convolve2d(img, blur_kernel, mode=\"same\", boundary=\"fill\")\n",
    "\n",
    "fig, axes = plt.subplots(1, 3, figsize=(14, 4))\n",
    "axes[0].imshow(img, cmap=\"gray\", vmin=0, vmax=255)\n",
    "axes[0].set_title(\"Clean Photo\")\n",
    "axes[1].imshow(img_noisy, cmap=\"gray\", vmin=0, vmax=255)\n",
    "axes[1].set_title(\"Noisy Photo (bad camera)\")\n",
    "axes[2].imshow(img_blurry, cmap=\"gray\", vmin=0, vmax=255)\n",
    "axes[2].set_title(\"Blurry Photo (out of focus)\")\n",
    "plt.suptitle(\"Same Plate — Different Quality Issues\", fontsize=13)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 🎯 Exercise 4: Create Your Own Image\n",
    "\n",
    "- Create a 24×24 grayscale image with a **bright square** (value 220) on a dark background (value 40)\n",
    "- The square should be centered, roughly 10×10 pixels\n",
    "- Display it with `plt.imshow()`\n",
    "- Print the pixel values of a row that crosses the square edge"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "##### Your code goes here #####\n",
    "\n",
    "# Create 24x24 dark image\n",
    "\n",
    "# Draw a bright 10x10 square in the center\n",
    "\n",
    "# Display with imshow\n",
    "\n",
    "# Print one row crossing the edge\n",
    "\n",
    "###### End of Your Code ######"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>✅ Solution</b></summary>\n",
    "\n",
    "```python\n",
    "img2 = np.full((24, 24), 40, dtype=float)\n",
    "img2[7:17, 7:17] = 220   # bright 10x10 square in the center\n",
    "\n",
    "plt.figure(figsize=(5, 5))\n",
    "plt.imshow(img2, cmap=\"gray\", vmin=0, vmax=255)\n",
    "plt.title(\"Bright Square on Dark Background\")\n",
    "plt.colorbar(label=\"Pixel Value\")\n",
    "plt.show()\n",
    "\n",
    "# Print row 12 (crosses the square)\n",
    "print(f\"Row 12 pixel values:\\n{img2[12].astype(int)}\")\n",
    "# Shows: 40 40 ... 40 220 220 ... 220 40 ... 40 — sharp edge transition\n",
    "```\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 🔍 Section 5: Convolution Filters\n",
    "*Slide a small number grid across the image. Multiply, sum, repeat. Detect edges, blur, sharpen.*\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 📖 Convolution Recap\n",
    "\n",
    "In Lecture 7 you traced convolution by hand: place a 3×3 filter on the image, multiply each filter value by the pixel beneath it, sum the 9 products → one output number. Slide and repeat. The output is a 'feature map' that shows where the filter's pattern was detected. Now: code it."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# scipy's convolve2d does the slide-multiply-sum for us\n",
    "# mode='same' keeps output the same size as input\n",
    "# boundary='fill' pads edges with zeros\n",
    "\n",
    "def apply_filter(image, kernel, title=\"Filtered\"):\n",
    "    \"\"\"Apply a filter (e.g. 3x3) and display before/after.\"\"\"\n",
    "    filtered = convolve2d(image, kernel, mode=\"same\", boundary=\"fill\")\n",
    "\n",
    "    fig, axes = plt.subplots(1, 2, figsize=(10, 4))\n",
    "    axes[0].imshow(image, cmap=\"gray\", vmin=0, vmax=255)\n",
    "    axes[0].set_title(\"Original\")\n",
    "    axes[1].imshow(filtered, cmap=\"gray\")\n",
    "    axes[1].set_title(title)\n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "    return filtered"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Vertical edge detection filter (Sobel-like)\n",
    "edge_v = np.array([\n",
    "    [-1, 0, 1],\n",
    "    [-2, 0, 2],\n",
    "    [-1, 0, 1]\n",
    "])\n",
    "print(\"Vertical Edge Filter:\\n\", edge_v)\n",
    "\n",
    "edges_v = apply_filter(img, edge_v, \"Vertical Edges Detected\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The filter produces high values where brightness changes sharply from left to right — the plate's left and right edges light up."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Horizontal edge detection filter\n",
    "edge_h = np.array([\n",
    "    [-1, -2, -1],\n",
    "    [ 0,  0,  0],\n",
    "    [ 1,  2,  1]\n",
    "])\n",
    "edges_h = apply_filter(img, edge_h, \"Horizontal Edges Detected\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Combine vertical and horizontal edges\n",
    "edge_magnitude = np.sqrt(edges_v**2 + edges_h**2)\n",
    "\n",
    "plt.figure(figsize=(5, 5))\n",
    "plt.imshow(edge_magnitude, cmap=\"hot\")\n",
    "plt.title(\"Combined Edge Detection — Full Plate Rim Visible\")\n",
    "plt.colorbar(label=\"Edge Strength\")\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The plate's circular rim appears as a bright ring — the filter detected ALL the edges, regardless of direction."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Averaging (blur) filter — each pixel becomes the average of its 3x3 neighborhood\n",
    "blur = np.ones((3, 3)) / 9\n",
    "print(\"Blur Filter:\\n\", blur.round(3))\n",
    "\n",
    "blurred = apply_filter(img_noisy, blur, \"Blurred (Denoised)\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Applied to the noisy image, the blur filter smooths out the random noise while keeping the overall plate shape. This is a basic denoising technique."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Sharpen filter — amplifies differences from neighbors\n",
    "sharpen = np.array([\n",
    "    [ 0, -1,  0],\n",
    "    [-1,  5, -1],\n",
    "    [ 0, -1,  0]\n",
    "])\n",
    "print(\"Sharpen Filter:\\n\", sharpen)\n",
    "\n",
    "sharpened = apply_filter(img, sharpen, \"Sharpened (Enhanced Edges)\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 🎯 Exercise 5: Filter the Noisy Image\n",
    "\n",
    "- Apply the **edge detection** filter (vertical) to the **noisy** image (`img_noisy`)\n",
    "- Apply the **blur** filter to the noisy image FIRST, then apply edge detection to the blurred result\n",
    "- Display all three: (1) edges on noisy image, (2) blurred image, (3) edges on blurred image\n",
    "- Which gives cleaner edges? Why does blurring first help?"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "##### Your code goes here #####\n",
    "\n",
    "# 1. Edge detection on noisy image directly\n",
    "\n",
    "# 2. Blur the noisy image first\n",
    "\n",
    "# 3. Edge detection on the blurred version\n",
    "\n",
    "# Which gives cleaner edges? Why? (comment)\n",
    "\n",
    "###### End of Your Code ######"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>✅ Solution</b></summary>\n",
    "\n",
    "```python\n",
    "# 1. Edge detection directly on noisy image\n",
    "noisy_edges = convolve2d(img_noisy, edge_v, mode=\"same\", boundary=\"fill\")\n",
    "\n",
    "# 2. Blur first, then edge detection\n",
    "blurred_noisy = convolve2d(img_noisy, blur, mode=\"same\", boundary=\"fill\")\n",
    "clean_edges = convolve2d(blurred_noisy, edge_v, mode=\"same\", boundary=\"fill\")\n",
    "\n",
    "fig, axes = plt.subplots(1, 3, figsize=(15, 4))\n",
    "axes[0].imshow(noisy_edges, cmap=\"gray\")\n",
    "axes[0].set_title(\"Edges on Noisy (messy)\")\n",
    "axes[1].imshow(blurred_noisy, cmap=\"gray\", vmin=0, vmax=255)\n",
    "axes[1].set_title(\"Blurred First\")\n",
    "axes[2].imshow(clean_edges, cmap=\"gray\")\n",
    "axes[2].set_title(\"Edges on Blurred (clean)\")\n",
    "plt.tight_layout()\n",
    "plt.show()\n",
    "\n",
    "# Blurring first smooths the noise, so edge detection finds the real plate rim cleanly.\n",
    "# This blur-then-detect pipeline is standard in image processing.\n",
    "# CNNs (Lecture 8) learn to do this automatically.\n",
    "```\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 💡 Insight\n",
    "\n",
    "For Lior's photo quality scorer: run edge detection on each food photo and compute mean edge magnitude. **Blurry** photos have weak edges (low magnitude) — the plate rim fades out. **Noisy** photos have artificially high edge magnitude — random noise creates false edges everywhere. Both are signs of low quality, detected differently: blurry = too few edges, noisy = too many spurious edges."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 📊 Section 6: Putting It All Together\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 🗣️ Discussion: Lior's Full Report\n",
    "\n",
    "*Instructor-led — no code. Think about how you'd present today's work to a non-technical CEO.*\n",
    "\n",
    "> Lior presents to the CEO. What are his talking points?\n",
    ">\n",
    "> **NLP side:**\n",
    "> 1. Can we auto-tag listings by cuisine? How accurate? Which cuisines get confused — and why?\n",
    "> 2. What are the most important words for identifying each cuisine? (Look at the coefficients we printed above.)\n",
    ">\n",
    "> **Image side:**\n",
    "> 3. How can we detect low-quality photos using filters? (Think: what does edge magnitude tell us about blurry vs. noisy photos?)\n",
    "> 4. Can we auto-enhance blurry photos? What filter would help — and when would it fail?\n",
    ">\n",
    "> **Big picture:**\n",
    "> 5. How does today's work connect to the models from Lectures 3–6? (Hint: the document-term matrix is just another feature matrix; image filters produce feature maps that CNNs will learn automatically in Lecture 8.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "### 🎓 What You Just Did\n",
    "\n",
    "**NLP:**\n",
    "- Preprocessed raw text: lowercase → tokenize → stopwords → lemmatize\n",
    "- Built a Bag-of-Words document-term matrix with scikit-learn\n",
    "- Measured text similarity using cosine similarity on BoW vectors\n",
    "- Classified meal descriptions by cuisine using Logistic Regression\n",
    "\n",
    "**Image Analysis:**\n",
    "- Created grayscale images as numpy arrays\n",
    "- Applied convolution filters: edge detection, blur, sharpen\n",
    "- Discovered that blur + edge detection = clean feature extraction\n",
    "- Designed a photo quality metric from edge strength\n",
    "\n",
    "### The Unstructured Data Toolkit\n",
    "\n",
    "```python\n",
    "# NLP\n",
    "CountVectorizer()              # text → word count matrix (BoW)\n",
    "cosine_similarity(X_bow)       # find similar documents\n",
    "\n",
    "# Images\n",
    "convolve2d(image, kernel)      # apply a filter\n",
    "edge_v, edge_h                 # detect edges\n",
    "blur = np.ones((3,3)) / 9     # smooth noise\n",
    "sharpen                        # enhance details\n",
    "```\n",
    "\n",
    "### 📅 What's Next\n",
    "\n",
    "- **Lecture 8**: Deep Learning — models that learn their OWN features (no hand-designed filters or word counts)\n",
    "- **Final Project**: Announced in Lecture 8 — apply everything from Lectures 1–7 to a real business problem\n",
    "\n",
    "### 💬 One Last Thing\n",
    "\n",
    "\"Every previous recitation gave you structured data in rows and columns. Today you processed raw text and raw pixels — the 80% of business data that was previously invisible to your models. The document-term matrix and the feature map are the bridges. With these, your entire Lectures 3–6 toolkit works on text and images too. That's not a new skill — it's an extension of everything you already know.\""
   ]
  }
 ]
}