Multi-language code duplicate detector using semantic embeddings and Tree-Sitter parsing
Project description
Code Duplicates CLI โ Version 3
Multi-language code duplicate detector using semantic embeddings, Tree-Sitter parsing, parallelism, minimum size filtering, configurable exclusions, and HTML reports with syntax highlighting and JSON output.
โจ Features
- Multi-language support:
py, js, ts, tsx, jsx, go, rs (rust), c, cpp, java(extensible) - AST extraction with
tree-sitter-languages(functions, methods, classes) - Semantic embeddings with
SentenceTransformers(default:sentence-transformers/all-mpnet-base-v2) - Fast similarity search with
faiss(Inner Product + L2 normalization) - Parallel processing for snippet extraction and embedding generation
- Smart caching of embeddings by file content hash
- Configurable exclusions for folders and files (node_modules, .git, etc.)
- Minimum block size filtering (by lines)
- Rich HTML reports with Pygments syntax highlighting + JSON export
- Configurable similarity threshold and top-k results per snippet
- Verbose mode and dry-run capabilities
๐ Installation
python -m venv .venv
source .venv/bin/activate # (Windows: .venv\Scripts\activate)
pip install -r requirements.txt
Note:
tree-sitter-languagesincludes pre-compiled grammars; no manual compilation required.
๐ Usage
Basic Usage
# Simple scan with defaults
python cli.py ./path/to/project
# Scan current directory
python cli.py .
Advanced Usage
python cli.py ./path/to/project \
--languages py,ts,tsx,js,go \
--threshold 0.85 \
--min-lines 5 \
--top-k 10 \
--report-html reports/duplicates.html \
--report-json reports/duplicates.json \
--model sentence-transformers/all-mpnet-base-v2 \
--cache-dir .cache_embeddings \
--exclusions-file .codeduplicates-ignore \
--verbose
Command Line Options
Options:
--languages TEXT Comma-separated list of file extensions (default: py,js,ts,tsx,jsx,go,rs,c,cpp,java)
--threshold FLOAT Similarity threshold (0.0-1.0, default: 0.85)
--min-lines INTEGER Minimum lines per code block (default: 3)
--top-k INTEGER Max similar snippets per block (default: 5)
--report-html PATH HTML report output path
--report-json PATH JSON report output path
--model TEXT Embedding model name (default: sentence-transformers/all-mpnet-base-v2)
--cache-dir PATH Directory for embedding cache
--exclusions-file PATH Custom exclusions file (default: .codeduplicates-ignore)
--verbose Enable verbose output
--dry-run Show what would be processed without running
--help Show this message and exit
๐ซ Exclusions
The CLI includes default exclusions for common folders like node_modules, .git, __pycache__, etc.
Create a custom exclusions file:
# .codeduplicates-ignore
node_modules
.git
dist
build
*.min.js
my-generated-folder
test-data
venv
.venv
__pycache__
.pytest_cache
coverage
.coverage
Supports glob patterns (*, ?, []) and comments with #.
๐ Examples
Language-Specific Scans
# Scan TypeScript/JavaScript with high threshold
python cli.py . --languages ts,tsx,js --threshold 0.92 --report-html reports/js-duplicates.html
# Python and Go only, minimum 5 lines, top-3 results
python cli.py src --languages py,go --min-lines 5 --top-k 3 --report-json reports/py-go-duplicates.json
# Rust projects with custom exclusions
python cli.py . --languages rs --exclusions-file rust-exclusions.txt --verbose
Performance Optimization
# Large codebase with caching
python cli.py . --cache-dir .embeddings-cache --min-lines 10 --threshold 0.80
# Quick scan with dry-run
python cli.py . --dry-run --verbose
๐๏ธ Project Structure
code-duplicates-cli-v3/
โโโ cli.py # Main CLI entry point
โโโ core/
โ โโโ config.py # Configuration constants
โ โโโ utils.py # Utility functions
โ โโโ embeddings.py # Embedding generation engine
โ โโโ similarity.py # Similarity calculation engine
โ โโโ report.py # HTML/JSON report generation
โ โโโ extractors/
โ โโโ base_extractor.py # Base extractor interface
โ โโโ parser_manager.py # Tree-sitter parser management
โ โโโ code_extractor.py # AST-based code extraction
โ โโโ regex_extractor.py # Regex-based extraction
โ โโโ simple_extractor.py # Simple text extraction
โโโ ARCHITECTURE.md # Detailed architecture documentation
โโโ MODEL_ANALYSIS.md # Embedding model analysis
โโโ requirements.txt # Python dependencies
๐ฏ Model Information
Current Default Model
- Model:
sentence-transformers/all-mpnet-base-v2 - Type: Semantic similarity model based on MPNet
- Dimensions: 768
- Strengths: Better semantic understanding, fewer false positives
- Use Case: General-purpose semantic similarity with good code understanding
Alternative Models
# Code-specific models
python cli.py . --model microsoft/codebert-base
python cli.py . --model microsoft/graphcodebert-base
# Faster alternatives
python cli.py . --model sentence-transformers/all-MiniLM-L6-v2
python cli.py . --model sentence-transformers/paraphrase-MiniLM-L6-v2
โก Performance Tips
For Large Repositories
- Use caching:
--cache-dir .embeddings-cacheto avoid recomputing embeddings - Increase minimum lines:
--min-lines 10to reduce noise from trivial blocks - Adjust threshold:
--threshold 0.80for broader matches,0.90+for strict matches - Limit top-k:
--top-k 3for faster processing
Memory Optimization
- Smaller models: Use
all-MiniLM-L6-v2for faster inference - Batch processing: The tool automatically handles large codebases efficiently
- Exclusions: Use comprehensive exclusion files to skip irrelevant directories
๐ Troubleshooting
Common Issues
- High false positives: Increase
--thresholdto 0.90 or higher - Missing duplicates: Decrease
--thresholdto 0.70-0.80 - Too many results: Increase
--min-linesor use stricter exclusions - Slow performance: Use
--cache-dirand consider a smaller model
Debug Mode
# Verbose output for debugging
python cli.py . --verbose --dry-run
# Test specific file types
python cli.py test-project --languages py --verbose
๐ Output Formats
HTML Report
- Syntax highlighting with Pygments
- Side-by-side comparison of duplicate code blocks
- Similarity scores and file locations
- Interactive navigation between duplicates
JSON Report
- Structured data for programmatic processing
- Complete metadata including file paths, line numbers, similarity scores
- Easy integration with CI/CD pipelines and other tools
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
๐ 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 code_duplicates_cli-3.0.0.tar.gz.
File metadata
- Download URL: code_duplicates_cli-3.0.0.tar.gz
- Upload date:
- Size: 18.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40f2dd933082b7e810a4cfa591c530e7be5927423d88ddd5a629cc3bae5ea65b
|
|
| MD5 |
61b9bc30b5e8754295d83cbd4d12b21c
|
|
| BLAKE2b-256 |
b0e3a37fe79272268241544d6ecc52ad5d8ecaab2545612c0e6996573029679b
|
File details
Details for the file code_duplicates_cli-3.0.0-py3-none-any.whl.
File metadata
- Download URL: code_duplicates_cli-3.0.0-py3-none-any.whl
- Upload date:
- Size: 18.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d2a03f84f7bf93db1d9a7270dee23d65c47ef4a9b3262fc08c0c83964b25990
|
|
| MD5 |
e4c062fae2b192a43eb1d04e03de48e0
|
|
| BLAKE2b-256 |
7e25444c0bf302fc8fe5a59199752be334fd874c4a05148366aba009d19c4453
|