Semantic Similarity
Two sentences can share no words yet mean the same thing — or share words yet mean something completely different. Semantic similarity measures how close two pieces of text are in meaning, using embeddings. It's what powers search that understands intent, not just keywords, and it's the idea behind retrieval, deduplication, and recommendations.
💡 In one line: Semantic similarity measures how close two texts are in meaning by comparing their embedding vectors.
What is Semantic Similarity?
It's a measure of how alike two texts are in meaning. You compute it by embedding both texts with the same model and comparing their vectors: the closer the vectors, the more similar the meaning.
Semantic vs. Lexical Similarity
These two are easy to confuse:
- Lexical similarity — overlap of the exact words or characters.
- Semantic similarity — overlap of meaning.
They often disagree:
- "car" vs "automobile" — low lexical, high semantic.
- "the bank raised rates" vs "he sat on the river bank" — high lexical (shared word "bank"), low semantic.
How It Works
The process is short.Â
Similarity Scores
The comparison produces a number — commonly a cosine similarity between 0 and 1, where higher means more similar. In practice you pick a threshold (e.g. "count as a match above 0.75") to turn the score into a decision.
Why It Matters
- Semantic search — find results by meaning, not keywords.
- RAG — retrieve the most relevant chunks for an LLM.
- Duplicate / paraphrase detection — spot texts that say the same thing.
- Recommendations, clustering, classification, and FAQ matching.
Measuring Closeness
To turn "closer vectors" into a score, you need a distance or angle measure. The two most common are cosine similarity (the angle between vectors) and Euclidean distance (the straight-line gap) — the next two subtopics.
Code Example
Different words, similar meaning — and the score reflects it.
Caveats
- Results depend on the embedding model's quality and domain.
- Similarity is relative, not absolute truth.
- Scores usually need calibration and thresholds per use case.
Summary
- Semantic similarity measures closeness in meaning via embedding vectors.
- It differs from lexical similarity — different words can mean the same thing, and shared words can mean different things.
- The process: embed both texts → compare vectors → similarity score.
- Scores (often cosine, 0–1) drive search, RAG, dedup, and recommendations.
- Closeness is measured with cosine similarity or Euclidean distance (next). EOF echo created