Swahili Chatbot with RegEx
An ELIZA-style Swahili chatbot: ranked regular-expression patterns match the input, and reassembly rules turn the match into a reply.
╌╌╌╌
A rule-based chatbot for Swahili, built in the style of
ELIZA. It holds a conversation
with no learned model at all: the single eliza function runs the input
through a fixed ladder of eight
regular expressions,
each with capture groups, and the first one to match builds the reply.
An input that matches nothing falls through to a neutral prompt.
The patterns are tried top to bottom in an if/elif chain, so their
written order is their priority. Each one
targets a family of Swahili sentences:
- Naming.
Naitwa XorJina langu ni X(my name is X) returns the respectful greetingShikamoo, X. Umeshindaje? - State of mind.
nimefurahi/sijahuzunikaand kin, thenime/sijaprefix plus a mood stem, get reflected back as a question. - Self-description.
Mimi ni …(I am …) becomesMbona wewe …?(why are you …?). - Family. A
mama/baba/kaka/dadanoun with the-ngu(my) suffix draws outNiambie mengine kumhusu …ko(tell me more about your …). - Intent. Modal stems
Nataka/Sitaki/Naweza/Siwezi/Nahitajiand the obligationLazima ni…are echoed with the person flipped.
The remaining rules catch thoughts (Nadhani/Natumai), ask for an
example on any generic verb, and deflect a short list of insults; the
default reply Niambie mengine… (tell me more) closes the ladder.
- 1input: a user utterance , an ordered list of (pattern , templates ) rules
- 2normalize and lowercase
- 3for each in , in order, do
- 4match against
- 5if succeeds then
- 6choose a template from
- 7return with capture groups of substituted in
- 8return the default fallback reply
Reflection keeps the replies coherent. When a captured group is spliced
back into a reply, its person markers are rewritten so the bot answers
from its own point of view. The possessive -angu (my) is swapped to
-ako (your), and the subject prefixes on verbs flip — a first-person
nime/nataka returns as second-person ume/unataka. Without this
step, echoing the user's words reads as nonsense; with it, a statement
about "me" comes back as a question about "you", so the reply stays on
topic without any parsing of meaning.
You can read the full report.
References
- Project repository
- Reference notes: Dialogue and Chatbots
╌╌ END ╌╌