Skip to main content

AI vs Human text detection using stylometric features and Random Forest

Project description

stylometric-ai-detector

AI vs Human text detection using stylometric features and a Random Forest classifier.

Overview

This library provides two functions:

  • extract_stylometric_features(text) — Extract 8 stylometric features from any text.
  • predict(text) — Classify text as AI-generated or human-written, with a confidence score.

The model is a Random Forest classifier achieving 96% accuracy, trained on the AI vs Human Text dataset. On first use, the model is automatically downloaded from Hugging Face and cached locally.

Installation

pip install stylometric-ai-detector

Quick Start

from stylometric_ai_detector import extract_stylometric_features, predict

# Extract stylometric features
features = extract_stylometric_features("The quick brown fox jumps over the lazy dog.")
print(features)
# {'char_count': 44, 'word_count': 9, 'avg_word_len': 4.0, 'punct_count': 1, 'sentence_count': 1, 'avg_sentence_len': 9.0, 'upper_case_count': 0, 'title_case_count': 1}

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

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

Stylometric Features

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. It is downloaded and cached to ~/.cache/stylometric-ai-detector/ on first use — no extra setup needed.

Dataset

Trained on Shanegerami's AI vs Human Text dataset from Kaggle. The dataset contains ~487k text samples labeled as human-written (0) or AI-generated (1).

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 Distribution

stylometric_ai_detector-0.2.2.tar.gz (6.8 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.2-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stylometric_ai_detector-0.2.2.tar.gz
  • Upload date:
  • Size: 6.8 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.2.tar.gz
Algorithm Hash digest
SHA256 1d29a3807f164c1d1212d1e35a133fe18b4d1246dd38ec68c4d01e0e6c61908a
MD5 5494faca267017128374f92a21405217
BLAKE2b-256 280effdd98a932556af759081362bddda8985f7e4d8a9a505761ac43027c17e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stylometric_ai_detector-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1bc83037b6f468001cd1942ac309943a6b95ea0817fc7066f6be3c0106476a29
MD5 1b661c68268174dc5012d45c5e9d9f5d
BLAKE2b-256 676dc98a9022ac1fdc520f4d065e0d9250e60e219e78da56726faebe633a1b86

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