Concepts / Lexical Similarity and Surface Matching

Lexical Similarity and Surface Matching

Lexical similarity measures whether texts look alike, while semantic similarity measures whether they mean alike.

  • Programming

When Words Mislead

Suppose you want to check whether a model's answer matches a reference answer. Looking for shared words is a natural first step, but it answers only one part of the question. Two texts can use different words and still mean much the same thing; nearly identical text can also express different meanings. Lexical similarity measures how alike texts look, while semantic similarity measures how alike their meanings are.

What do you think happens?

The sentences “Let's eat, grandma” and “Let's eat grandma” differ by one comma. Does their near-identical surface form mean their meanings are equivalent?

Reveal answer
Answer:

No. The comma changes the meaning: one sentence is an invitation to eat with grandma, while the other says grandma is the thing being eaten.

A lexical comparison sees almost identical strings. A meaning-based comparison needs to account for the punctuation's effect on meaning.

Punctuation pairHigh overlap; differentmeaningsSynonym pairLower overlap; closemeanings
How can word overlap and shared meaning diverge? One pair has high surface overlap but different meanings; the other has lower overlap but close meanings.
Sentence pairSurface formMeaning
“Let's eat, grandma” / “Let's eat grandma”Nearly identical; the difference is a comma.Different: the comma changes who or what is being eaten.
“The car is fast” / “The automobile is quick”Fewer words are shared.Similar in meaning; this is a generated illustration.

Surface form and meaning are separate things to compare.

From Text to Embeddings

Lexical similarity compares aspects of the text's form, such as shared words and characters. Semantic similarity asks whether the texts have similar meanings, regardless of how their words are spelled or arranged. It cannot be computed directly from raw words in the same way as lexical similarity: first, each text must be transformed into a numerical representation called an embedding. An embedding is a list of numbers that stands in for a piece of text, allowing the texts to be compared mathematically. Since semantic similarity compares these numerical representations, it is also called embedding similarity.

Text A“The car is fast”Embedding A[0.8, 0.6]Text B“The automobile is quick”Embedding B[0.7, 0.7]represented byrepresented by
What numeric vectors might stand in for two texts? These made-up values illustrate the text-to-vector mapping; they are not claimed to be outputs from a particular embedding method.

The vector is a representation used for comparison, not the text itself. The embedding method is a separate part of the process. For this topic, the key idea is that once the texts have embeddings, a metric can compare those embeddings.

Reading Cosine Similarity

Definition

Cosine similarity compares two embeddings by dividing their dot product by the product of their Euclidean norms. The normalization means the score reflects the direction of the vectors rather than their lengths. The score ranges from -1 to 1.

Rule

The dot product multiplies values in matching positions and adds the results. A Euclidean norm, also called an L2 norm, is the square root of the sum of a vector's squared values. For example, the norm of [0.11, 0.02, 0.54] is $\sqrt{0.11^{2}+0.02^{2}+0.54^{2}}$. Dividing by both norms removes the effect of vector length from the comparison.

Worked example

Calculate a Cosine Score

For a small, generated example, let $A=[1,2]$ and $B=[2,1]$. Find their cosine similarity.

  1. 1
    Find the dot product

    $A\cdot B=(1)(2)+(2)(1)=4$.

  2. 2
    Find the norms

    $\lVert A\rVert_2=\sqrt{1^{2}+2^{2}}=\sqrt{5}$ and $\lVert B\rVert_2=\sqrt{2^{2}+1^{2}}=\sqrt{5}$.

  3. 3
    Divide

    The cosine score is $\frac{4}{\sqrt{5}\sqrt{5}}=\frac{4}{5}=0.8$.

Result

The score is 0.8. It is between the maximum of 1 and the minimum of -1, indicating that the vectors point in similar but not identical directions.

AlignedAngle 0°; score 1PerpendicularAngle 90°; score 0OppositeAngle 180°; score -1
How does the angle between two vectors relate to the score? Aligned vectors score 1, perpendicular vectors score 0, and opposite vectors score -1.

A score of 1 means the embeddings point in the same direction; a score of -1 means they point in opposite directions. A score of 0 corresponds to perpendicular vectors, which have no directional alignment in this comparison. Values between the extremes indicate partial agreement. These scores describe the relationship between embeddings; interpreting them as a judgment about text meaning also depends on the quality of those embeddings.

Where Score Reliability Can Falter

A semantic similarity score depends on how the embeddings were produced. Different embedding algorithms can produce different embeddings for the same text, so comparing those embeddings can yield different scores. Reliability also depends on embedding quality: poor embeddings may give a low score even when two texts have the same meaning. A low score is therefore evidence about the representations being compared as well as about the texts.

Embedding algorithmDifferent methods candifferText embeddingsQuality mattersCosine comparisonSimilarity scoreMay vary with embeddingsproducesare comparedyields
Where can variation and reliability concerns enter? The embedding method determines the representations, and their quality affects how trustworthy the comparison is.

There can also be a practical cost: producing embeddings may require substantial computation and time. When many texts must be scored, generating embeddings can cost more than the cosine comparison itself. Named semantic textual similarity metrics include BERTScore, which uses embeddings generated with BERT, and MoverScore, which uses embeddings from a mixture of algorithms. Because both rely on embeddings, their scores are affected by the advantages and drawbacks of their embedding sources.

  • Common mistake

    Treating word overlap as a reliable substitute for shared meaning.

    What it looks like

    “The car is fast” and “The automobile is quick” share fewer words but can express similar meanings.

    Why it fails

    Lexical similarity concerns surface form; semantic similarity concerns meaning.

    Do this instead

    Ask separately whether the texts look alike and whether they mean alike.

  • Common mistake

    Treating a high cosine score as a guarantee that two texts mean the same thing.

    What it looks like

    The cosine score compares embeddings, and the embeddings depend on the method that produced them.

    Why it fails

    Different methods can produce different representations, and poor embeddings can misrepresent texts.

    Do this instead

    Interpret the score in light of the embedding method and the quality of its representations.

  • Common mistake

    Assuming the cosine score depends on vector length.

    What it looks like

    The formula divides the dot product by the product of the two Euclidean norms.

    Why it fails

    That normalization removes the effect of vector length so the score reflects direction.

    Do this instead

    Use the full cosine formula, including both norms.

Practice the Comparison

MEDIUM

Consider the generated pair “A child rides a bicycle” and “A kid is cycling.” First, compare their surface form: which words or characters overlap? Then consider whether their meanings are close. If an embedding method returned vectors $A=[1,0]$ and $B=[0,1]$, what would their cosine score be, and what does that score say about the vectors?

Hints
  • Keep the surface-form question separate from the meaning question.
  • The dot product of these two vectors is $0$.
  • Each vector has Euclidean norm $1$.

For the stated vectors, the cosine score is $0$. The vectors are perpendicular, so this score describes their directions. It does not by itself prove that the sentences have unrelated meanings: the meaning-based interpretation depends on whether the embedding method has represented them well.

Key Takeaways

  • Lexical similarity compares surface form, including shared words and characters; semantic similarity compares meaning.
  • An embedding is a numerical representation of text. Semantic similarity is also called embedding similarity because embeddings are what get compared.
  • Cosine similarity divides the dot product by the product of the vectors' Euclidean norms; its range is from -1 to 1.
  • Scores of 1, 0, and -1 correspond to aligned, perpendicular, and opposite vector directions.
  • A semantic similarity score depends on the embedding method and the quality of its representations.

Related concepts

More concepts starting with L