Skip to content

Repository files navigation

Enhanced Predictive Keyboard System

A sophisticated predictive text system that combines rule-based NLP techniques with deep learning models to provide accurate, context-aware text predictions in real-time.

πŸš€ Features

Core Functionality

  • Hybrid Prediction System: Combines ML models with rule-based fallbacks for robust predictions
  • Real-time Predictions: Instant suggestions as you type with configurable debouncing
  • Offline Support: Complete caching system for offline operation
  • Multiple Models: Support for both formal (Wikipedia-trained) and conversational (chat-trained) models
  • Smart Fallbacks: Automatically switches to rule-based predictions when ML confidence is low

Technical Features

  • Enhanced BiLSTM Architecture: Bidirectional LSTM with attention mechanism for better context understanding
  • N-gram Models: Unigram, bigram, and trigram models for rule-based predictions
  • POS Tagging: Part-of-speech aware predictions for grammatically correct suggestions
  • Intelligent Caching: Multi-level caching for instant responses
  • Model Persistence: All models and data are cached locally for offline use

πŸ“‹ Prerequisites

  • Python 3.8 or higher
  • 4GB+ RAM recommended
  • 2GB+ free disk space for models and datasets

πŸ› οΈ Installation

  1. Clone or download the project files

  2. Create a virtual environment (recommended):

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Download NLTK data (first time only):
python -c "import nltk; nltk.download('punkt'); nltk.download('averaged_perceptron_tagger'); nltk.download('brown'); nltk.download('stopwords')"

πŸš€ Quick Start

Option 1: Run with Pre-trained Models (Fastest)

  1. Start the server:
python app.py
  1. Open your browser and navigate to:
http://localhost:5000
  1. The system will automatically initialize with rule-based predictions. You can start typing immediately!

Option 2: Train Custom Models

  1. Train a formal (Wikipedia) model:
python model_train.py --mode wiki --limit 20000 --epochs 10
  1. Train a conversational (chat) model:
python model_train.py --mode chat --limit 20000 --epochs 10
  1. Start the server with trained models:
python app.py

🎯 Usage Guide

Web Interface

  1. Typing Area: Start typing in the text area to see real-time predictions
  2. Suggestions: Click on any suggestion chip to apply it to your text
  3. Modes:
    • Hybrid: Combines ML and rule-based (recommended)
    • ML Only: Uses only the neural network model
    • Rule Only: Uses only linguistic rules

API Endpoints

Get Predictions

POST /predict
Content-Type: application/json

{
    "text": "I want to",
    "mode": "hybrid",
    "ml_mode": "wiki",
    "k": 5
}

Train Model

POST /train
Content-Type: application/json

{
    "mode": "wiki",
    "limit": 20000,
    "epochs": 10,
    "batch_size": 128
}

Evaluate Model

POST /evaluate
Content-Type: application/json

{
    "mode": "wiki",
    "sample_size": 1000
}

πŸ—οΈ Architecture

System Components

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Web Interface (HTML)           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Flask Application (app.py)       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β€’ Request handling                      β”‚
β”‚  β€’ Prediction service                    β”‚
β”‚  β€’ Model management                      β”‚
β”‚  β€’ Caching layer                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Rule-Based     β”‚  β”‚    ML Models      β”‚
β”‚   Predictor     β”‚  β”‚   (BiLSTM)        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ N-grams       β”‚  β”‚ β€’ Wikipedia       β”‚
β”‚ β€’ POS tagging   β”‚  β”‚ β€’ Chat/Dialog     β”‚
β”‚ β€’ Patterns      β”‚  β”‚ β€’ Attention       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Model Architecture (BiLSTM)

Input Text β†’ Tokenization β†’ Embedding (256d)
                ↓
         Bidirectional LSTM (256 units)
                ↓
         Bidirectional LSTM (128 units)
                ↓
           Dense Layer (512)
                ↓
           Dropout (0.4)
                ↓
           Dense Layer (256)
                ↓
           Dropout (0.3)
                ↓
         Output (Vocabulary Size)

Rule-Based System

  1. Phrase Completions: Common phrase patterns (e.g., "I want to" β†’ ["go", "eat", "see"])
  2. N-gram Models: Statistical predictions based on word sequences
  3. POS Patterns: Grammar-aware predictions based on part-of-speech tags
  4. Word Associations: Context-aware word relationships

πŸ“Š Performance Optimization

Caching Strategy

  • Model Caching: All trained models are saved to disk
  • Prediction Caching: Recent predictions are cached in memory
  • Dataset Caching: Downloaded datasets are cached locally
  • Tokenizer Caching: Preprocessed tokenizers are persisted

Response Time Optimization

  • Debouncing: Configurable delay (200ms default) to reduce API calls
  • Batch Processing: Efficient batch inference for multiple predictions
  • Memory Management: Automatic cache size limits and cleanup

πŸ”§ Configuration

Model Parameters

Edit in model_train.py:

max_vocab = 20000      # Maximum vocabulary size
max_seq_len = 30       # Maximum sequence length
embedding_dim = 256    # Embedding dimensions
lstm_units = 256       # LSTM unit count

Server Configuration

Edit in app.py:

CACHE_MAX_SIZE = 1000  # Maximum prediction cache entries
debug = True           # Debug mode
host = "127.0.0.1"     # Server host
port = 5000            # Server port

Training Parameters

# Customize training
python model_train.py \
    --mode wiki \
    --limit 50000 \      # Number of sentences
    --epochs 15 \        # Training epochs
    --batch-size 256     # Batch size

πŸ“ File Structure

project/
β”œβ”€β”€ app.py                          # Flask application
β”œβ”€β”€ model_train.py                  # ML model training
β”œβ”€β”€ rule_based_predictive_keyboard.py  # Rule-based predictor
β”œβ”€β”€ index.html                      # Web interface
β”œβ”€β”€ requirements.txt                # Python dependencies
β”œβ”€β”€ README.md                       # Documentation
β”œβ”€β”€ model_cache/                    # Cached models (created automatically)
β”‚   β”œβ”€β”€ wiki_bilstm_final.h5      # Wikipedia model
β”‚   β”œβ”€β”€ chat_bilstm_final.h5      # Chat model
β”‚   β”œβ”€β”€ tokenizer_wiki.pkl        # Wikipedia tokenizer
β”‚   β”œβ”€β”€ tokenizer_chat.pkl        # Chat tokenizer
β”‚   β”œβ”€β”€ rule_based_model.pkl      # Rule-based model
β”‚   └── sentences_*.pkl           # Cached datasets
└── templates/                      # HTML templates (if using)
    └── index.html

πŸ› Troubleshooting

Common Issues

  1. "Model not found" error:

    • Solution: Train the model first using python model_train.py
  2. Slow predictions:

    • Solution: Reduce topK value or increase debounce delay
  3. Out of memory:

    • Solution: Reduce limit parameter when training or use smaller batch size
  4. Dataset download fails:

    • Solution: Check internet connection; cached data will be used if available
  5. NLTK data not found:

    • Solution: Run the NLTK download command in the installation section

Performance Tips

  1. For faster startup: Use pre-trained models from cache
  2. For better accuracy: Train with more data (increase limit)
  3. For faster predictions: Use rule-based mode or reduce suggestion count
  4. For offline use: Train models once and they'll be cached permanently

πŸ“ˆ Model Evaluation Metrics

After training, you can evaluate your models:

  • Top-1 Accuracy: Percentage of correct next-word predictions
  • Top-5 Accuracy: Percentage where correct word is in top 5 predictions
  • Perplexity: Lower is better (measures prediction uncertainty)

Expected performance:

  • Rule-based: 15-25% Top-1 accuracy
  • ML (trained on 20k sentences): 35-45% Top-1 accuracy
  • ML (trained on 50k+ sentences): 45-55% Top-1 accuracy

πŸš€ Advanced Usage

Custom Datasets

To use your own dataset:

  1. Add your dataset loader in model_train.py:
def load_custom_dataset():
    # Your code here
    return sentences
  1. Train with custom data:
sentences = load_custom_dataset()
trainer = PredictiveKeyboardTrainer()
trainer.train(sentences)

API Integration

The system can be integrated into other applications:

import requests

# Get predictions
response = requests.post('http://localhost:5000/predict', 
    json={'text': 'Hello', 'mode': 'hybrid', 'k': 5})
predictions = response.json()['suggestions']

πŸ“„ License

This project is provided as-is for educational and development purposes.

🀝 Contributing

Improvements are welcome! Consider:

  • Adding more sophisticated language models (GPT, BERT)
  • Implementing user personalization
  • Adding multi-language support
  • Improving the UI/UX
  • Adding voice input support

⚑ Performance Benchmarks

Mode Response Time Accuracy (Top-1) Accuracy (Top-5)
Rule-based <10ms 20% 35%
ML (cached) 15-30ms 40% 65%
Hybrid 20-40ms 45% 70%

Benchmarks on Intel i5, 8GB RAM with 20k training sentences


Ready to start? Run python app.py and open http://localhost:5000 πŸš€

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages