Natural Language Processing
Senior 6 · ICT
Natural Language Processing
Lesson 2 of 5
22 min
Every September the office of a large secondary school in Musanze answers the same questions hundreds of times: when do classes start, what is the fee for the term, which documents does a new student bring, where is the S4 dormitory. A chatbot on the school's website or WhatsApp number could answer most of them at any hour, and pass the unusual questions to a person. In the next three lessons you will build one in Python; in the last lesson you will build another with Google Dialogflow. First you need the vocabulary of chatbots, and a Python environment that works.
Definition
Chatbot: a computer program that holds a conversation with a person in natural language, by text or by voice.
Utterance: one thing the user says or types. Intent: what the user wants to achieve with an utterance. Example: "What time does school start?" and "When do lessons begin?" are two utterances with the same intent, start_time.
Entity: a piece of information inside an utterance that the bot needs, such as a date, a class or a place. Example: in "Fees for S5?", the class S5 is an entity.
Fallback: the bot's reply when it cannot work out the intent.
Not every chatbot has every part. The four main kinds differ in how they understand and how they reply:
| Kind | How it works | Strengths | Weaknesses |
|---|---|---|---|
| Rule-based (menu or keyword) | Fixed menus, or keywords and patterns mapped to fixed replies | Predictable, cheap, easy to check | Fails on any wording the rules did not foresee |
| Retrieval-based | Compares the message with stored example conversations and returns the reply of the closest match | Learns from examples; replies are pre-approved | Can pick a wrong but similar-looking reply |
| Intent-based NLU platform (Dialogflow) | A trained model classifies the intent and extracts entities; the designer writes responses | Handles varied wording; structured | Needs good training phrases and a cloud service |
| Generative (large language model) | A language model writes a new reply each time | Very flexible and fluent | Can state false things confidently; harder to control and check |
Worked example 1: annotate an utterance
A parent types: "Can I pay the S4 fees for Mugisha by mobile money before 15 January?" Identify the intent and the entities.
pay_fees (the parent wants to pay; a related intent might be payment_methods).Worked example 2: choose the kind of chatbot
The chatbots in this unit are written in Python 3. You need three things: Python itself, a virtual environment for the project, and an editor.
python.org/downloads. On Windows, tick Add python.exe to PATH on the first screen of the installer. Check it with python --version (Windows) or python3 --version (macOS and Linux)..venv interpreter when asked.On Windows (Command Prompt):
C:\Users\Keza> mkdir schoolbot
C:\Users\Keza> cd schoolbot
C:\Users\Keza\schoolbot> python --version
Python 3.12.6
C:\Users\Keza\schoolbot> python -m venv .venv
C:\Users\Keza\schoolbot> .venv\Scripts\activate
(.venv) C:\Users\Keza\schoolbot> python -m pip install --upgrade pip
(.venv) C:\Users\Keza\schoolbot> pip install chatterbot
(.venv) C:\Users\Keza\schoolbot> pip freeze > requirements.txtOn macOS or Linux (Terminal):
$ mkdir schoolbot && cd schoolbot
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -m pip install --upgrade pip
(.venv) $ pip install chatterbot
(.venv) $ deactivate # leave the environment when finishedThe (.venv) at the start of the prompt shows that the environment is active: python and pip now refer to the project's own copies. requirements.txt lists the exact packages, so a classmate can rebuild the same environment with pip install -r requirements.txt.
Save this script as check_env.py and run it to confirm everything is in place:
import sys
print("Python", sys.version.split()[0])
print("Running from:", sys.executable)
print("Inside a virtual environment:", sys.prefix != sys.base_prefix)
Example output, inside an active environment:
Python 3.14.6
Running from: /home/keza/schoolbot/.venv/bin/python
Inside a virtual environment: TrueDefinition
Virtual environment: an isolated Python installation for one project, with its own pip and its own installed packages, created with python -m venv.
Package (library): reusable code that you install with pip, Python's package installer, from the Python Package Index (PyPI). Example: pip install chatterbot.
Corey Schafer: Python Tutorial: VENV (Windows), How to Use Virtual Environments with the Built-In venv Module. Watch how the environment is created, activated and checked with pip list; the same commands appear in the steps above.
Warning
The most common setup errors, and their causes
'python' is not recognized…: Python is not on the PATH. Re-run the installer and tick Add python.exe to PATH, or use the py launcher on Windows.ModuleNotFoundError: No module named 'chatterbot': the package was installed into a different Python. Activate the environment, then install again; pip --version shows which Python pip belongs to.Set-ExecutionPolicy -Scope CurrentUser RemoteSigned.Tip
One project, one environment. Create .venv inside each project folder, never install project packages into the system Python, and keep requirements.txt up to date. Then "it works on my computer" becomes "it works on every computer".
schoolbot, a virtual environment inside it, and activate it.check_env.py. It must print Inside a virtual environment: True..venv interpreter.Check: check_env.py reports a virtual environment, and your list has ten utterances grouped into at least five intents.
Practice
exam_timetable, and one entity that might appear in them..venv on Windows, and on Linux.ModuleNotFoundError when running the program in the editor. Give two likely causes and the fix for each.requirements.txt.python -m venv .venv then .venv\Scripts\activate. Linux: python3 -m venv .venv then source .venv/bin/activate..venv and reinstall. (b) The editor is using a different interpreter: select the .venv interpreter in the editor.Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.