# 05 โ Data Flow Diagram (DFD)
## Overview
This diagram traces how data flows through the BAYAN system โ from user input to final storage, through all NLP processing stages.
## Level 0 โ Context Diagram
```mermaid
graph LR
USER["๐ค User"] -->|"Arabic Text"| BAYAN["๐ต BAYAN System"]
BAYAN -->|"Corrections ยท Summaries ยท Documents"| USER
BAYAN <-->|"Auth ยท Data"| SUPABASE["๐๏ธ Supabase"]
BAYAN <-->|"Models"| HUGGINGFACE["๐ค HuggingFace"]
```
## Level 1 โ System DFD
```mermaid
graph TD
subgraph "Data Sources"
INPUT["๐ User Input Text"]
IMPORT["๐ฅ Imported File
(TXT / DOCX / PDF)"]
end
subgraph "1.0 Text Correction Pipeline"
SPELL["1.1 AraSpell
Spelling Correction"]
GRAMMAR["1.2 Grammar Check
Bayan Grammar"]
PUNCT["1.3 Punctuation
PuncAra-v1"]
MERGE["1.4 Merge Results
Offset Mapping"]
end
subgraph "2.0 Display Pipeline"
HIGHLIGHT["2.1 Error Highlighting
renderer.js"]
SIDEBAR["2.2 Suggestions Panel
ui.js"]
SCORE["2.3 Writing Score
Calculation"]
STATS["2.4 Editor Stats
Words ยท Chars ยท Sentences"]
end
subgraph "3.0 Summarization Pipeline"
SUMM_IN["3.1 Extract Clean Text"]
SUMM_MODEL["3.2 MBart Summarization"]
SUMM_OUT["3.3 Summary Display"]
end
subgraph "4.0 AutoComplete Pipeline"
AC_IN["4.1 Cursor Context"]
AC_MODEL["4.2 AutoComplete Model"]
AC_OUT["4.3 Dropdown Suggestions"]
end
subgraph "5.0 Storage"
LOCAL["5.1 localStorage
Draft ยท Dismissed Words"]
DOCUMENTS_DB["5.2 Supabase: documents"]
SUMMARIES_DB["5.3 Supabase: summaries"]
SETTINGS_DB["5.4 Supabase: settings"]
PROFILES_DB["5.5 Supabase: profiles"]
end
subgraph "6.0 Export"
EXPORT_TXT["6.1 Export TXT"]
EXPORT_DOCX["6.2 Export DOCX"]
EXPORT_PDF["6.3 Export PDF"]
end
INPUT --> SPELL
IMPORT --> SPELL
SPELL -->|"Corrected tokens"| GRAMMAR
GRAMMAR -->|"Grammar-checked text"| PUNCT
PUNCT -->|"Punctuated text"| MERGE
MERGE -->|"All suggestions + offsets"| HIGHLIGHT
MERGE --> SIDEBAR
MERGE --> SCORE
INPUT --> STATS
INPUT --> SUMM_IN
SUMM_IN --> SUMM_MODEL
SUMM_MODEL --> SUMM_OUT
SUMM_OUT --> SUMMARIES_DB
INPUT --> AC_IN
AC_IN --> AC_MODEL
AC_MODEL --> AC_OUT
INPUT --> LOCAL
LOCAL -->|"Sync"| DOCUMENTS_DB
INPUT --> EXPORT_TXT
INPUT --> EXPORT_DOCX
INPUT --> EXPORT_PDF
style SPELL fill:#EF4444,color:#fff
style GRAMMAR fill:#F59E0B,color:#000
style PUNCT fill:#3B82F6,color:#fff
style SUMM_MODEL fill:#8B5CF6,color:#fff
style AC_MODEL fill:#10B981,color:#fff
```
## Data Stores Summary
| Store | Type | Data | Access Pattern |
|-------|------|------|----------------|
| `localStorage` | Client-side | Draft HTML, dismissed words, word goal, theme | Read/write on every input |
| `documents` | Supabase | id, user_id, title, content, timestamps | CRUD per user (RLS) |
| `summaries` | Supabase | id, document_id, user_id, original, summary | Append-mostly |
| `settings` | Supabase | id, user_id, preferences JSON | Read on login, write on change |
| `profiles` | Supabase | id, display_name, avatar_url, auth_provider | Auto-created on signup |
## Data Transformation Chain
```
Raw Arabic Text
โ
โโโโ AraSpell โโโ Corrected words (with alternatives)
โ โ
โ โผ
โ Grammar Engine โโโ Grammar errors (with suggestions)
โ โ
โ โผ
โ Punctuation โโโ Punctuation insertions
โ โ
โ โผ
โ Offset Mapper โโโ Maps corrected offsets โ original offsets
โ โ
โ โผ
โ Unified Suggestions Array
โ โ
โ โโโโ Error Spans (renderer.js)
โ โโโโ Sidebar Cards (ui.js)
โ โโโโ Score Calculation
โ
โโโโ AutoComplete Model โโโ Top-K word suggestions
โ
โโโโ MBart Summarizer โโโ Compressed summary text
```