{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 1: Language Detection\n", "\n", "This notebook documents the language detection module for the RAG-based mental health support chatbot. The goal is to classify the user question language so later modules can route retrieval and response generation correctly." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Approach\n", "\n", "- Use traditional NLP as required by the project brief.\n", "- Extract character-level TF-IDF features because language identity is strongly reflected in scripts, accents, and short character patterns.\n", "- Train a Multinomial Naive Bayes classifier because it is fast, explainable, and strong for sparse text features." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys\n", "from pathlib import Path\n", "\n", "PROJECT_ROOT = Path.cwd().resolve().parent if Path.cwd().name == 'notebooks' else Path.cwd().resolve()\n", "sys.path.append(str(PROJECT_ROOT / 'src' / 'models'))\n", "\n", "from language_classifier import LanguageDetector, DATA_DIR" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Training character n-gram TF-IDF language detector...\n", "Validation accuracy: 99.35%\n", "Test accuracy: 99.40%\n", "Saved model to \\src\\models\\saved_lang_model.pkl\n", "Saved evaluation reports to \\reports\\module_1_language_detection\n" ] }, { "data": { "text/plain": [ "0.994" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "detector = LanguageDetector()\n", "results = detector.train(\n", " train_path=DATA_DIR / 'lang_train.csv',\n", " validation_path=DATA_DIR / 'lang_val.csv',\n", " test_path=DATA_DIR / 'lang_test.csv',\n", ")\n", "results['test']['accuracy']" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I feel anxious and need help. -> {'language_code': 'en', 'language_name': 'English', 'confidence': 0.9971600701429593, 'is_confident': True, 'message': None}\n", "انا اشعر بالقلق واحتاج الى المساعدة. -> {'language_code': 'ar', 'language_name': 'Arabic', 'confidence': 0.9999992535818422, 'is_confident': True, 'message': None}\n", "Je me sens stresse aujourd'hui. -> {'language_code': 'fr', 'language_name': 'French', 'confidence': 0.9966388195110697, 'is_confident': True, 'message': None}\n" ] } ], "source": [ "samples = [\n", " 'I feel anxious and need help.',\n", " 'انا اشعر بالقلق واحتاج الى المساعدة.',\n", " \"Je me sens stresse aujourd'hui.\",\n", "]\n", "\n", "for sample in samples:\n", " print(sample, '->', detector.predict_with_confidence(sample))" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }