Skip to content

Technical Architecture

PyArud (پَايْعَرُوضْ) is designed as a modular, immutable, zero-dependency pipeline separating pure Arabic normalization and 1:1 phonetic alignment from formal Farahidian grammar scansion, custom meter compilation, classical Qafiyah analysis, and CLI/Markdown reporting.

flowchart TD
    A["Raw Arabic Text"] --> B["pyarud.core.arabic<br/>Normalization & Tashkeel Validation"]
    B --> C["pyarud.core.phonetics<br/>ArudiConverter & 1:1 ArudiAlignment"]
    C --> D["pyarud.meters.registry<br/>Persistent Immutable MeterRegistry"]
    D --> E["pyarud.meters.engine<br/>DeterministicProsodyEngine"]
    E -->|"Exact Match"| F["Formal DFS / NFA-DP Grammar<br/>+ Farahidian Disambiguation"]
    E -->|"Broken Verse"| G["Slot-Level Levenshtein<br/>Viterbi DP Alignment"]
    C --> H["pyarud.qafiyah.analyzer<br/>QafiyahAnalyzer & Rhyme Faults"]
    F --> I["pyarud.models.analysis<br/>VerseAnalysis & PoemAnalysis"]
    G --> I
    H --> I
    I --> J["pyarud.formatters.console<br/>AR/EN Pretty, Compact, Markdown"]
    I --> K["pyarud.cli & JSON Export<br/>Zero-Dep CLI + UTF-8 to_json()"]

Architectural Layers

1. Zero-Dependency Core

pyarud.core — Pure stdlib Arabic normalization, diacritic ratio checks, and 1:1 synchronized phonetic compilation.

2. Unified Grammar Engine

pyarud.meters — Immutable MeterRegistry, 16 classical meters, 6 opt-in Muhmal meters, custom/free-verse MeterSpecs, and slot-level Viterbi DP.

3. Science of Rhyme

pyarud.qafiyah — Al-Khalil boundary extraction, all 6 letters & 6 movements, opening Tasri' verification, and 10 poem-level rhyme faults.

4. Models, Formatters & CLI

pyarud.models, formatters, cli — Strictly typed PEP 561 dataclasses, bilingual terminal/Markdown reports, and zero-dep CLI.


1. Zero-Dependency Core (pyarud.core)

  • constants.py: Arabic Unicode letters, harakat, tanween, Uthmani/Quranic marks, Farsi/Urdu variants, phonetic proximity groups (مخارج الحروف), and METER_PRIORITY (single source of truth).
  • arabic.py: Precompiled str.translate and regex utilities (strip_tashkeel, strip_tatweel, strip_punctuation, normalize_ligatures, normalize_orthography, normalize_spaces, tashkeel_ratio, validate_tashkeel).
  • phonetics.py (ArudiConverter & ArudiAlignment):
  • Ordered compilation preserving a strict 1:1 bijection (len(arudi_text.replace(" ", "")) == len(pattern)).
  • Inflection-preserving DEFAULT_ARUDI_REPLACEMENTS with per-family prefix guards and accusative Fathatan carrier stripping.
  • Solar/Lunar الـ & لِلـ assimilation, connected Hamzat al-Wasl, Iltiqa' al-Sakinayn, Alif al-Tafriq, and pronominal هاء الضمير saturation.
  • Single-pass align_hemistich_variants() with explicit is_muqayyad tracking, optional classical poetic-license variants (include_licenses=True), and ArudiAlignment.slice_arudi() / slice_words().

2. Unified Grammar Engine, Custom Meters & Registry (pyarud.meters)

  • tafeela.py & zihaf.py: Immutable, safely hashable classical Tafeela models with structural ending units (watad_majmu, sabab_khafif, watad_mafruq), authoritative surface Arabic form naming (TAFEELA_FORM_NAMES_AR), and validated single/double Zihafs and 'Ilal.
  • bahr.py: All 16 classical meters (29 authentic sub-meter forms) with المراقبة (Muraqabah), المعاقبة (Mu'aqabah), الاعتماد (I'timad), and first-class التصريع (Tasri') pairings.
  • specs.py & muhmal.py: Frozen, validated ProsodicRule (with topological compose serialization), TafeelaSpec, FootSlotSpec, MeterSpec, fluent MeterBuilder, JSON/TOML loaders, and the opt-in MUHMAL_METERS pack.
  • registry.py (MeterRegistry): Persistent, immutable copy-on-write registry with lazy grammar compilation and zero global mutation.
  • engine.py (DeterministicProsodyEngine):
  • Dynamic per-slot suffix (min_len, max_len) DFS pruning (handles feet of any length, including length-2 Batr and length-9 Tarfeel).
  • Bounded NFA/DP exact parser for variable-length free-verse meters (repeat_range).
  • Primary-reading priority (explicitly vocalized Mutlaqah readings are never overwritten by forged-sukun Muqayyadah fallbacks) and pairwise classical foot-level disambiguation (Kamel vs Rajaz, Wafer vs Hazaj) scoped strictly to classical tiers.
  • Unified slot-level Levenshtein Viterbi DP (match_broken_verse / align_broken_shatr) localizing broken, missing, and extra-bit feet across classical, Muhmal, free-verse, and custom meters.

3. Science of Rhyme (pyarud.qafiyah)

  • QafiyahAnalyzer (shares ArudiConverter with ArudProcessor and reuses precomputed ArudiAlignment):
  • Exact Farahidian Qafiyah boundary, 5 rhythmic types (المترادف، المتواتر، المتدارك، المتراكب، المتكاوس), and 9 structural classifications.
  • All 6 letters (rawi, wasl, khuruj, ridf, tasees, dakhil) and all 6 classical movements (majra, nafadh, hadhw, ishba, rass, tawjih).
  • Opening-verse Tasri' verification (is_tasri_rhyme) and poem-level rhyme-fault detection (detect_rhyme_faults: IQWA, ISRAF, IKFA, IJAZAH, all 5 SINAD_*, and ITA).

4. Models, Processor, Formatters & CLI

  • Strictly typed PEP 561 models (pyarud.models) using stdlib enum.StrEnum (MeterKey, MeterVariation, MeterTier, FootStatus, RhymeType, RhymeFaultType) and slotted dataclasses (FootAnalysis, ShatrAnalysis, QafiyahAnalysis, RhymeFault, MeterMatchCandidate, VerseAnalysis, PoemAnalysis) with .to_dict() and UTF-8 .to_json().
  • ArudProcessor (ArudhProcessor): Zero global mutable state, scoped .with_meters() / .without_meters(), strict MeterNotFoundError / TashkeelError signaling, true pre-consensus is_homogeneous, and consensus bahr_type.
  • formatters.console & cli.py: Bilingual (ar/en), multi-style (pretty, compact, markdown, json), RTL-safe, NO_COLOR-aware terminal reporting plus the zero-dependency pyarud / arud CLI (scan, arudi, qafiyah, meters, zihafs, explain).