A dictionary-like class that uses semantic similarity for key matching instead of exact matches.
- Drop-in replacement for dict: Implements the complete standard dictionary interface
- Semantic matching: Find keys based on semantic similarity, not just exact matches
- Flexible embedding: Works with various embedding models (sentence-transformers, Hugging Face, OpenAI)
- Type annotations: Fully typed with generics support
You can install the package from PyPI:
For additional functionality, you can install optional dependencies:
Get up and running with SemanticDictionary in just a few steps:
-
Install the package with your preferred embedding model:
pip install semantic-dictionary[sentence-transformers] -
Create a simple dictionary:
from semantic_dictionary import SemanticDictionary, SentenceTransformerAdapter from sentence_transformers import SentenceTransformer # Initialize the embedding model and adapter model = SentenceTransformer('all-MiniLM-L6-v2') adapter = SentenceTransformerAdapter(model) # Create the semantic dictionary with a similarity threshold sd = SemanticDictionary(adapter, similarity_threshold=0.75) # Add some items sd["customer support"] = "Help desk contact information" sd["product pricing"] = "Current product price list" sd["shipping policy"] = "Information about shipping options" -
Use semantic lookups:
# These will work even though the keys don't exactly match print(sd["customer help"]) # Returns "Help desk contact information" print(sd["price list"]) # Returns "Current product price list" print(sd["delivery info"]) # Returns "Information about shipping options" # Check if semantically similar keys exist if "refund policy" in sd: print("Refund information found!") else: print("No refund information available") -
Adjust the similarity threshold as needed for your use case:
# More strict matching (closer to 1.0) strict_sd = SemanticDictionary(adapter, similarity_threshold=0.9) # More lenient matching (closer to 0.0) lenient_sd = SemanticDictionary(adapter, similarity_threshold=0.6)
- Examples - Check out various examples, including:
- Basic Example - Simple demonstration of core functionality
- Sentence Transformers Example - Using with sentence-transformers
- Advanced Example - Real-world use cases like FAQ systems, command routing, and more
SemanticDictionary provides exceptions representing common issues:
- KeyError: Raised when no semantically similar key is found
- ZeroVectorError: Raised when a zero vector is encountered during similarity calculation
- EmbeddingError: Raised when there's a problem with embedding generation
It's important to understand when semantic similarity is used versus standard dictionary behavior:
- Key lookups: sd[key], sd.get(key), key in sd
- Key deletion: del sd[key], sd.pop(key)
- Key checking: key in sd
- Key finding: sd.setdefault(key, default)
- Dictionary merging: sd | other_dict, sd |= other_dict, sd.update(other_dict)
- Dictionary comparison: sd == other_dict, sd != other_dict, sd < other_dict, etc.
- Dictionary iteration: sd.keys(), sd.values(), sd.items(), iter(sd)
This distinction is crucial to understand when working with SemanticDictionary, as it affects how keys are matched and processed.
If you encounter issues, please open an issue on the GitHub repository with a minimal reproducible example.
-
Clone the repository:
git clone https://github.com/eu90h/semantic-dictionary.git cd semantic-dictionary -
Install development dependencies:
pip install -e ".[all]" pip install -r requirements-dev.txt
This project is licensed under the MIT License - see the LICENSE file for details.
.png)


![Nitrux [Linux] 5.0 Launches with Hyprland as Default Desktop](https://linuxiac.com/wp-content/uploads/2025/11/nitrux50.jpg)