Showing posts with label Cosine Similarity. Show all posts
Showing posts with label Cosine Similarity. Show all posts

Monday, December 2, 2024

Matching Products to Advertisements Using Machine Learning


Matching Advertisements to Products Using Machine Learning

๐Ÿง  Matching Advertisements to Products Using Machine Learning

๐Ÿ“‘ Table of Contents


๐Ÿš€ Introduction

Imagine handling thousands of product listings and advertisements daily. The ability to automatically match ads to the correct products is not just convenient—it’s essential for modern digital platforms.

๐Ÿ’ก Goal: Match each advertisement to the most relevant product—or return no match if similarity is low.

๐Ÿ” Understanding the Problem

Inputs

  • Product Data (title, description, images)
  • Advertisement Data (title, description, images)

Outputs

(ad_id, product_id)
(ad_id, None)
๐Ÿ“– Expand Explanation

If no product crosses the similarity threshold, we explicitly return None to avoid incorrect mapping.


⚠️ Challenges

  • Data inconsistency – informal vs formal descriptions
  • Multimodal data – text + images
  • Scalability – large datasets
  • Threshold tuning – subjective similarity cutoffs
๐Ÿ’ก Real-world systems fail more due to bad thresholds than bad models.

๐Ÿงฉ Solution Overview

  1. Preprocess data
  2. Compute similarity
  3. Match and filter

๐Ÿ›  Data Preprocessing

Text Processing

  • Tokenization
  • Lowercasing
  • Stopword removal
  • Lemmatization

Embedding Techniques

  • TF-IDF
  • Word2Vec
  • BERT / Sentence-BERT

Image Processing

  • Use CNN models (ResNet, EfficientNet)
  • Extract feature vectors

๐Ÿ“ Similarity Measurement

Cosine Similarity Formula

cos(ฮธ) = (A · B) / (||A|| ||B||)

This measures how similar two vectors are based on angle rather than magnitude.

Euclidean Distance

d = √(ฮฃ (xi - yi)^2)

Multimodal Combination

Final Score = w1 * text_similarity + w2 * image_similarity
๐Ÿ“– Why Combine Modalities?

Text alone may miss visual similarity. Images alone may miss context. Together, they give better accuracy.


⚙️ Machine Learning Pipeline

  1. Feature extraction (text + image)
  2. Embedding storage
  3. Similarity computation
  4. Ranking + threshold filtering
๐Ÿ’ก Precompute embeddings to improve speed drastically.

๐Ÿ’ป Code Example

from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity

model = SentenceTransformer('all-MiniLM-L6-v2')

ad_embedding = model.encode(ad_text)
product_embedding = model.encode(product_text)

score = cosine_similarity([ad_embedding], [product_embedding])
print(score)

๐Ÿ–ฅ CLI Output Example

Processing Ads...
Embedding Generated ✔
Calculating Similarity...

Ad 101 → Product 55 (Score: 0.87)
Ad 102 → None (Score: 0.32)
๐Ÿ“‚ Expand CLI Explanation

Scores above threshold (e.g., 0.75) are accepted. Others are rejected to avoid false matches.


๐Ÿšง Common Issues & Solutions

1. Data Imbalance

Use precision/recall instead of accuracy.

2. Noisy Data

Apply spell correction and filtering.

3. Performance

Use FAISS for fast nearest neighbor search.

4. Threshold Problems

Use validation data for tuning.


๐ŸŽฏ Key Takeaways

  • Multimodal learning improves accuracy
  • Embeddings are the foundation
  • Threshold tuning is critical
  • Scalability requires smart indexing

๐Ÿ“Œ Final Thoughts

Matching advertisements to products is not just a machine learning task—it’s a system design challenge. The best solutions combine strong modeling, efficient computation, and continuous evaluation.

If implemented correctly, this approach can significantly improve automation, reduce manual effort, and enhance user experience in any data-driven platform.

Monday, October 14, 2024

Automated Financial News Summarization and Evaluation Using BLEU Score


Financial News Summarization using NLP and BLEU Score Evaluation

Financial News Summarization using NLP and BLEU Score Evaluation

Natural Language Processing (NLP) has transformed how we analyze large amounts of text data. One important application of NLP is automatic text summarization, where lengthy articles are condensed into shorter summaries while preserving key information.

In finance, news arrives continuously from multiple sources. Investors, analysts, and traders often struggle to read every article related to stock markets. Automatic summarization helps reduce information overload by generating concise summaries from large collections of financial news.

In this tutorial, we will explore a complete NLP pipeline that:

  • Fetches financial news articles
  • Processes and cleans text
  • Generates summaries using clustering
  • Evaluates summaries using BLEU score

๐Ÿ’ก What You Will Learn

  • How financial news APIs work
  • How NLP preprocessing works
  • Sentence tokenization techniques
  • Stopword removal
  • Cosine similarity calculations
  • KMeans clustering for summarization
  • BLEU score evaluation
  • Text similarity mathematics
  • Summary evaluation metrics
  • Python NLP workflow design

Table of Contents


1. Introduction to Financial NLP

Financial markets generate enormous amounts of textual information daily:

  • News reports
  • Earnings announcements
  • Economic updates
  • Market analysis
  • Company press releases

Reading all this manually is impossible.

NLP systems automate:

$$ Text \ Processing \rightarrow Information \ Extraction \rightarrow Summarization $$

This allows investors to quickly understand important developments.


2. Fetching Financial News Articles

The first step is collecting news articles related to stock symbols.

The system uses:

$$ NewsAPI $$

to fetch recent financial articles.

News Fetching Workflow

  1. Select stock symbols
  2. Send API request
  3. Retrieve article data
  4. Filter invalid articles
  5. Combine content into a document

Python Example


from newsapi import NewsApiClient

api = NewsApiClient(api_key='YOUR_API_KEY')

articles = api.get_everything(
    q='AAPL',
    from_param='2023-08-17',
    to='2023-09-01',
    language='en'
)

Why Filtering Matters

Some articles may contain:

  • Missing descriptions
  • Broken content
  • Duplicate entries
  • Incomplete titles

Filtering improves overall summary quality.


3. Text Preprocessing

Raw text contains unnecessary words and symbols.

Preprocessing transforms raw text into cleaner structured data.

Preprocessing Steps

Step Purpose
Lowercasing Normalize text
Tokenization Split sentences and words
Stopword Removal Remove common words
Punctuation Removal Clean symbols

Example

Original Sentence:


Apple stock surged after strong quarterly earnings.

After preprocessing:


apple stock surged strong quarterly earnings

4. Sentence Tokenization

Tokenization breaks large text into smaller units.

Sentence Tokenization

Separates text into sentences.

Word Tokenization

Separates sentences into words.

Python Example


from nltk.tokenize import sent_tokenize

sentences = sent_tokenize(document)

Mathematical Representation

Suppose:

$$ Document = \{S_1, S_2, S_3, ..., S_n\} $$

Each sentence becomes an individual processing unit.


5. Building the Similarity Matrix

The summarizer calculates similarity between sentences.

This determines which sentences discuss similar topics.

Cosine Similarity

Cosine similarity measures angular similarity between vectors.

$$ CosineSimilarity(A,B)= \frac{A \cdot B} {||A|| ||B||} $$

Interpretation

Value Meaning
1 Identical sentences
0 No similarity
-1 Opposite direction

Similarity Matrix Example

S1 S2 S3
S1 1.0 0.8 0.2
S2 0.8 1.0 0.1
S3 0.2 0.1 1.0

6. KMeans Clustering for Summarization

KMeans groups similar sentences together.

The algorithm attempts to minimize:

$$ \sum_{i=1}^{k} \sum_{x \in C_i} ||x - \mu_i||^2 $$

Meaning of Symbols

Symbol Meaning
\(C_i\) Cluster
\(\mu_i\) Cluster centroid
\(x\) Sentence vector

Why Clustering Helps

Instead of selecting random sentences:

  • Clusters group related information
  • Representative sentences capture major themes
  • Redundant information is reduced

Python Example


from sklearn.cluster import KMeans

kmeans = KMeans(n_clusters=5)

kmeans.fit(similarity_matrix)

7. Generating the Summary

After clustering:

  • Representative sentences are selected
  • Key information is extracted
  • A concise summary is formed

Summary Goal

$$ Important \ Information \rightarrow Shorter \ Representation $$

Example Workflow

  1. Cluster similar sentences
  2. Find centroid sentence
  3. Select representative sentence
  4. Combine selected sentences
Why Extractive Summarization Works

Extractive summarization selects existing sentences from documents.

Advantages:

  • Preserves factual correctness
  • Simpler implementation
  • Lower hallucination risk

Disadvantages:

  • Can sound less natural
  • May include redundant wording

8. Evaluating the Summary using BLEU Score

BLEU stands for:

$$ Bilingual \ Evaluation \ Understudy $$

Originally designed for machine translation, BLEU is now widely used in NLP evaluation.

Purpose of BLEU

BLEU compares:

$$ Generated \ Summary $$

against:

$$ Reference \ Summary $$

BLEU Score Range

BLEU Score Interpretation
1.0 Perfect match
0.7 - 0.9 Very strong similarity
0.4 - 0.6 Moderate similarity
0.0 - 0.3 Weak similarity

9. Mathematics Behind BLEU Score

BLEU evaluates overlapping word sequences called:

$$ n-grams $$

BLEU Formula

$$ BLEU = BP \times exp \left( \sum_{n=1}^{N} w_n \log p_n \right) $$

Explanation

Symbol Meaning
\(BP\) Brevity penalty
\(p_n\) n-gram precision
\(w_n\) Weight

Brevity Penalty

Short summaries should not unfairly receive high scores.

$$ BP = \begin{cases} 1 & c > r \\ e^{(1-r/c)} & c \leq r \end{cases} $$

Where:

  • \(c\) = candidate summary length
  • \(r\) = reference summary length

10. Python BLEU Score Example


from nltk.translate.bleu_score import sentence_bleu

reference = [["apple", "reported", "strong", "earnings"]]

candidate = ["apple", "reported", "excellent", "earnings"]

score = sentence_bleu(reference, candidate)

print(score)

Expected Output


0.75

This indicates strong similarity.


11. CLI Output Examples

Python Execution


python summarize_news.py

CLI Output Example


Fetching financial news...

Articles Retrieved: 42

Generating summary...

Summary Generated Successfully

Calculating BLEU Score...

BLEU Score: 0.68

Generated Summary Output


Apple reported strong quarterly earnings while
investors reacted positively to revenue growth
and improved market forecasts.

Reference Summary


Apple posted strong earnings results and market
confidence increased after positive forecasts.

12. Real World Applications

Financial summarization systems are used in:

  • Stock market analysis
  • Trading dashboards
  • News aggregation platforms
  • Portfolio monitoring systems
  • AI-powered investment assistants

Algorithmic Trading

Some systems automatically analyze:

$$ News \ Sentiment \rightarrow Trading \ Decisions $$

Risk Analysis

Financial institutions use NLP to detect:

  • Negative sentiment
  • Economic warnings
  • Market instability

Key NLP Insights

  • NLP enables automatic text understanding.
  • Summarization reduces information overload.
  • Clustering groups related sentences.
  • Cosine similarity measures sentence closeness.
  • BLEU score evaluates summary quality.
  • Financial NLP has major industry applications.

13. Conclusion

In this tutorial, we explored how NLP can automatically summarize financial news articles and evaluate summary quality using BLEU score.

The workflow included:

  • Fetching financial news
  • Preprocessing text
  • Building similarity matrices
  • Applying KMeans clustering
  • Generating summaries
  • Evaluating summaries mathematically

This demonstrates how machine learning and NLP techniques can transform massive amounts of textual information into concise actionable insights.

As financial markets continue generating enormous volumes of data, intelligent summarization systems will become increasingly important for investors, analysts, and automated decision-making systems.

๐ŸŽฏ Final Takeaways

  • Financial NLP automates news understanding.
  • Summarization reduces reading time.
  • Clustering identifies important information.
  • BLEU score measures summary similarity.
  • Mathematics powers NLP evaluation systems.
  • NLP is becoming essential in finance.

Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts