How can a Python program find the sentence most similar to a question?
A customer types “What is your address?” A plain word search would only find lines containing those exact words. But a small AI model can compare the meaning of the question with every sentence in a file, and pick the closest one — even if the wording is different.
The program in this note does exactly that for a small restaurant: it reads a text file of restaurant facts, finds the sentence most similar to the question, and prints it, in 20 lines. No account, no key, no cost — everything runs on your own computer.
The data file
The restaurant's knowledge is a plain text file, sentences-restaurant.txt, with one
fact per line:
Opening hour 9am-11pm
Address : Unit 10, 2F, Festival Walk, Kowloon Tong
Set lunches beef set $100, Pork set $110, Chicken set $120
The file is included in the demo zip, so you can run the program first and edit the sentences later.
The idea in one minute
Two pieces of mathematics do the work. First, a pre-trained model turns every sentence into a list of numbers, called an embedding: picture the numbers as coordinates, so every sentence becomes a point, and sentences with similar meanings end up close together. Second, cosine similarity compares two of these number lists by measuring the angle between them. A score of 1 means same direction, same meaning; 0 means unrelated. The program picks the sentence with the highest score — and only answers if that score is good enough. This meaning-based way of finding text, without a generative model inventing anything, is the subject of my working paper: Lam, Pong Yuen (2026), “Natural Language Processing: Foundations and Applications without Hallucination from Generative AI”, Working Paper. Available at SSRN: https://doi.org/10.2139/ssrn.7384558. See also my research page.
The program, piece by piece
from sentence_transformers import SentenceTransformer
import numpy as np
model = SentenceTransformer('BAAI/bge-m3')
Two libraries are brought in. SentenceTransformer loads a ready-made model that
turns sentences into embeddings; BAAI/bge-m3 is a model trained by the Beijing
Academy of Artificial Intelligence that handles many languages well, so we do not train anything
ourselves — its model page is at
huggingface.co/BAAI/bge-m3.
The first run downloads about 2 GB, so it needs the internet once; after that
the model is saved on the computer. numpy, shortened to np by habit,
is the standard library for fast number-list mathematics.
with open('sentences-restaurant.txt', 'r', encoding='utf-8') as f:
sentences = [line.strip() for line in f.readlines() if line.strip()]
user_question = "What is your address?"
The file is read into a list of sentences, with surrounding spaces removed and empty lines skipped. The question lives on its own line, so it is easy to change and ask something else.
question_embedding = model.encode(user_question.strip().lower())
sentence_embeddings = model.encode([s.lower() for s in sentences])
model.encode turns text into embeddings: first the question, then every sentence in
the file. Everything is lowercased first so that capital letters do not affect the comparison.
An embedding is also called a vector — an arrow of numbers pointing at
the sentence's meaning.
dot_product = np.dot(sentence_embeddings, question_embedding)
dense_scores = dot_product / (np.linalg.norm(sentence_embeddings, axis=1) * np.linalg.norm(question_embedding))
The dot product
(np.dot) multiplies two vectors together; dividing it by the two
lengths (np.linalg.norm — norm means length) gives the
cosine of the angle between them. The result, dense_scores, is one score per
sentence: the question compared with line 1, line 2, line 3, and so on.
ranked_indices = np.argsort(dense_scores)[::-1]
i = ranked_indices[0]
if dense_scores[i] > 0.5:
print(f"Your question is: {user_question}")
print(f"You can find answer from the sentence number {i}")
print(f"The sentence is: {sentences[i]}")
else:
print("no answer")
np.argsort sorts the scores and returns their positions; [::-1]
reverses the order so the best score comes first, and ranked_indices[0] is the
winning sentence's number. Then the important part: the program answers only if the best score
is above 0.5. If nothing in the file is close enough, it honestly prints
no answer instead of guessing.
The whole program
from sentence_transformers import SentenceTransformer
import numpy as np
model = SentenceTransformer('BAAI/bge-m3')
with open('sentences-restaurant.txt', 'r', encoding='utf-8') as f:
sentences = [line.strip() for line in f.readlines() if line.strip()]
user_question = "What is your address?"
question_embedding = model.encode(user_question.strip().lower())
sentence_embeddings = model.encode([s.lower() for s in sentences])
dot_product = np.dot(sentence_embeddings, question_embedding)
dense_scores = dot_product / (np.linalg.norm(sentence_embeddings, axis=1) * np.linalg.norm(question_embedding))
ranked_indices = np.argsort(dense_scores)[::-1]
i = ranked_indices[0]
if dense_scores[i] > 0.5:
print(f"Your question is: {user_question}")
print(f"You can find answer from the sentence number {i}")
print(f"The sentence is: {sentences[i]}")
else:
print("no answer")
Running it on your own computer
Download the zip file and unzip
it to a folder. The zip holds the program, the sentences-restaurant.txt data file,
and a pyproject.toml project file. Open the folder in Visual Studio Code and type
these three lines in the terminal, one at a time:
uv python pin 3.12.10
uv sync
uv run cos_similarity12_restaurant.py
The first line records which Python version this folder uses. The second line sets up the
project and installs the two packages the program needs, sentence-transformers and
numpy. The third line runs the program. The first run takes longer because of the
model download; after that, the answer appears in a few seconds:
Similarity scores: [0.5011991 0.5202471 0.39348638]
Ranked indices: [1 0 2]
Your question is: What is your address?
You can find answer from the sentence number 1
The sentence is: Address : Unit 10, 2F, Festival Walk, Kowloon Tong
Read the first line against the data file: the address sentence scored 0.520, the opening-hours sentence 0.501 — close behind, because hours are also a practical detail — and the set-lunch sentence 0.393. The winner only just passed the 0.5 bar, which is typical for such a short question; a richer file widens the gap.
If you have not set up uv yet, start with How to install Visual Studio Code? and then the uv note for Windows or Mac.
Limitations
- The 0.5 bar is a guess. Set it lower and weak matches slip through; set it
higher and good questions get
no answer. Try a few questions and adjust it to your file. - It finds a sentence, it does not write an answer. The reply is always a line from the file, word for word. The advantage is that there is no chance for AI to hallucinate — that is, to generate an answer that may be wrong. Of course, we can do both: display the sentence so that we can read it ourselves, and also ask AI to read the sentence and generate an answer from it. In this way, we can easily double-check the sentence found to be most similar against the answer generated by AI — see the DeepSeek note below.
- The first run is heavy. The model download is about 2 GB and needs the internet once; later runs are offline and fast.
- Very short questions are hard to place. A two-word question carries little meaning to compare, so scores stay low for every sentence.
Further reading
- The Sentence Transformers documentation lists other pre-trained models, including smaller and faster ones.
- How does a program split a text into sentences? shows how a larger document can be prepared as clean sentences before matching.
- How can a Python program use the DeepSeek API? is the natural next step: hand the found sentence to a chat model so it can write a natural reply.
- How to mitigate AI hallucination with Retrieval-Augmented Generation (RAG) by providing a webpage? is the same two-step idea at full scale: find similar text first, then let a model answer from it.