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.

This model serves as a baseline for comparison with neural network approache developed as part of another project.

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.3.tar.gz (6.9 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.3-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stylometric_ai_detector-0.2.3.tar.gz
  • Upload date:
  • Size: 6.9 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.3.tar.gz
Algorithm Hash digest
SHA256 5fd295dd3284e3c854b7ade0b607194d8812de50f2cda10e3fbfb3901032cfdb
MD5 2e5f61aab74630a44e0b1a219cf69c7c
BLAKE2b-256 cee18c816835b2d5e349cc161e1263d0d84e1ebb264ee01a9e0cd86cfb39c0a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stylometric_ai_detector-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 296ca31e879546c28c406af27f3c8f2fd11a882eb84123d898f15c864eea2e5a
MD5 7ee3a0d6656febb0825c5b69aa9d1a72
BLAKE2b-256 cc980bc3bc7fb9422ea49ed6dbda21bbb337e0269e083bdaf65e8623ac3b69c2

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