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-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.1.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.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fast_tfidf-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 2070418425d889fd18199bcfc162f75d5131e43c93d0a9242ca1c29461c82945
MD5 e0ded04dd5d3dd5ee8cc8fde522996fa
BLAKE2b-256 807f02f2f31fe8134997c6a1fd3ff39037697082b5d57f41eccb30216b4587f7

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: fast_tfidf-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b94eb3daf4820b4613d17d4b4ca602c2a073560af750401067ac769a60b21506
MD5 0362ed74d2cdcfe58c5871702f5f7c25
BLAKE2b-256 6b700a8a63deba4d7752f0794992dfd54fada2c64f97631b213fda98464e1124

See more details on using hashes here.

Provenance

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