Skip to main content

AI vs Human text detection using stylometric features and a Random Forest classifier — a baseline model for benchmarking.

Project description

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.

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

stylometric_ai_detector-0.2.4.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

stylometric_ai_detector-0.2.4-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file stylometric_ai_detector-0.2.4.tar.gz.

File metadata

  • Download URL: stylometric_ai_detector-0.2.4.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for stylometric_ai_detector-0.2.4.tar.gz
Algorithm Hash digest
SHA256 f573c6277f2826a8813d147bfe1ffb163e511fbd1be16d034ed04884e09cb24d
MD5 e6609a7e747809b2cb7da7b49fec009b
BLAKE2b-256 c66cfd6f4e85889b60fed9cae586674adf30f9d9b82427e89fbff10cd2d78810

See more details on using hashes here.

File details

Details for the file stylometric_ai_detector-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for stylometric_ai_detector-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d4dbfb78e4d49f4f84c39597ee21a7f664b1df53e4f0b8fc8e46f2c69e2ae1f0
MD5 eab201254edab7a6f04b4171da9c41a8
BLAKE2b-256 d8df2d2f6c37a128a03747ab6ce7615559f23d159cabc77af0d818ae6c6c8b73

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