Skip to main content

stylometric-ai-detector

PyPI version Python License: MIT Code style: black

⚠️ This model is a baseline. It was developed as a reference point for comparing more advanced (neural network-based) approaches from a separate project. While it achieves 96% accuracy, it relies on surface-level stylometric features and may not generalize across domains, languages, or AI model generations. Use as a benchmark, not as a final detector.


AI vs Human text detection using stylometric features and a Random Forest classifier. The package provides two simple functions — one to extract 8 stylometric features from any text, and one to classify text as AI-generated or human-written with a confidence score.

Installation

pip install stylometric-ai-detector

Quick Start

from stylometric_ai_detector import extract_stylometric_features, predict

# Extract stylometric features from any text
features = extract_stylometric_features("The quick brown fox jumps over the lazy dog.")
# {'char_count': 44, 'word_count': 9, 'avg_word_len': 4.0, ...}

# Classify text — model auto-downloaded from Hugging Face on first call
result = predict(text="Artificial intelligence is transforming our world.")
# {'label': 'AI', 'probability': 0.99}

# Or pass pre-computed features
result = predict(features=features)
# {'label': 'AI', 'probability': 0.91}

How It Works

The detector extracts 8 stylometric features that capture surface-level patterns in text — character counts, word lengths, punctuation density, sentence structure, and capitalization. These features are fed into a Random Forest classifier trained on ~487k labeled samples.

Feature Description
char_count Total number of characters
word_count Total number of words
avg_word_len Average word length
punct_count Number of punctuation characters
sentence_count Number of sentences
avg_sentence_len Average sentence length in words
upper_case_count Number of fully uppercase alphabetic words
title_case_count Number of title-case words

Model

The trained Random Forest model is hosted on Hugging Face at dinisds/stylometric-ai-detector.

On first use, the model is automatically downloaded and cached to ~/.cache/stylometric-ai-detector/ — no extra setup needed. You can override the repo by setting the HF_MODEL_REPO environment variable.

Metric Value
Algorithm Random Forest (100 estimators)
Accuracy 96.03%
Train set 389,788 samples
Test set 97,447 samples
Dataset AI vs Human Text

Development

# Clone and set up
git clone https://github.com/dinis-a/stylometric-ai-detector.git
cd stylometric-ai-detector
python -m venv .venv
source .venv/bin/activate
pip install -e .[dev]

# Run tests
pytest -v

# Format code
isort . && black .

# Lint
isort --check-only --diff . && black --check --diff .

Project Structure

├── stylometric_ai_detector/   # Library package
│   ├── __init__.py            # Public API: extract_stylometric_features, predict
│   ├── features.py            # Feature extraction logic
│   ├── predict.py             # Model loading & prediction
│   └── data/                  # Bundled model file
├── tests/                     # Test suite (pytest)
│   ├── test_features.py
│   └── test_predict.py
└── pyproject.toml             # Package metadata & tool config

Limitations

  • Trained on a single English-language dataset — may not generalize to other languages or domains
  • Stylometric patterns vary across AI models and versions (this was trained on pre-2024 data)
  • Relies on surface-level text statistics, not semantic understanding
  • Intended as a baseline for comparison; production use-cases should prefer more robust approaches

License

MIT — see LICENSE for details.

Metadata

Release files for stylometric-ai-detector 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stylometric-ai-detector 0.2.4
File Size Uploaded
stylometric_ai_detector-0.2.4.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stylometric-ai-detector 0.2.4
File Interpreter ABI Platform
stylometric_ai_detector-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / stylometric_ai_detector-0.2.4.tar.gz

Download URL stylometric_ai_detector-0.2.4.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f573c6277f2826a8813d147bfe1ffb163e511fbd1be16d034ed04884e09cb24d
BLAKE2b-256 checksum
How to use checksums
c66cfd6f4e85889b60fed9cae586674adf30f9d9b82427e89fbff10cd2d78810
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / stylometric_ai_detector-0.2.4-py3-none-any.whl

Download URL stylometric_ai_detector-0.2.4-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d4dbfb78e4d49f4f84c39597ee21a7f664b1df53e4f0b8fc8e46f2c69e2ae1f0
BLAKE2b-256 checksum
How to use checksums
d8df2d2f6c37a128a03747ab6ce7615559f23d159cabc77af0d818ae6c6c8b73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.2.4 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page