@salem-qahtani/arabic-profanity
v0.1.0
Published
Arabic + English (incl. Arabizi) profanity classifier with injectable word lists.
Maintainers
Readme
@salem-qahtani/arabic-profanity
A profanity classifier for Arabic, English and Arabizi (Arabic written in Latin letters and digits). It returns a verdict (allow, flag or block) and can mask what it matched. It handles the evasions that defeat naive word lists: stretched letters (fuuuck, كسسس), invisible characters inside words, Arabic diacritics and tatweel, interchangeable Arabic letter forms, fullwidth and compatibility characters, and words spelled out with spaces (f u c k, ك س ي).
You bring the word lists. The package ships the engine and no word lists. For English, it uses obscenity's English dataset as a default flag tier, so English works out of the box; the default can be replaced or turned off. For Arabic, the word lists section documents a contributed starting point.
npm install @salem-qahtani/arabic-profanityRequires Node.js 20 or later (or any modern JavaScript runtime). It ships ESM and CommonJS builds with TypeScript types.
Quick start
import { createFilter } from "@salem-qahtani/arabic-profanity";
const filter = createFilter({
enBlock: ["badword"], // English words to block outright
arBlock: ["مثال"], // Arabic words to block outright (exact word match)
arFlag: ["تجربة"], // Arabic words to flag for review (written naturally; folded for you)
arabiziFlag: ["5ara"], // Arabizi spellings to flag for review
});
filter.classify("some text");
// → { action: "allow" } | { action: "block" } | { action: "flag", source: "en" | "ar" | "arabizi" }
filter.moderate("that is b a d w o r d");
// → { verdict: { action: "flag", source: "en" }, masked: "that is ****" }Create the filter once (at startup or module scope) and reuse it: createFilter compiles every list into matchers, and classify / moderate are then pure, synchronous and fast.
Verdicts and tiers
| Verdict | Meaning | What typically triggers it |
| --- | --- | --- |
| block | Tier 1: an unambiguous list hit. Safe to act on without a human. | A word in enBlock or arBlock. |
| flag | Tier 2: a broader, fuzzier or heuristic hit. Accept it but hold it for review, or mask it. | obscenity's English dataset, arFlag, arabiziFlag, a spaced-out word, or a stretched Arabic block word. |
| allow | Nothing matched. | |
When several things match, the highest tier wins. A flag verdict says which matcher fired (source: "en" | "ar" | "arabizi"), so you can route it.
A verdict never contains the matched word, its position or its length. That is deliberate: you can't accidentally echo a list entry back to a user, which would leak your list and coach people on how to evade it. If you need to show the user something, use the masked text.
How each list matches:
enBlockandarabiziFlagmatch whole Latin tokens and tolerate stretching.fuckalso matchesfuuuckandfuckkk, andkossmatcheskossss. Every run of identical letters must keep at least its listed length, so an entry with a genuine double letter (nigger) does not match the one-letterniger.arBlockmatches whole Arabic words exactly, after folding (see Normalization). That is what lets it block without review. Because the match is exact, a stretched form of a block word (زفتتت) misses the block tier. It is caught by the flag tier instead, and never silently blocked.arFlag(and everyarBlockentry) also match Arabic words under a parity rule: each run of a letter must have exactly its listed length, or be stretched to at least two more. A genuine doubled letter is exactly two, while a stretch is usually three or more. So a single-letter entry such asملnever matches the innocent doubledممل, butكسسسdoes matchكس. The cost is that a stretch of exactly two (كسس) is not caught.- Spaced-out words. A run of at least
spacedMinRun(default 3) single letters separated only by spaces or punctuation is joined and re-tested at the flag tier only. A heuristic never blocks. - Matching is always by whole word, never by substring. Inflected or compound forms need their own entries. The English default (obscenity) is the exception: it does its own fuzzy matching.
Masking
moderate(text) returns { verdict, masked }. In masked, every matched region is replaced by exactly ****, whatever the length of the word, so the length isn't revealed either.
filter.moderate("fuck this shit"); // masked: "**** this ****"
filter.moderate("f.u.c.k"); // masked: "****" (separators are part of the region)
filter.moderate("👍👍fuck👍👍"); // masked: "👍👍****👍👍"
filter.moderate("a perfectly fine day"); // masked is the input, unchanged- Choosing tiers.
mask: "block" | "flag" | "both"(default"both") picks which tiers are masked. Matches that overlap are one match seen by two matchers, so they merge into one region that takes the highest tier involved. Withmask: "flag", a word on a block list therefore stays visible even if a flag pattern also matched it. Matches that only touch keep their own tiers, so a flag word written right up against a block word is still masked under"flag". Masked regions that end up touching are shown as a single****. - Positions are exact. The matchers see normalized text, but masking maps every position back to your original input, through every normalization step. Masks never split a character, an emoji or a combining sequence (see the next section for the one runtime caveat). If a match covers only part of a character that normalization expanded (for example
ﷺ, which expands to a whole phrase), the whole original character is masked. - The verdict never changes.
moderate(text).verdictis always identical toclassify(text).classifyis cheaper, so use it when you don't need the mask.
When the whole input comes back as ****
The library masks the entire input whenever it cannot be sure where a match is, rather than risk masking the wrong characters or leaving the match visible. Your verdict is unaffected, but a user would see their whole comment replaced by ****. There are two situations where this happens.
1. A custom English matcher that can't say where it matched. If you supply your own english matcher without spans, or its spans returns anything invalid, every English hit masks the whole input. The default matcher always reports its spans, so this only applies to custom matchers.
2. Segmentation that doesn't line up with normalization. Masking cuts the input into grapheme clusters using Intl.Segmenter, and checks on every call that this lines up with the normalized text. With Intl.Segmenter available, a mismatch effectively never happens. It is available in Node.js 20+ (official builds include full ICU), Deno, Bun, Chrome and Edge 87+, Safari 14.1+ and Firefox 125+.
Without Intl.Segmenter, the library approximates clusters itself: each code point, with any following combining marks, joiners, variation selectors and skin-tone modifiers attached. That handles accents (e + U+0301), Arabic diacritics and emoji. The approximation does not group characters that compose across separate code points, such as conjoining Hangul jamo. If a match lands in text like that, the whole input is masked. Some complex Indic and Southeast Asian clusters can also be split, which is cosmetic: a mark next to a **** may end up on its own. Runtimes where this applies:
- Firefox older than 125.
- Node.js built without ICU (
--with-intl=none). - Some embedded or mobile JavaScript engines, such as Hermes in React Native, depending on version.
If you target one of these, load an Intl.Segmenter polyfill such as @formatjs/intl-segmenter before your first moderate call. You can check with typeof Intl.Segmenter === "function". classify doesn't use segmentation and works the same everywhere.
Normalization
Before matching, the text goes through these steps:
- NFKC. Folds compatibility characters: fullwidth
fuckbecomesfuck, the ligaturefibecomesfi, and Arabic presentation forms become ordinary letters. - Invisible characters are removed wherever they appear. That means every Unicode default-ignorable code point: zero-width spaces and joiners, directional marks and controls (including the Arabic Letter Mark), the soft hyphen, the word joiner, variation selectors and tag characters. The Arabic tatweel (
ـ) is removed too. None of these change how a word looks, so a word is matched as the reader sees it. The flip side: an invisible character placed between two words no longer separates them. - Arabic folding. Diacritics (harakat) are removed.
أ إ آ ٱbecomeا,ةbecomesه, andىbecomesي. - Latin lowercasing. Digits are kept, because Arabizi uses them for letters:
7for ح,3for ع,5for خ.
List entries go through the same steps when the filter is built, so you can write them in their natural spelling, with diacritics, tatweel, ة, presentation forms or fullwidth letters. An entry that could never match throws a RangeError from createFilter, naming the list and index but not the entry. Examples: a phrase with a space (moby dick), a symbol (f*ck), an accented Latin letter (fück), or a script that doesn't belong in that list. Matching is by single word, and Latin tokens are a-z and 0-9 only, so such an entry would otherwise sit in your list doing nothing.
Configuration
| Option | Default | Description |
| --- | --- | --- |
| enBlock | [] | Latin words to block. Whole token, tolerates stretching. |
| arBlock | [] | Arabic words to block. Exact whole word after folding. Also covered by the flag parity rule, so stretched forms are flagged. |
| arFlag | [] | Arabic words to flag, using the parity rule. |
| arabiziFlag | [] | Arabizi spellings to flag. Whole token, tolerates stretching. |
| tokenWhitelist | [] | Exact normalized words that no word-list check will match, in either tier. It does not reach the English matcher, which reads whole text rather than words; use enWhitelist for English false positives. |
| enWhitelist | [] | Phrases added to the default English matcher's own whitelist (for example "cockpit"). Ignored if you supply english. |
| english | obscenity | The English flag-tier matcher: { hasMatch(text), spans?(text) }, or false to disable it. Without spans, or if spans returns anything invalid, an English hit still counts toward the verdict and moderate masks the whole input (it can't tell where the match is). |
| spacedMinRun | 3 | Minimum number of spaced single letters that are joined and re-tested. At 2, any pair of isolated letters becomes a candidate, so lower it only after measuring against your own lists. |
| mask | "both" | Which tiers moderate masks: "block", "flag" or "both". |
defaultEnglishMatcher(extraWhitelist?) is exported, so you can wrap or extend the default English matcher.
Security notes
- Keep your lists on the server. The engine itself is safe to run anywhere, but any list you bundle into client-side code is public to anyone who opens the developer tools.
- Cap input length yourself. There is no built-in length limit. Every pattern is anchored and avoids ambiguous repetition, so there is no catastrophic backtracking. Cost grows linearly with input length times list size, because every word is tested against every pattern; the test suite runs 200,000-character inputs. NFKC can expand some characters (for example
ﷺ) to many times their length.moderatealso builds an index map over the input whenever something matches, so it costs several times more thanclassifyin time and memory. Limit untrusted input to what your field actually allows. - What masking reveals.
maskedshows where every match is, though not its length. That makes onemoderatecall a list-membership test for every word in the input at once. Someone shown their own masked text can paste thousands of candidate words, including stretched spellings, and learn in a single request which are on your list. Withmask: "block"or"flag", they also learn which tier each word is on. Any per-request rate limit is therefore far weaker against masked output than against a plain accept or reject. If your lists are sensitive, show masked text to other readers but not back to its author, or limit by input size or by the number of masked regions. - This is one layer. Keyword matching can't understand context, sarcasm or images. Combine it with user reports and human review.
Known limitations
These are planned for later releases:
- Latin letters with accents split a word into separate tokens:
fückis seen asf+ck. English hits may still be caught by the obscenity tier. - Persian and Urdu letter variants that look like Arabic letters (
کforك,یforي) are not yet folded. The same goes for Arabic-Indic digits in Arabizi. - Text is matched in the order it is stored, not the order it is displayed. Bidirectional override controls are stripped, but a word written backwards and displayed forwards by such a control is still seen backwards.
- Partially spaced words (
fu ck), and spaced runs shorter thanspacedMinRun, are not joined. - A neighbouring one-letter word joins a spaced run: in
what a b a d w o r d, the wordais joined too, givingabadword, so the spaced word is missed.
Word lists
The lists below are contributed by the package author as documentation. They are not package defaults, and nothing here is loaded unless you copy it into your config. They were compiled by a native Arabic speaker for one platform's moderation. They lean towards dialect and slang, and are neither exhaustive nor neutral: every community should review and adapt them.
Entries are already in folded form: no diacritics, ه instead of ة, ي instead of ى, and no tatweel. The one exception is two stretched spellings that keep ى on purpose. They are folded when the filter is built, like any entry. Matching is by whole word, so the forms with the definite article ال are separate words. Add them with a helper:
const withArticle = (words: string[]) => [...words, ...words.map((w) => "ال" + w)];Starter list: explicit profanity
Explicit sexual, scatological and vulgar terms, with little or no innocent use. They are suitable for arBlock, for example arBlock: withArticle(STARTER_AR_BLOCK).
export const STARTER_AR_BLOCK = [
"قحبه", "قحبتي", "قحاب", "مقحبه", "قحيب", "قحبون", "قحبتك", "قحبتكم",
"قحبتنا", "قحبتهم", "قحبتهما", "تتمقحب", "يتمقحب", "اتمقحب", "مقاحيب", "يمقحبهم",
"يمقحبهن", "متمقحبه", "متمقحب", "عاهره", "عاهرتي", "عواهر", "عاهرات", "عهر",
"عاهرتنا", "عاهرتهم", "عاهرتك", "عاهرتهما", "يمعهرهم", "يمعهرهن", "معهره", "تتمعهر",
"يتمعهر", "متمعهر", "متمعهره", "اتمعهر", "شرموطه", "شرموطتي", "شرموطتك", "شرموطتنا",
"متشرمط", "متشرمطه", "شرموطتهم", "شرومطتكم", "شرموطتهما", "تتشرمط", "يتشرمط", "شرموطهم",
"شرمطونا", "شرموطني", "اتشرمط", "شرمطه", "يشرمطهم", "يشرمطهن", "متناك", "تتناك",
"بنيك", "يتناك", "بيتناك", "منيوك", "ناك", "ناكو", "ناكوا", "ينيكون",
"حينيكون", "سينيكون", "بينيكون", "ناكوهم", "ناكونا", "ناكوني", "ناكوك", "حيتناك",
"ينيكهن", "ينيكهم", "نيك", "زب", "زبي", "زبه", "زبك", "زبكما",
"زبكم", "زبنا", "ازباب", "خصاوي", "كس", "كسي", "كسها", "كسهم",
"كسك", "كسهما", "كسهن", "اكساس", "اكساسهم", "اكساسهن", "اكساسهما", "كسيس",
"كسم", "كسمك", "كسبوك", "كسختك", "كسخوك", "كسخالتك", "كسجدك", "كسجدتك",
"كسمها", "كسمي", "كسوجهك", "كسوجهي", "كسختي", "كسخوي", "كسبوي", "ديوث",
"ديوت", "ديوثه", "دياثه", "ديوته", "دياته", "مديثه", "مديته", "اديث",
"اديت", "مديث", "مديت", "عرص", "معرص", "نغل", "تجليخ", "جلخ",
"سكس", "طيز", "طيزه", "اطياز", "طيزها", "طيزهن", "طيزهم", "طيزي",
"فقحه", "فكحه", "فقحتها", "فكحتها", "فقحتهم", "فكحتهم", "ممحون", "يتمحن",
"تتمحن", "اتمحن", "ممحونه", "مماحين", "ممحونات", "ممحونين", "ديد", "داعر",
"داعره", "يمصه", "يلعنكسمك", "كىىىى", "كىىىمك",
];Arabizi spellings, suitable for arabiziFlag:
export const STARTER_ARABIZI = [
"kos", "87ba", "g7ba", "zby", "zbe", "zbi", "ksmk", "anek",
];Words with legitimate, non-abusive uses
This platform blocked the words below as well, but each one also has real, everyday or serious use, which is why none of them is in the starter list. Decide per community. Flagging them for human review is the safer choice.
Exact collisions with everyday words. محنه ("ordeal, tribulation"), كسكسه ("couscous") and ثدي ("breast", the standard anatomical word) are spelled exactly like a vulgar word and an everyday one. The Arabic block tier matches whole words exactly, so blocking these blocks those exact everyday words too, and nothing beyond them. This platform accepted that trade:
export const COLLISIONS_AR_BLOCK = [
"كسكسه", "محنه", "ثدي",
];Vulgar in slang, standard elsewhere. قضيب also means "rod" or "rail", and زغب means "fluff" or "down". مازوخي is a clinical term, and دعاره is the standard word for prostitution. اغتصاب is not slang at all: it is the standard word for rape. It is left out of the starter list because it appears constantly in news, reviews and serious discussion, and blocking it would silence exactly those conversations:
export const LEGITIMATE_USE_AR_BLOCK = [
"اغتصاب", "يغتصبني", "يغتصب", "اغتصب", "اغتصبهم", "مغتصب", "اغتصبهما", "اغتصبتهما",
"اغتصبتهم", "يغتصبهم", "يغتصبهما", "يغتصبهن", "تغتصب", "تغتصبهم", "تغتصبهن", "تغتصبهما",
"مغتصبه", "قضيب", "قضيبه", "قضيبي", "قضيبنا", "قضيبهم", "اقضبتنا", "مازوخي",
"مازوخيه", "زغب", "يزغب", "مزغوب", "تزغب", "زاغب", "دعاره",
];Arabizi spellings with innocent collisions that this platform flagged. ks is a common abbreviation, m9 is also a pistol and a camera model, and arkb means "I ride":
export const AMBIGUOUS_ARABIZI = [
"ks", "m9", "arkb",
];Not published: religious, family and identity terms
The platform these lists come from also flags a broader set covering religious, family and identity terms, because they turn up in compound insults. Whether to match those is a per-platform decision, and keyword matching in these categories produces heavy review-queue volume, because the same words are used innocently all the time. They are not published here: if you want that behaviour, build the set for your own audience and dialect.
License
MIT. The default English matcher is obscenity (MIT), installed as a dependency.
