Rule-Based FAQ Chatbot
Build a chatbot in Scala that answers frequently asked questions in the terminal, without any AI model: it compares what the user typed with a list of known questions and answers, scores how well each one matches, and replies with the best answer, or admits it doesn't know when nothing matches well enough. It's the kind of simple, predictable bot that powers many help pages. You'll make matching smarter step by step: ignoring case, punctuation and common words, matching word forms, and tolerating typos. You'll learn text processing in Scala, collections and higher-order functions, scoring and thresholds, pattern matching, reading JSON, and testing with ScalaTest.
- Estimate
- ~9h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
sbt · ScalaTest
Project roadmap
- 01
Load the knowledge base
~1.5hCreate an sbt project with ScalaTest and a JSON library (such as circe or upickle). Write faq.json with about 20 entries for a real topic (for example a gym or an online shop), each with an id, several ways of asking the question, keywords and the answer. Load it into a case class FaqEntry, and stop with a clear error when the file is missing or invalid. Done when: the FAQ loads and the number of entries is printed.
- 02
Normalize text
~1.5hWrite a normalize function that lowercases text, removes punctuation, splits it into words, drops common words (the, a, is, how and so on) and reduces simple plurals (orders to order). Apply it to both the user's input and the FAQ questions. Done when: unit tests show that How do I RETURN my orders? becomes return order.
- 03
Score each entry
~2hScore each FAQ entry by comparing the input's words with the entry's words, for example with Jaccard similarity (shared words divided by all distinct words) across its question variants, plus a bonus for keywords. Treat words within an edit distance of 1 as matching (implement Levenshtein distance), so small typos still match. Done when: typical questions and questions with one typo score their correct entry highest.
- 04
Choose the answer
~1hPick the highest-scoring entry if its score is above a threshold (for example 0.35). When the top two scores are very close, ask which one the user meant. Otherwise give a fallback answer suggesting a few topics it can help with. Done when: clear questions get answers, ambiguous ones get a choice, and unrelated ones get the fallback.
- 05
Build the conversation loop
~1.5hWrite a loop that reads questions, prints answers, and handles commands: help (list the topics), debug (toggle showing the top 3 scores for each question, which helps tune the threshold) and quit. Log questions that got the fallback to a file, so you know which FAQ entries to add. Done when: a full conversation works, and unanswered questions are logged.
- 06
Test the matching
~1.5hWrite ScalaTest tests: normalization, Levenshtein distance, and a table of 20 or more sample questions with the entry each should match (including typos and different wording), plus unrelated questions that must get the fallback. Done when: sbt test passes.
Resources
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack