Skip to main content

Moroccan Darija (Arabic) phrase analyzer — classify each word by source language (French/English/Darija), detect semantic entities (money, time, date, phone...) and strip morphological prefixes. Built in Rust for O(1) lookup.

Project description

darija-router

Moroccan Darija language router — O(1) phrase analysis with morphological stripping, semantic tagging, NER, and code-switching detection. Built in Rust, wrapped for Python.

Classifies Arabic-script Darija words by source language (fr, en, darija), Latin spelling, category, probability, semantic role, and confidence score.

Install

pip install darija-router

Quick Start

import darija_router

result = darija_router.analyze(
    "خويا باغي نلقاك غدوة الساعة 3 صباح فالساحة، "
    "واش باغي نشريو شي تيليفون جديد ب 500 درهم؟"
)

API Reference

darija_router.analyze(phrase, mode=None, fields=None)

Tokenizes a phrase and classifies each token.

Parameters:

  • phrase (str): Input text in Arabic script
  • mode (str, optional): Output preset — "full" (default), "compact", "ner", "codeswitch"
  • fields (list[str], optional): Custom field selection — e.g. ["token", "lang", "confidence"]

Returns: list[dict] with per-token results.

darija_router.run_ner(phrase)

Named Entity Recognition. Returns span-based entities and per-token NER tags.

Returns:

{
  "entities": [
    {"text": "500 درهم", "entity_type": "MONEY", "start": 0, "end": 8, "token_indices": [0, 1]}
  ],
  "tokens": [
    {"token": "500", "ner_tag": "MONEY"},
    {"token": "درهم", "ner_tag": "MONEY"}
  ]
}

Entity types: MONEY, MEASURE, TIME, DATE, PHONE, EMAIL, URL, CARDINAL, DECIMAL, ORDINAL, FRACTION, PERCENT, PERSON, LOC

darija_router.run_codeswitch(phrase)

Code-switching detection with language spans and statistics.

Returns:

{
  "spans": [
    {"text": "خويا واش", "lang": "darija", "start": 0, "end": 9}
  ],
  "tokens": [
    {"token": "خويا", "lang": "darija", "latin": null, "confidence": 1.0}
  ],
  "stats": {
    "darija": 50.0, "fr": 25.0, "en": 0.0, "num": 12.5, "punct": 0.0, "other": 0.0, "unknown": 12.5
  }
}

darija_router.lookup(word)

Single word lookup. Same format as one element from analyze().

darija_router.__version__

Package version string.

Semantic Tags

Tag Detection Example
PUNCT Punctuation characters ؟ ، . !
CARDINAL Digits or Arabic number words 50 عشرين مية
DECIMAL Decimal numbers 3.5 ١٢.٥
MONEY Cardinal + currency word 50 درهم → both tokens tagged
MEASURE Cardinal + unit word 2 كيلو → both tokens tagged
TIME الساعة + cardinal, or time words الساعة 3 صباح مساء
DATE Date words or cardinal + month غدوة 15 مارس
ORDINAL Arabic ordinal words تاني تالت أول
FRACTION Arabic fraction words نص ربع تلت
PERCENT % suffix or بالمئة 20%
PHONE Phone number patterns +212661234567
EMAIL Contains @ and . user@gmail.com
URL Starts with http or www. http://google.com
ADDRESS Address keyword + following word زنقة الحسن

Output Modes

Mode Description
"full" (default) All fields: token, index, lang, entries, stem, semantic, confidence
"compact" token + lang only (no entries/stem)
"ner" NER preset: entities + tokens with ner_tag
"codeswitch" Code-switching preset: spans + tokens + stats

Confidence Scoring

Match Type Score
Exact match 1.0
Normalized (ة→ه) 0.95
Single prefix/suffix strip 0.85
Double strip (prefix + suffix) 0.70
Semantic tag only (no lexicon match) 0.60
Unknown 0.0

How It Works

  1. Embedded lexicon — 1,926 Arabic-script keys (2,226 entries) compiled into the binary via include_str!(). Zero file I/O, zero startup cost.

  2. O(1) HashMap lookup — exact match against a HashMap<String, Vec<Entry>>.

  3. Morphological fallback — when an exact match fails, the engine strips prefixes and suffixes:

    Prefixes: ال و ف ب ل م ك

    Suffixes: هم ها ش ك ي

    Normalization: ةه

    Tries: exact → normalize → single strip → double strip. Minimum stem length of 2 prevents over-stripping.

  4. Semantic tagger — rule-based second pass detects 14 semantic categories via regex patterns and context-aware grouping (e.g. cardinal + currency word → MONEY).

  5. NER engine — extracts span-based entities with context rules for PERSON (سيدي/لالة + next word) and LOC (شارع/زنقة/حي + next words).

  6. Code-switching detector — assigns per-token language labels, groups consecutive same-language tokens into spans, and computes aggregate statistics.

  7. 23 thematic categories — technology, food, transportation, health, administration, clothing, home, education, sports, work, money, adjectives, family, colors, time, nature, slang, functional grammar, common verbs, common descriptions, time expressions, numbers.

Build from Source

git clone https://github.com/LaamiriOuail/darija-router.git
cd darija-router
pip install maturin
maturin develop --release

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

darija_router-0.14.0-cp312-cp312-win_amd64.whl (261.8 kB view details)

Uploaded CPython 3.12Windows x86-64

darija_router-0.14.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (345.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

darija_router-0.14.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (329.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

darija_router-0.14.0-cp312-cp312-macosx_11_0_arm64.whl (322.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

darija_router-0.14.0-cp312-cp312-macosx_10_12_x86_64.whl (335.2 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

Details for the file darija_router-0.14.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for darija_router-0.14.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2e3d831f8d7cfb5b83e9f9d97eb22efe8e04bc08ef0a3a73ddd492b547d6f0d1
MD5 f490e02359b17dc459ee09873f8619d5
BLAKE2b-256 9951c94399c69fcd636eeedbd7f32f49a649ff9ccdb1efe92e546d111e0238e8

See more details on using hashes here.

File details

Details for the file darija_router-0.14.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for darija_router-0.14.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f026147cfa4bbbc499420973a1e96f5e57e127f42fe7b9a9358f63f13509f26b
MD5 a82d605eb514340f2987f053d64847cb
BLAKE2b-256 f1e32ba69b736078bb01694600b01abf3e5018d4e736d3bb45cf45cdfbb519e4

See more details on using hashes here.

File details

Details for the file darija_router-0.14.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for darija_router-0.14.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2e54a57faf2ac42ee9262738e49c54eae2af71324eacd3e7777953371c3ca5dd
MD5 c84daf18ae192b0b27f7ff73583df705
BLAKE2b-256 bb443c6e350cc42ded750638a906a747b08a6f3d41e64f8ed349962a7f877b63

See more details on using hashes here.

File details

Details for the file darija_router-0.14.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for darija_router-0.14.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0758793f812349df0ad783b14b5906bc0f5e59b2becb61cc977bfcc54391501c
MD5 4c57fb21f8ea7a2f8ad7e4948ed42b11
BLAKE2b-256 98daeca9e26e01d1d4179678238206e1d9f7d475b009e4fafbb71f2bba5eaacf

See more details on using hashes here.

File details

Details for the file darija_router-0.14.0-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for darija_router-0.14.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5f6848fd12c526d7ad4a5644e84291301de9a6475530a731f686d2857ab195cd
MD5 d637dee70cd0387270824d020bc466d8
BLAKE2b-256 00b8951671ca6431d9e6f945bc5836ea203081ef511b2372fe04c9e2388e16c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page