Scoring Word Guesses with Semantic Similarity and spaCy
Introduction: Making Guesses Smarter
Welcome to the first lesson of our course, “Enhancing the Word Play Game with new functionalities” In this lesson, we will make our word prediction game more interactive and fair by adding a way to score player guesses. Instead of only rewarding exact matches, we want to give points for guesses that are close in meaning to the correct answer. This will make the game more fun and challenging, and it will feel more like how people actually use language.
By the end of this lesson, you’ll know how to compare two words for similarity and assign a score based on how close they are in meaning. This is a key step in building a smarter, more engaging game.
What Is Semantic Similarity?
Semantic similarity is a way to measure how close two words are in meaning. For example, car and automobile mean almost the same thing, so they are semantically similar. On the other hand, car and banana are not similar at all.
Here’s a simple table to show some examples:
| Word 1 | Word 2 | Are they similar? |
|---|---|---|
| car | automobile | Yes |
| cat | kitten | Somewhat |
| car | banana | No |
In our game, we want to reward players for making guesses that are close in meaning, not just exact matches. This makes the game fairer and more fun.
How Computers Compare Word Meanings
To compare word meanings, computers use something called word vectors or embeddings. You can think of a word vector as a list of numbers that represents the meaning of a word. Words with similar meanings have vectors that are close together.
For example, the word cat might be represented by a vector like [0.2, 0.5, 0.1, ...], and kitten might have a vector that is very close to it. The word banana would have a very different vector.
We can measure how close two vectors are using a mathematical formula called cosine similarity:
Let’s break down the formula:
- and are the word vectors for the two words you want to compare. Each vector is a list of numbers that represents the meaning of a word.
- is the dot product of the two vectors. This is a way to combine the two lists of numbers into a single value that reflects how much they point in the same direction.
- and are the lengths (also called the “norms” or “magnitudes”) of each vector. You can think of this as how long each arrow is if you drew it on a graph.
- divides the dot product by the lengths of the two vectors. This makes sure the similarity score is always between -1 and 1, where 1 means the vectors point in exactly the same direction (very similar), 0 means they are completely unrelated, and -1 means they are opposite (which doesn’t usually happen with word vectors).
In simple terms, this formula measures how close the two word meanings are by looking at the angle between their vectors: the smaller the angle, the more similar the words.
To make this easy, we will use a Python library called spaCy. spaCy comes with pre-trained word vectors, so we don’t have to build them ourselves.
