Skip to main content

A fast implementation of tf-idf

Project description

Fast TF-IDF

A blazingly fast TF-IDF implementation for Python that uses multiprocessing to parallelise document processing.

Key Features:

  • orders of magnitude faster than TensorFlow's TextVectorization (see benchmarks below)
  • tensorflow-compatible vocabulary and idf weights can be used with TensorFlow's TextVectorization, it just computes them much quicker
  • it's much more memory efficient too, consuming less than half the memory required by TextVectorization for the same dataset in my tests

Installation

# Using pip
pip install fast-tfidf

# Or install from source
git clone https://github.com/npil/fast-tfidf.git
cd fast-tfidf
pip install -e .

Usage

This package provides vocabulary extraction and IDF weight calculation that can be used standalone or integrated with other libraries.

Basic Example

from fast_tfidf import get_vocabulary_and_idf_weights

# Your text documents
documents = [
    "machine learning is amazing",
    "deep learning and machine learning",
    "artificial intelligence and deep learning"
]

# Extract vocabulary and IDF weights
# Get top 1000 terms (unigrams only)
vocabulary, idf_weights = get_vocabulary_and_idf_weights(
    documents,
    n_features=1000,
    remove_stopwords=False,
    use_bigrams=False
)

print(f"Vocabulary size: {len(vocabulary)}")
print(f"Top terms: {vocabulary[:5]}")
print(f"IDF weights: {idf_weights[:5]}")

With Bigrams

# Include bigrams for richer representation
vocabulary, idf_weights = get_vocabulary_and_idf_weights(
    documents,
    n_features=2000,
    use_bigrams=True  # Enable bigrams
)

With Stopword Removal

# Remove common English stopwords
vocabulary, idf_weights = get_vocabulary_and_idf_weights(
    documents,
    n_features=1000,
    remove_stopwords=True  # Filter out stopwords
)

Integration with TensorFlow

from tensorflow.keras.layers import TextVectorization
from fast_tfidf import get_vocabulary_and_idf_weights

vocabulary, idf_weights = get_vocabulary_and_idf_weights(documents, n_features=2000)

vectoriser = TextVectorization(
    vocabulary=vocabulary,
    idf_weights=idf_weights,
    output_mode='tf_idf'
)

# No adapt() needed - use pre-computed weights! 🚀
# Your model can now use 'vectoriser' for TF-IDF transformation

Note: Although fast-tf-idf uses a different IDF formula than TensorFlow's default log((1 + n_docs) / (1 + df)) + 1 vs TensorFlow's internal formula), this only affects absolute values while preserving relative term importance.

Performance

Benchmark results comparing against TensorFlow's TextVectorization:

Documents TensorFlow fast-tfidf Speedup
100 204 ms 29 ms 7.1x
1,000 1.06 s 28 ms 38.3x ⚡⚡
5,000 3.00 s 43 ms 70.4x ⚡⚡⚡
10,000 7.51 s 77 ms 97.0x ⚡⚡⚡⚡

See benchmarks/ directory for detailed results and comparison scripts.

Development

Dependencies are managed using poetry.

There is a vscode dev container that can be used for development, or otherwise you can run make install in the environment of your choice to install the package and all dependencies (requires poetry and make).

We use the following tools to ensure code quality:

  1. ruff
  2. mypy
  3. pytest

You can run all formatting / linting / type checks using make lint (you can use make fix to automatically fix some formatting errors).

You can run unittests using make test.

You can run linting and unittests using make lint-test.

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

fast_tfidf-0.1.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

fast_tfidf-0.1.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file fast_tfidf-0.1.2.tar.gz.

File metadata

  • Download URL: fast_tfidf-0.1.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fast_tfidf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b56647ba811ee1d8abe627b5335c5f3da114a3922b4bec24300bd205a1c8ea5d
MD5 063f056789b3af67b78a4f46a0fd4fd4
BLAKE2b-256 146967224641a228a154a21fb3c7b0672176bd476887c48bad51ca965b80b76c

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_tfidf-0.1.2.tar.gz:

Publisher: release.yaml on npil/fast-tfidf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_tfidf-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fast_tfidf-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fast_tfidf-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b031fd33df8d7493b032bd2e711881c75d1c3d79697c4d9ae27355096e18c8c1
MD5 a50e326e7ac97be429cf5003c66e5694
BLAKE2b-256 c59708de6749f5c2b74e071539099b323f75e189ea3bc0f1e8d2ce3bd53f5b5d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_tfidf-0.1.2-py3-none-any.whl:

Publisher: release.yaml on npil/fast-tfidf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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