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.
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.
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.
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
- How to determine the encoding of text
- How to determine the language of a piece of text?
- How to embed a text file in a .NET assembly?
- How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?
- How to embed small icon in UILabel
- How to encode dependency path as a feature for classification?
- How to fetch vectors for a word list with Word2Vec?
- How to find all permutations of a given word in a given text?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.