Skip to main content

Arabic multi-label toxic content classifier

Project description

arabic-toxic

Arabic Multi-Label Toxic Content Classifier

A fast and lightweight Arabic NLP package for toxic content detection, content moderation, social media analysis, and Arabic text safety classification.

The package supports multi-label classification and can detect several categories of toxic and harmful content in Arabic text.


Installation

pip install arabic-toxic

Quick Start

Single Text Classification

from arabic_toxic import classify

result = classify("يلعن امك يا ابن الحمار")

print(result)

Output

{
    "text": "يلعن امك يا ابن الحمار",
    "labels": ["Cussing", "Hatred"],
    "top_label": "Cussing",
    "is_toxic": True,
    "scores": {
        "Cussing": 99.999,
        "Hatred": 94.404,
        "Sexual": 2.938,
        "Racial": 0.762,
        "Appearance": 0.033,
        "Violence": 0.027,
        "NOT": 0.001
    }
}

Toxic / Non-Toxic Detection

The classify() function returns an is_toxic field that directly tells whether the input text is toxic.

from arabic_toxic import classify

result = classify("أنت إنسان محترم")

print(result["is_toxic"])

Output

False

Full Example

from arabic_toxic import classify

print(classify("أنت إنسان محترم"))
print(classify("يلعن امك يا ابن الحمار"))

Output

{
    "text": "أنت إنسان محترم",
    "labels": ["NOT"],
    "top_label": "NOT",
    "is_toxic": False,
    "scores": {
        "Hatred": 23.802,
        "Appearance": 23.436,
        "Racial": 13.917,
        "NOT": 11.666,
        "Cussing": 10.767,
        "Sexual": 3.805,
        "Violence": 1.656
    }
}

{
    "text": "يلعن امك يا ابن الحمار",
    "labels": ["Cussing", "Hatred"],
    "top_label": "Cussing",
    "is_toxic": True,
    "scores": {
        "Cussing": 99.999,
        "Hatred": 94.404,
        "Sexual": 2.938,
        "Racial": 0.762,
        "Appearance": 0.033,
        "Violence": 0.027,
        "NOT": 0.001
    }
}

Classifying a List of Texts

The current version can classify a list of texts by applying classify() to each item.

from arabic_toxic import classify

texts = [
    "أنت إنسان محترم",
    "شكراً لك على المساعدة",
    "يلعن امك يا ابن الحمار",
    "الشكل والتفكير حمار",
    "ليبرالية شوية صعاليك"
]

results = [classify(text) for text in texts]

for result in results:
    print(result)

Output

{
    "text": "أنت إنسان محترم",
    "labels": ["NOT"],
    "top_label": "NOT",
    "is_toxic": False,
    "scores": {
        "Hatred": 23.802,
        "Appearance": 23.436,
        "Racial": 13.917,
        "NOT": 11.666,
        "Cussing": 10.767,
        "Sexual": 3.805,
        "Violence": 1.656
    }
}

{
    "text": "شكراً لك على المساعدة",
    "labels": ["NOT"],
    "top_label": "NOT",
    "is_toxic": False,
    "scores": {
        "NOT": 94.747,
        "Hatred": 4.771,
        "Violence": 3.802,
        "Sexual": 3.508,
        "Cussing": 2.306,
        "Appearance": 2.286,
        "Racial": 2.183
    }
}

{
    "text": "يلعن امك يا ابن الحمار",
    "labels": ["Cussing", "Hatred"],
    "top_label": "Cussing",
    "is_toxic": True,
    "scores": {
        "Cussing": 99.999,
        "Hatred": 94.404,
        "Sexual": 2.938,
        "Racial": 0.762,
        "Appearance": 0.033,
        "Violence": 0.027,
        "NOT": 0.001
    }
}

{
    "text": "الشكل والتفكير حمار",
    "labels": ["Hatred"],
    "top_label": "Hatred",
    "is_toxic": True,
    "scores": {
        "Hatred": 98.338,
        "Racial": 12.536,
        "Appearance": 12.058,
        "NOT": 8.266,
        "Cussing": 2.405,
        "Sexual": 1.201,
        "Violence": 0.909
    }
}

{
    "text": "ليبرالية شوية صعاليك",
    "labels": ["Hatred", "Racial"],
    "top_label": "Racial",
    "is_toxic": True,
    "scores": {
        "Racial": 70.922,
        "Hatred": 65.004,
        "Cussing": 22.948,
        "Appearance": 5.112,
        "NOT": 4.478,
        "Sexual": 2.064,
        "Violence": 1.858
    }
}

Probability Scores

from arabic_toxic import predict_proba

scores = predict_proba("يلعن امك يا ابن الحمار")

print(scores)

Supported Labels

Label Description
Appearance Appearance-based insults
Cussing Profanity and offensive language
Hatred Hate and hostility
NOT Non-toxic content
Racial Ethnic or racial attacks
Sexual Sexual harassment or abuse
Violence Violent threats or content

Model Architecture

The model uses:

  • Arabic text normalization
  • TF-IDF word n-grams
  • TF-IDF character n-grams
  • Multi-label classification
  • One-vs-Rest strategy
  • SGD Logistic Classifier

Designed for:

  • Content Moderation
  • Social Media Monitoring
  • Arabic NLP Research
  • Online Community Safety
  • AI Safety Applications

Evaluation Results

Test-set performance:

Metric Score
Macro F1 0.9109
Micro F1 0.9205
Subset Accuracy 0.8433
Hamming Loss 0.0307

Notes

This package is intended for Arabic content moderation, research, and production prototyping.

Performance may vary depending on dialect, domain, and writing style. Always validate the model on your own data before using it in production decision systems.


Author

Dr. Faisal Alshargi

AI Researcher • NLP Engineer • Data Scientist

GitHub: https://github.com/alshargi

PyPI: https://pypi.org/project/arabic-toxic/

Project details


Download files

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

Source Distribution

arabic_toxic-0.2.3.tar.gz (3.4 MB view details)

Uploaded Source

Built Distribution

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

arabic_toxic-0.2.3-py3-none-any.whl (3.4 MB view details)

Uploaded Python 3

File details

Details for the file arabic_toxic-0.2.3.tar.gz.

File metadata

  • Download URL: arabic_toxic-0.2.3.tar.gz
  • Upload date:
  • Size: 3.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for arabic_toxic-0.2.3.tar.gz
Algorithm Hash digest
SHA256 82fb066b922829b4d81935aacef3ec4b0e0c2514e9e78eb67bc5f41b91102d5d
MD5 a1ad0d12a1143b1b90f73328ec9392ac
BLAKE2b-256 11ce3154ea946d5074364646dcc6e82e44a2c324bb7a7e6b663af2d91f01dccb

See more details on using hashes here.

File details

Details for the file arabic_toxic-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: arabic_toxic-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 3.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for arabic_toxic-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 373987b3e41fc8d1e7b1eb3399687e1e97d7262a56e4a05f2371ce6ed7ee28c5
MD5 bb1045cedd823ce67c108dcf1b953ac9
BLAKE2b-256 4374d1176ec41608a37b0f9df88a75729a460ff3ef23366559b38e70f96522a8

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