This project demonstrates the development of a custom BERT-based model for classifying legal case texts into predefined outcome categories. The dataset contains legal text information, and the model utilizes transfer learning with BERT for robust feature extraction and classification.
- Project Overview
- Dataset
- Model Architecture
- Preprocessing Steps
- Training and Evaluation
- Requirements
- Usage
- Results
- Future Improvements
The goal is to classify legal cases based on textual data (titles and case descriptions). The model leverages the pretrained BERT (bert-base-uncased) for extracting meaningful embeddings from the text. Two text inputs are utilized:
- Case Title: A short summary or title of the case.
- Case Text: A detailed description of the case.
The model processes both fields separately, combines their outputs, and predicts the outcome of the case.
-
The dataset (
legal_text_classification.csv) includes the following columns:case_title: The title of the legal case.case_text: The detailed text describing the case.case_outcome: The outcome label for the case.
-
The dataset is split into 80% training and 20% testing data.
A custom BERT-based model (CustomBertModel) is designed:
- Input Layers: Handles tokenized and padded sequences for both
case_titleandcase_text. - Dual BERT Modules: Processes both inputs independently using pretrained BERT models.
- Fusion Layer: Combines embeddings from both inputs using learnable weights
w1andw2. - Classifier: A fully connected softmax layer predicts the outcome category.
The combined representation is calculated as: [ \text{combined_output} = w1 \times \text{title_embedding} + w2 \times \text{text_embedding} ]
-
Text Cleaning:
- Lowercasing
- Punctuation removal
- Lemmatization and stemming
-
Tokenization:
- Both
case_titleandcase_textare tokenized using BERT's tokenizer. - Tokenized inputs are padded and truncated to a maximum length of 128.
- Both
-
Label Encoding:
- Target labels (
case_outcome) are encoded into integers for classification.
- Target labels (
-
Compilation:
- Optimizer:
Adamwith a learning rate of2e-5. - Loss: Sparse Categorical Crossentropy.
- Metrics: Accuracy.
- Optimizer:
-
Fine-Tuning:
- All layers of BERT are trainable, except the
poolerlayer, which is frozen.
- All layers of BERT are trainable, except the
-
Callbacks:
- Early stopping is used to halt training when validation loss stops improving.
-
Evaluation:
- Confusion Matrix: Visualizes true vs. predicted labels.
- Classification Report: Displays precision, recall, F1-score, and support for each class.
Install the required dependencies:
pip install tensorflow transformers pandas scikit-learn nltk matplotlib