Skip to main content

Semantic Comparer

Advanced text alignment and semantic containment analysis tool using modern Python practices.

Features

  • Semantic Alignment: Uses Smith-Waterman algorithm with sentence transformers for intelligent text comparison
  • Modern CLI: Built with Typer and Rich for beautiful, user-friendly interface
  • Async Processing: High-performance asynchronous operations
  • File Support: Direct text input or file-based processing
  • Rich Output: Colorized, formatted results with detailed statistics
  • Type Safety: Full type annotations and modern Python practices

Usage

Install

pip install semantic-comparer

Basic Comparison

# Compare two texts directly
semantic-comparer compare "This is the first text." "This is the second text."

# Compare with custom parameters
semantic-comparer compare \
  "First text content" \
  "Second text content" \
  --model paraphrase-multilingual-MiniLM-L12-v2 \
  --gap-penalty 0.3 \
  --similarity-threshold 0.5

File-based Comparison

# Compare text files (prefix with @)
semantic-comparer compare @file1.txt @file2.txt

# Mix direct text and file
semantic-comparer compare "Direct text" @file.txt

Output Options

# Quiet mode (summary only)
semantic-comparer compare text1 text2 --quiet

# Save detailed results to JSON
semantic-comparer compare text1 text2 --output results.json

Command Line Options

Option Short Description Default
--model -m Sentence transformer model paraphrase-multilingual-MiniLM-L12-v2
--gap-penalty -g Penalty for gaps (0.0-1.0) 0.3
--similarity-threshold -t Minimum similarity for matches (0.0-1.0) 0.5
--output -o Output file for JSON results None
--quiet -q Suppress detailed output False

Understanding the Results

Alignment Types

  • ✓ Match: Paragraphs that are semantically similar
  • ⚠ Only in A/B: Paragraphs present in one text but not the other
  • ✗ Unaligned: Paragraphs that couldn't be matched

Containment Score

The semantic containment score measures how much of text A's semantic content is found in text B:

  • 0.0-0.4: Low similarity (Red)
  • 0.4-0.7: Moderate similarity (Yellow)
  • 0.7-1.0: High similarity (Green)

Advanced Usage

Custom Models

# Use a different sentence transformer model
semantic-comparer compare text1 text2 --model all-mpnet-base-v2

Fine-tuning Parameters

# Stricter matching (higher threshold)
semantic-comparer compare text1 text2 --similarity-threshold 0.8

# More lenient gap handling (lower penalty)
semantic-comparer compare text1 text2 --gap-penalty 0.1

Technical Details

Algorithm

The tool uses the Smith-Waterman algorithm adapted for semantic similarity:

  1. Text Segmentation: Split texts into paragraphs
  2. Embedding Generation: Convert paragraphs to semantic vectors
  3. Similarity Calculation: Compute cosine similarity between vectors
  4. Dynamic Programming: Apply Smith-Waterman for optimal alignment
  5. Score Calculation: Weighted containment score based on matches

Performance

  • Async Processing: Non-blocking I/O operations
  • Memory Efficient: Streaming file processing for large texts
  • Progress Tracking: Real-time progress indicators
  • Error Handling: Robust error handling with user-friendly messages

Security

  • Input Validation: Comprehensive parameter validation
  • File Safety: Secure file operations with size limits
  • Text Sanitization: Removal of problematic characters
  • Error Isolation: Graceful error handling without data exposure

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes with proper type annotations
  4. Add tests for new functionality
  5. Ensure code passes linting and type checking
  6. Submit a pull request

License

MIT License - see LICENSE file for details.

Release files for semantic-comparer 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for semantic-comparer 0.1.8
File Size Uploaded
semantic_comparer-0.1.8.tar.gz 66.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for semantic-comparer 0.1.8
File Interpreter ABI Platform
semantic_comparer-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 77.9 kB

Release files / semantic_comparer-0.1.8.tar.gz

Download URL semantic_comparer-0.1.8.tar.gz
Size 66.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a4cdf6d7c89d8743775af5f40ec3ffa0049f38480f64f4dec811e9f76263c5ed
BLAKE2b-256 checksum
How to use checksums
da63a2ff48c13904d54c08e1258cf6ffbd58b73b5e08e892b659f61a47193875
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.8.0

Release files / semantic_comparer-0.1.8-py3-none-any.whl

Download URL semantic_comparer-0.1.8-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03d0553c8261c55749466d6f1de5e90912a401eed3ca281478e1ed6fd695499a
BLAKE2b-256 checksum
How to use checksums
9b69d6c1ac299460060920ee8867703f23cfb8c63021784fc2f61eafe09b98a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.8.0

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page