Natural Language Processing/Swahili Chatbot with RegEx
17 / 67

01/2024Natural Language Processing

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 X or Jina langu ni X (my name is X) returns the respectful greeting Shikamoo, X. Umeshindaje?
  • State of mind. nimefurahi / sijahuzunika and kin, the nime/sija prefix plus a mood stem, get reflected back as a question.
  • Self-description. Mimi ni … (I am …) becomes Mbona wewe …? (why are you …?).
  • Family. A mama/baba/kaka/dada noun with the -ngu (my) suffix draws out Niambie mengine kumhusu …ko (tell me more about your …).
  • Intent. Modal stems Nataka/Sitaki/Naweza/Siwezi/Nahitaji and the obligation Lazima 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.

Algorithm:Respond(u,R)\textsc{Respond}(u, R) — first-match pattern reply
  1. 1
    input: a user utterance uu, an ordered list RR of (pattern pp, templates TT) rules
  2. 2
    xx \gets normalize and lowercase uu
  3. 3
    for each (p,T)(p, T) in RR, in order, do
  4. 4
    mm \gets match pp against xx
  5. 5
    if mm succeeds then
  6. 6
    tt \gets choose a template from TT
  7. 7
    return tt with capture groups of mm substituted in
  8. 8
    return 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

  1. Project repository
  2. Reference notes: Dialogue and Chatbots

╌╌ END ╌╌