Your email app quietly moves junk mail into a spam folder. A news app puts stories under "Sports" or "Business" without anyone sorting them by hand. How does a computer decide which group a piece of text belongs to?
Nobody writes a rule for every possible sentence. Instead, the computer learns from examples. In this project you will do exactly that: teach a computer to tell whether a message is about cricket or about food, and then test it on sentences it has never seen.
The idea: learning from labelled examples
Imagine your little cousin is learning to sort the family's WhatsApp messages into two piles. You show them a few: "India won by five runs" goes in the cricket pile, "Mum made aloo paratha" goes in the food pile. After a dozen examples, they start noticing clues. Words like wicket, bowler and six mean cricket. Words like dosa, lassi and spicy mean food.
Machine learning works the same way. We give the computer:
- Examples (here, short messages), and
- Labels (the correct answer for each one: "cricket" or "food").
The computer studies them and builds a model: a set of learned patterns it can use to label new messages. Learning from examples that already have the right answers is called supervised learning. Sorting things into groups is called classification.
How does a computer read words?
Computers work with numbers, not words. So first we turn each message into numbers by counting words. "the bowler took three wickets" becomes something like: the = 1, bowler = 1, took = 1, three = 1, wickets = 1, and 0 for every other word the model knows. A tool called CountVectorizer does this for us.
Then a classifier called Naive Bayes looks at which words show up more often in cricket messages and which show up more often in food messages. When it sees a new message, it checks the words and picks the label that fits best.
Try it: build your message sorter
This project uses scikit-learn, a popular free machine learning library for Python.
- Install it once by typing
pip install scikit-learnin your terminal or command prompt. (Ask a grown-up to help if this is your first time.) - Open a new Python file and type the program below.
- Run it and read the three predictions.
- Compare them with the output shown after the code.
# Teach a computer to sort messages into "cricket" or "food"
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
messages = [
"what a six by the batsman",
"the bowler took three wickets",
"india won the match by five runs",
"great catch at the boundary",
"the captain chose to bat first",
"he scored a century in the test match",
"mum made hot aloo paratha for breakfast",
"this masala dosa is so crispy",
"i want mango lassi with lunch",
"the biryani was spicy and tasty",
"we ate pani puri after school",
"dal and rice is my favourite dinner",
]
labels = ["cricket"] * 6 + ["food"] * 6
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(messages) # words -> counts
model = MultinomialNB()
model.fit(X, labels) # learn from examples
new_messages = [
"the bowler was too fast for the batsman",
"can we have paratha and lassi",
"a crispy dosa after the match",
]
predictions = model.predict(vectorizer.transform(new_messages))
for text, label in zip(new_messages, predictions):
print(label.ljust(8), "<-", text)
You should see:
cricket <- the bowler was too fast for the batsman
food <- can we have paratha and lassi
cricket <- a crispy dosa after the match
The first two are easy wins. The model never saw those exact sentences, but it recognised the clue words.
The third one is interesting. "A crispy dosa after the match" has food words and a cricket word. The model chose cricket. Was that right? A person might say it is about food. This shows something important: a model is only as good as its examples. With just twelve training messages, it has very little to go on.
Experiment like a data scientist
- Add three more food messages that mention crispy or dosa, run again, and see if the third prediction changes.
- Write five new test messages of your own. How many does the model get right? That fraction is its accuracy.
- Add a third label, such as "school", with six example messages. The same code can handle three groups.
- Try a message with no known words at all, like "hello friend". What does the model guess, and why is that guess not very meaningful?
For parents
This is a real machine learning workflow, scaled down: gather labelled data, convert text to numbers, train a model, test on new examples. The tricky third prediction is a good moment to discuss why AI systems make mistakes and why the quality and balance of training data matters. If installing a library is new, it is worth doing together.
What you learned
- Supervised learning means training a model on examples that already have correct labels.
- Classification means sorting things into groups, like "cricket" or "food".
- Computers turn text into numbers, for example by counting words.
- A trained model can label sentences it has never seen before.
- Models make mistakes, especially with little or confusing data, so we test them on new examples.
Go further
This project hints at much more. In a full course, students learn to clean and tokenise text, measure accuracy properly with separate training and test sets, compare different models, and build bigger projects such as an inbox sorter. Want to build more? Book a free demo class and try a live machine learning lesson.