Semantic Similarity — Measuring Meaning Between Texts

Machine Learning
Introduced by
Nils Reimers & Iryna Gurevych (Sentence-BERT)
Year
2019
Type
NLP text-comparison task

What does it do?

Semantic similarity analysis measures how close in meaning two texts are, rather than how many words they share on the surface.

Building on the Sentence-BERT approach of Reimers and Gurevych (2019), it represents each text as a vector and compares those vectors, so that paraphrases can score high even when their wording differs.

The intuition behind the method

Two sentences can use different words and still say the same thing, while two others can share many words and mean different things. Counting overlapping words misses this distinction. The method places each text at a point in a vector space produced by an embedding model, so that texts with similar meaning tend to lie close together. The similarity between the two points then serves as a score: high for close paraphrases, low for unrelated passages.

History

The problem of scoring how alike two sentences are in meaning is associated with the *SEM 2013 shared task on Semantic Textual Similarity (Agirre et al., 2013). Sentence-BERT (Reimers & Gurevych, 2019) produced sentence embeddings that can be compared directly with cosine similarity, which made this kind of comparison practical for larger sets of text pairs.

When to use it

Use semantic similarity when you have pairs of texts and need to judge closeness in meaning rather than shared wording, for example in duplicate detection, paragraph matching, or passage retrieval. It requires an embedding model that covers the language of the texts. It is a poor choice when your decision depends on exact wording, such as verbatim quotation checks, where a surface-level comparison is the appropriate test. Because a threshold is only as reliable as the pairs used to set it, plan for a labelled calibration set drawn from your own data before relying on a cutoff.

Limitations and considerations

  • The score is a cosine between fixed-length sentence vectors, so it reports how close two texts are but not which aspects of their meaning differ.
  • Quality is bounded by the embedding model: the scores reflect what that model learned about meaning, including any gaps for the language or domain of the texts.
  • The score follows the model's notion of meaning, which may not match a domain-specific or legal definition of 'the same'.
  • Scores from different embedding models are not on a shared scale, so a threshold set with one model does not transfer to another.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Compares the vector representations of whole texts, so paraphrases with different wording can receive high scores where a word-overlap measure would not.
  • Reduces each pair to a single cosine similarity value, which can be thresholded directly for tasks such as duplicate detection or retrieval.
  • Works across languages when the chosen embedding model covers the languages involved.

Applications

  • Plagiarism and copy detection by comparing submitted text against reference sources.
  • Paragraph and passage matching across documents.
  • Information retrieval — ranking documents by closeness in meaning to a query.
  • Deduplication of near-identical content in large text collections.

How it works

How it works

1

You start from pairs of texts to compare, for example a submitted paragraph against a reference passage, or a query against candidate documents. Both members of each pair must be available together, because each pair is scored as a unit. How many pairs you need depends on the task. A threshold is best set on a labelled set of pairs drawn from your own data, as described under interpretation.

2

Each text is passed through an embedding model that maps it to a fixed-length numerical vector. The model must cover the language of your texts, since an embedding model trained on other languages will place their sentences in poorly suited positions. Multilingual sentence-embedding models are one option for corpora that are not in English. Sentences with similar meaning are intended to map to nearby vectors even when their words differ. Record the name and version of the embedding model with every score, because scores from different models are not on a shared scale.

3

The two vectors are compared, most commonly with cosine similarity, which yields one number per pair. For vectors u and v, cosine similarity is their dot product divided by the product of their lengths, so it depends on the angle between the vectors rather than on their magnitudes. The value lies between -1 and 1. Higher values indicate that the two texts point in more similar directions in the embedding space, which is read as closer meaning. Scores are comparable across pairs only when they come from the same embedding model.

4

The scores are read in the context of the task, for example to flag likely copies, rank matching paragraphs, or retrieve relevant passages. The cutoff for the task should be calibrated on a labelled sample of pairs drawn from the same embedding model and domain, and the false-positive and false-negative rates observed on that sample should be reported alongside it. Borderline pairs are inspected before any decision is acted on.

Common pitfalls

  • Using an embedding model that does not cover the language of the corpus produces unreliable scores; match the model to the language before comparing pairs.
  • Treating the cosine value as a verdict. A cutoff should be calibrated on labelled pairs from the same model and domain, and borderline pairs inspected, rather than accepted as a number chosen by default.
  • Setting a threshold without labelled data. A cutoff chosen by intuition or on a small set of unlabelled pairs is not validated and can misclassify pairs in either direction.
  • Mixing scores produced by different embedding models in one ranking or one threshold, since the scores are not comparable across models.

Frequently asked questions

How is this different from comparing shared words?

Word-overlap measures only count matching tokens, so they miss paraphrases that mean the same thing with different words. Semantic similarity embeds each text into a meaning space and compares the vectors, so closeness in meaning is captured even when the wording differs.

Does it work in languages other than English?

Yes, provided the embedding model covers the target language. Multilingual sentence-embedding models are one option for non-English corpora; applying a model that does not cover the language gives unreliable scores.

How do I turn a similarity score into a decision?

Choose a cutoff suited to the task, for example a high cutoff for flagging likely copies, and calibrate it on a labelled sample of pairs from the same embedding model and domain, reporting the false-positive and false-negative rates on that sample. Inspect borderline pairs before acting. Scores are comparable across pairs from the same model, but a score is not itself a verdict.

How is the similarity score computed?

Each text is encoded into a vector by an embedding model, and the two vectors are compared, most commonly with cosine similarity. This yields a single number between -1 and 1, where higher values mean closer meaning as the model represents it.

Related methods

Sources

  1. Reimers, N. & Gurevych, I. (2019). Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks. EMNLP.

    Open publication reference
  2. Agirre, E. et al. (2013). *SEM 2013 shared task: Semantic Textual Similarity. ACL (*SEM).

    Open publication reference