O(1) semantic vocabulary reduction using BERT and FAISS.
Project description
Semantic Reducer
A high-performance Python library that bridges the gap between Deep Learning and traditional Machine Learning.
semantic-reducer uses BERT contextual embeddings and FAISS vector search to semantically normalize and reduce the vocabulary of a text corpus.
By mapping semantically equivalent words (for example: fast, quick, rapid) to a single, highly frequent canonical word, it creates cleaner and denser input for traditional ML models such as TF-IDF + SVM, Random Forest, or Logistic Regression.
After the initial processing stage, inference on new text requires only a dictionary lookup, enabling O(1) runtime complexity.
Example : https://colab.research.google.com/drive/1qZAxCif_shYCu1gIfUtU-zmDLVc4tpEm?usp=sharing
Features
-
Context-Aware Processing
Uses Transformer models (default:bert-base-multilingual-cased) to capture contextual meaning instead of relying on static embeddings.- Language Agnostic Supports any language that has an Hugging Face Transformer.
-
Smart Vocabulary Reduction
Automatically replaces rare or obscure words with their most frequent semantic equivalents. -
O(1) Inference Speed
After training, the BERT and FAISS pipeline is no longer required. Text reduction is performed using a lightweight Python dictionary. -
Production Ready
Supports saving and loading compiled reduction maps for deployment. -
Hardware Agnostic
Automatically detects and uses GPU (CUDA) if available, while remaining fully functional on CPU.
Installation
Install via pip:
pip install semantic-reducer
Quick Start
1. Training and Building the Reduction Map
from semantic_reducer import SemanticReducer
# Initialize the reducer
# (downloads the BERT model on first run)
reducer = SemanticReducer()
# Training corpus
corpus = [
"The quick brown fox jumps over the lazy dog.",
"A fast dark-colored canine leaps above a tired hound.",
"Speedy foxes jump over sleepy dogs."
]
# Extract contextual representations
reducer.process_corpus_contextually(corpus, batch_size=2)
# Finalize embeddings and create FAISS index
reducer.finalize_embeddings()
reducer.build_index()
# Build the semantic reduction dictionary
reducer.build_reduction_map(
threshold=0.85, # similarity threshold
top_k=5 # number of neighbors to consider
)
# Save the trained system
reducer.save_system(prefix="my_corpus")
2. Lightning-Fast Inference
Once the reduction map is built, BERT is no longer required. New text is processed through a dictionary lookup.
from semantic_reducer import SemanticReducer
# Initialize reducer
reducer = SemanticReducer()
# Load saved system
reducer.load_system(prefix="my_corpus")
# Reduce new text instantly
new_text = "The rapid fox leaped."
reduced_text = reducer.reduce_text(new_text)
print(f"Original: {new_text}")
print(f"Reduced: {reduced_text}")
Workflow Overview
-
Contextual Encoding
Extract contextual word embeddings using BERT. -
Vector Indexing
Store embeddings in a FAISS similarity search index. -
Semantic Clustering
Identify semantically similar words. -
Canonical Replacement
Replace each cluster with the most frequent word in the corpus. -
Dictionary Compilation
Store the final mappings for fast inference.
Requirements
- Python ≥ 3.8
- torch
- numpy
- faiss-cpu
- transformers
- tqdm
Install dependencies manually if needed:
pip install torch numpy faiss-cpu transformers tqdm
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file semantic_reducer-0.1.1.tar.gz.
File metadata
- Download URL: semantic_reducer-0.1.1.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5e847e27155e577bb28781b034e21f7c45830dd445a725696b92ac962e5a7af
|
|
| MD5 |
f9ee400f4dbff563051be4bb22b5221b
|
|
| BLAKE2b-256 |
d2592a50032f6a051a75ab0cf5c8a92f8dd91ec6441862b85360450748e982ce
|
Provenance
The following attestation bundles were made for semantic_reducer-0.1.1.tar.gz:
Publisher:
main.yml on abkafi1234/Semantic-Reducer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
semantic_reducer-0.1.1.tar.gz -
Subject digest:
b5e847e27155e577bb28781b034e21f7c45830dd445a725696b92ac962e5a7af - Sigstore transparency entry: 1059427813
- Sigstore integration time:
-
Permalink:
abkafi1234/Semantic-Reducer@d478b7edc7c84e59ed891938e148c7153d07f960 -
Branch / Tag:
refs/tags/update0.1.1 - Owner: https://github.com/abkafi1234
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@d478b7edc7c84e59ed891938e148c7153d07f960 -
Trigger Event:
release
-
Statement type:
File details
Details for the file semantic_reducer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: semantic_reducer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
261eb03a16c1e7aa686516d74ad7f91f7aad83b04f479595a0243155400c5841
|
|
| MD5 |
e15e3cbfb6b7e6411cc07ad101d0ed76
|
|
| BLAKE2b-256 |
2c7e5abd35a0bce594267800a8d6f9bea284ad75876d73ae707b5335e29899a4
|
Provenance
The following attestation bundles were made for semantic_reducer-0.1.1-py3-none-any.whl:
Publisher:
main.yml on abkafi1234/Semantic-Reducer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
semantic_reducer-0.1.1-py3-none-any.whl -
Subject digest:
261eb03a16c1e7aa686516d74ad7f91f7aad83b04f479595a0243155400c5841 - Sigstore transparency entry: 1059427822
- Sigstore integration time:
-
Permalink:
abkafi1234/Semantic-Reducer@d478b7edc7c84e59ed891938e148c7153d07f960 -
Branch / Tag:
refs/tags/update0.1.1 - Owner: https://github.com/abkafi1234
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@d478b7edc7c84e59ed891938e148c7153d07f960 -
Trigger Event:
release
-
Statement type: