File size: 9,253 Bytes
51e1331
 
 
 
 
 
043f08c
51e1331
 
 
 
 
3d9ba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
---
title: Bilingual Text Summarization NLP Suite
emoji: ๐Ÿ“–
colorFrom: indigo
colorTo: blue
sdk: gradio
sdk_version: 5.9.1
app_file: app.py
pinned: false
license: mit
---

# ๐Ÿ“š Bilingual Arabicโ€“English NLP System for Automatic Text Summarization
### ู†ุธุงู… ุงู„ู…ุนุงู„ุฌุฉ ุงู„ู„ุบูˆูŠุฉ ุงู„ุชู„ู‚ุงุฆูŠุฉ ู„ู„ุชู„ุฎูŠุต ุงู„ุงุณุชุฎุฑุงุฌูŠ ูˆุงู„ุชุฌุฑูŠุฏูŠ ู„ู„ู†ุตูˆุต ุงู„ุนุฑุจูŠุฉ ูˆุงู„ุฅู†ุฌู„ูŠุฒูŠุฉ

---

## ๐ŸŒŸ Executive Overview
This repository contains an end-to-end **Bilingual (Arabic & English) Natural Language Processing (NLP) System for Automatic Text Summarization**, implemented completely **from scratch**.

It addresses the fundamental linguistic, structural, and morphological differences between Arabic (a morphologically rich, non-concatenative language) and English (an analytic language with fixed syntax), offering both **Extractive** and **Abstractive** summarization paradigms alongside a rigorous quantitative evaluation suite (**ROUGE**, **BLEU**, **TTR**, and **Compression Metrics**) and a modern interactive Web Dashboard.

---

## ๐Ÿ›๏ธ System Architecture

```mermaid
flowchart TD
    A[User Input: Text / PDF / DOCX / TXT] --> B[Language Detection & Character Routing]
    
    B -->|Arabic| C1[Arabic Preprocessing Engine: Tashkeel, Normalization, ISRI Stemmer]
    B -->|English| C2[English Preprocessing Engine: Contractions, Lemmatizer, Filter]
    
    C1 --> D{Summarization Mode}
    C2 --> D
    
    D -->|Extractive| E1[Sentence Segmentation & TF-IDF Vectorizer]
    E1 --> E2[Graph-based TextRank / LSA SVD / Hybrid Centroid]
    E2 --> E3[Extractive Chronological Summary]
    
    D -->|Abstractive| F1[Bilingual Vocabulary & Dynamic Pad Batching]
    F1 --> F2[PyTorch Seq2Seq with Bahdanau / Luong Attention]
    F2 --> F3[Beam Search / Greedy Decoding]
    F3 --> F4[Abstractive Paraphrased Summary]
    
    E3 --> G[Evaluation Suite: ROUGE-1/2/L, BLEU-1..4, Diversity & Ratio]
    F4 --> G
    
    G --> H[Streamlit Interactive Web GUI & CLI]
```

---

## ๐Ÿ“ Repository Structure

```text
sumary project/
โ”‚
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ sample_arabic.txt        # Comprehensive Arabic sample text
โ”‚   โ”œโ”€โ”€ sample_english.txt       # Comprehensive English sample text
โ”‚   โ”œโ”€โ”€ arabic_stopwords.txt     # Curated Arabic stopword lexicon
โ”‚   โ”œโ”€โ”€ english_stopwords.txt    # Standard English stopword lexicon
โ”‚   โ””โ”€โ”€ datasets/                # Generated bilingual parallel corpora (.json)
โ”‚
โ”œโ”€โ”€ nlp_core/                    # Fundamental NLP and preprocessing engine
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ language_detector.py     # Unicode & n-gram language classifier (AR/EN)
โ”‚   โ”œโ”€โ”€ arabic_preprocessor.py   # Tashkeel removal, Alef/Yaa/Taa normalization, Light stemmer
โ”‚   โ”œโ”€โ”€ english_preprocessor.py  # Contraction expansion, lowercasing, stemmer
โ”‚   โ”œโ”€โ”€ tokenizer.py             # Arabic & English sentence segmenter and word tokenizer
โ”‚   โ””โ”€โ”€ vocabulary.py            # Vocab manager, token encoder/decoder, special tokens
โ”‚
โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ extractive/              # Extractive summarization algorithms (From Scratch)
โ”‚   โ”‚   โ”œโ”€โ”€ tfidf.py             # Custom Sublinear TF-IDF & Cosine Similarity
โ”‚   โ”‚   โ”œโ”€โ”€ textrank.py          # Graph PageRank power iteration summarizer
โ”‚   โ”‚   โ”œโ”€โ”€ lsa.py               # Latent Semantic Analysis with SVD
โ”‚   โ”‚   โ””โ”€โ”€ hybrid_scorer.py     # Multi-feature (TextRank + Centroid + Position + Length)
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ abstractive/             # Deep learning Sequence-to-Sequence (PyTorch From Scratch)
โ”‚       โ”œโ”€โ”€ attention.py         # Bahdanau (Additive) & Luong Attention mechanisms
โ”‚       โ”œโ”€โ”€ encoder.py           # Bidirectional GRU/LSTM Encoder
โ”‚       โ”œโ”€โ”€ decoder.py           # Attention-guided GRU Decoder
โ”‚       โ”œโ”€โ”€ seq2seq_model.py     # Full Seq2Seq model with Greedy & Beam Search
โ”‚       โ””โ”€โ”€ transformer_wrap.py  # Modular wrapper for Hugging Face Pretrained Models
โ”‚
โ”œโ”€โ”€ training/                    # ML/DL Training and Dataset pipeline
โ”‚   โ”œโ”€โ”€ sample_data_gen.py       # Bilingual parallel dataset synthesizer
โ”‚   โ”œโ”€โ”€ dataset.py               # PyTorch Dataset and dynamic pad collator
โ”‚   โ””โ”€โ”€ train.py                 # Training loop with teacher forcing, validation, checkpointing
โ”‚
โ”œโ”€โ”€ evaluation/                  # Quantitative benchmark suite (From Scratch)
โ”‚   โ”œโ”€โ”€ rouge.py                 # ROUGE-1, ROUGE-2, and ROUGE-L (Precision, Recall, F1)
โ”‚   โ”œโ”€โ”€ bleu.py                  # BLEU-1 to BLEU-4 and Cumulative BLEU with Brevity Penalty
โ”‚   โ””โ”€โ”€ metrics_manager.py       # Unified evaluator (Compression, Diversity, Overlap)
โ”‚
โ”œโ”€โ”€ checkpoints/                 # Saved PyTorch model weights (.pt)
โ”‚   โ”œโ”€โ”€ seq2seq_arabic.pt
โ”‚   โ””โ”€โ”€ seq2seq_english.pt
โ”‚
โ”œโ”€โ”€ tests/                       # Complete automated unit test suite
โ”‚   โ”œโ”€โ”€ test_preprocessing.py
โ”‚   โ”œโ”€โ”€ test_extractive.py
โ”‚   โ”œโ”€โ”€ test_abstractive.py
โ”‚   โ””โ”€โ”€ test_evaluation.py
โ”‚
โ”œโ”€โ”€ app.py                       # Modern Interactive Streamlit Web GUI
โ”œโ”€โ”€ main_cli.py                  # Versatile Command-Line Interface
โ”œโ”€โ”€ requirements.txt             # Python dependencies
โ””โ”€โ”€ README.md                    # Documentation & System Manual
```

---

## ๐Ÿš€ Getting Started

### 1. Installation & Environment Setup

```bash
# Clone the repository and navigate to folder
cd "sumary project"

# Install dependencies
pip install -r requirements.txt
```

### 2. Run Automated Unit Tests

```bash
python -m unittest discover tests
```
*Executes all 14 unit tests validating preprocessing, TF-IDF, TextRank, LSA, Seq2Seq Attention forward pass, ROUGE, and BLEU.*

---

## ๐Ÿ’ป Usage

### 1. Interactive Web Application (GUI)

Launch the modern glassmorphic dashboard:
```bash
streamlit run app.py
```

**Features in the Web App:**
- ๐Ÿ“„ Upload `.txt`, `.pdf`, or `.docx` documents.
- ๐ŸŒ Automatic language detection for Arabic and English.
- ๐ŸŽ›๏ธ Toggle between **Extractive** (TextRank, LSA, Hybrid) and **Abstractive** (Seq2Seq Attention, Transformers).
- ๐Ÿ” Visual sentence highlighting showing exact extracted sentences in the source text.
- ๐Ÿ“ˆ Live quantitative ROUGE and BLEU calculation against reference summaries.
- ๐Ÿ‹๏ธ **Training Studio tab** to train and fine-tune Seq2Seq models directly from the GUI.
- ๐Ÿ’พ One-click export to download generated summaries.

---

### 2. Command-Line Interface (CLI)

#### A. Arabic Extractive Summarization (TextRank)
```bash
python main_cli.py --file data/sample_arabic.txt --mode extractive --method textrank --sentences 2
```

#### B. English Extractive Summarization (Hybrid Multi-Feature)
```bash
python main_cli.py --file data/sample_english.txt --mode extractive --method hybrid --sentences 3
```

#### C. Arabic Abstractive Summarization (Neural Seq2Seq with Attention)
```bash
python main_cli.py --text "ุชุนุฏ ู…ุนุงู„ุฌุฉ ุงู„ู„ุบุงุช ุงู„ุทุจูŠุนูŠุฉ ูุฑุนุง ุฌูˆู‡ุฑูŠุง ู…ู† ุงู„ุฐูƒุงุก ุงู„ุงุตุทู†ุงุนูŠ ู„ูู‡ู… ูˆุชูˆู„ูŠุฏ ุงู„ู†ุตูˆุต ุงู„ุจุดุฑูŠุฉ." --mode abstractive --method seq2seq --beam 3
```

#### D. Summarize and Evaluate with ROUGE & BLEU Benchmarks
```bash
python main_cli.py --file data/sample_english.txt --mode extractive --method textrank --ref "Natural language processing enables computers to comprehend text and summarize documents efficiently."
```

#### E. JSON Output for API / Automated Pipelines
```bash
python main_cli.py --file data/sample_arabic.txt --mode extractive --json
```

---

## ๐Ÿง  Model Training

To retrain the neural Seq2Seq models on custom or augmented datasets:

```bash
# Train both Arabic and English models for 10 epochs
python training/train.py --lang all --epochs 10 --batch_size 8 --lr 0.002

# Train only Arabic model
python training/train.py --lang ar --epochs 15

# Train only English model
python training/train.py --lang en --epochs 15
```

Checkpoints are automatically saved to `checkpoints/seq2seq_arabic.pt` and `checkpoints/seq2seq_english.pt`.

---

## ๐Ÿ“Š Evaluation & Mathematical Formulations

### 1. ROUGE-N (Recall, Precision, F1)
$$\text{ROUGE-N Recall} = \frac{\sum_{S \in \{\text{Reference}\}} \sum_{\text{gram}_n \in S} \text{Count}_{\text{match}}(\text{gram}_n)}{\sum_{S \in \{\text{Reference}\}} \sum_{\text{gram}_n \in S} \text{Count}(\text{gram}_n)}$$

### 2. BLEU Score with Brevity Penalty (BP)
$$\text{BLEU} = \text{BP} \cdot \exp\left(\sum_{n=1}^{N} w_n \ln p_n\right), \quad \text{where } \text{BP} = \begin{cases} 1 & \text{if } c > r \\ e^{(1 - r/c)} & \text{if } c \le r \end{cases}$$

### 3. PageRank Centrality (TextRank)
$$WS(V_i) = (1 - d) + d \sum_{V_j \in \text{In}(V_i)} \frac{w_{ji}}{\sum_{V_k \in \text{Out}(V_j)} w_{jk}} WS(V_j)$$

### 4. Bahdanau Additive Attention
$$e_{ij} = v_a^T \tanh(W_a s_{i-1} + U_a h_j), \quad \alpha_{ij} = \frac{\exp(e_{ij})}{\sum_k \exp(e_{ik})}, \quad c_i = \sum_j \alpha_{ij} h_j$$

---

## ๐Ÿ“œ License
This project is licensed under the MIT License - open for educational, academic research, and commercial use.