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

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-tf-idf 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.0.tar.gz (3.9 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.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fast_tfidf-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 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.0.tar.gz
Algorithm Hash digest
SHA256 85e70bcb4469f23ec844474bcc994dface033d3343e485c6af6d30787c84cc81
MD5 d78305c1c4e5253a22639ad8c8e261b2
BLAKE2b-256 91007ba53c04e58bd9feb274bd7275bbe00237b7e9c1655dc9dcf8dde97e1639

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_tfidf-0.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: fast_tfidf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8fe4449acf7cab6965b11a3ec9cb83e6add9b424d51aa42994416d5dd50e441
MD5 6b1343244451ead9d0a610cca6147193
BLAKE2b-256 da5e520b61b757ae365f28d6c2a2e92d8d6bd8b457f23a2b9fbc3003ba11505f

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_tfidf-0.1.0-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