Sentence Similarity
Topic Analysis
Semantic Analysis
Text Comparison
Natural Language Processing

How to determine if two sentences talk about similar topics?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Determining whether two sentences talk about similar topics is a semantic similarity problem, not just a string-matching problem. The right method depends on how accurate you need to be and how much infrastructure you can afford. For lightweight tasks, TF-IDF plus cosine similarity is often enough. For better topic understanding, sentence embeddings usually work better.

Start With the Difference Between Words and Meaning

Two sentences can talk about the same topic without sharing many exact words.

  • "The doctor treated the patient in the hospital."
  • "A physician provided care at the medical center."

A pure keyword approach may miss that similarity because the wording changed. That is why topic similarity usually requires either a vector representation or a model that captures semantics.

A Simple Baseline: TF-IDF With Cosine Similarity

A strong baseline is to vectorize the sentences with TF-IDF and compare the vectors using cosine similarity.

python
1from sklearn.feature_extraction.text import TfidfVectorizer
2from sklearn.metrics.pairwise import cosine_similarity
3
4sentences = [
5    "Artificial intelligence is changing healthcare.",
6    "Machine learning is transforming medicine.",
7]
8
9vectorizer = TfidfVectorizer(stop_words="english")
10X = vectorizer.fit_transform(sentences)
11score = cosine_similarity(X[0], X[1])[0][0]
12print(round(score, 3))

This is easy to run and often good enough for short pipelines, search ranking, or simple duplicate detection.

The main limitation is that TF-IDF mostly sees shared words and frequencies. It does not deeply understand synonyms or context.

Sentence Embeddings Work Better for Semantic Similarity

When the wording can vary a lot, embeddings are a better fit. A sentence embedding model maps each sentence to a dense numeric vector where semantically related sentences tend to land closer together.

python
1from sentence_transformers import SentenceTransformer
2from sklearn.metrics.pairwise import cosine_similarity
3
4model = SentenceTransformer("all-MiniLM-L6-v2")
5sentences = [
6    "Artificial intelligence is changing healthcare.",
7    "Machine learning is transforming medicine.",
8]
9embeddings = model.encode(sentences)
10score = cosine_similarity([embeddings[0]], [embeddings[1]])[0][0]
11print(round(float(score), 3))

This usually performs much better on paraphrases because the model captures semantic relationships beyond exact token overlap.

Similar Topic Is Not the Same as Same Meaning

It is useful to separate three related questions:

  • are the sentences lexically similar
  • are they about the same topic
  • do they express the same claim

For example:

  • "Python is a popular language for data science."
  • "Python's syntax is easier to learn than C++."

These share a topic area, but they are not saying the same thing. Depending on your application, that may still count as similar enough.

So before choosing a threshold, define whether you want topic overlap or near-duplicate meaning.

Preprocessing Still Matters

Even good models benefit from consistent text handling. Useful steps include:

  • lowercasing when the model or vectorizer expects it
  • removing obvious formatting noise
  • normalizing contractions or punctuation if the pipeline is simple
  • avoiding over-aggressive stop-word removal in very short texts

Preprocessing is especially important for TF-IDF baselines because the representation is fragile compared with embedding models.

Thresholds Need Empirical Tuning

A score only becomes useful once you decide what score counts as "similar." There is no universal cutoff such as 0.7 that works everywhere.

A practical approach is:

  • collect example sentence pairs
  • label them as similar or not similar for your use case
  • compute scores
  • choose a threshold that fits the business tradeoff between false positives and false negatives

That is much better than guessing a threshold from intuition.

Use the Simplest Method That Meets the Need

If you are building a quick classifier or a rules-heavy system, TF-IDF may be enough. If you need paraphrase awareness, FAQ matching, semantic search, or topic clustering, embeddings are usually worth it.

The right answer is not the most advanced model. It is the simplest method that performs well enough on your actual data.

Common Pitfalls

  • Treating topic similarity as simple keyword overlap.
  • Using TF-IDF and expecting strong synonym or paraphrase detection.
  • Assuming a similarity threshold from a blog post will fit your dataset.
  • Confusing "same topic" with "same exact meaning."
  • Evaluating on only a few handpicked examples instead of a labeled sample.

Summary

  • Sentence topic similarity is usually best treated as a semantic similarity problem.
  • TF-IDF plus cosine similarity is a good lightweight baseline.
  • Sentence embeddings usually perform better when wording differs.
  • Define whether you care about topic overlap or exact semantic equivalence.
  • Tune similarity thresholds on labeled examples from your real use case.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.