Code Similarity (csim)
Code Similarity (csim) provide a module designed to detect similarities between source code files, even when obfuscation techniques have been applied. It is particularly useful for programming instructors and students who need to verify code originality.
Key Features
- Source Code Similarity Analysis: Compares source code files to determine their degree of similarity.
- Pairwise Reporting: Generate detailed similarity reports for all file pairs.
- File Grouping: Cluster similar files into groups based on a configurable threshold.
- Flexible Search Strategies:
- Exhaustive Search: All-pairs comparison for maximum precision
- Advanced Analysis: Utilizes parse trees and the tree edit distance algorithm for in-depth analysis.
- Parse Trees: Represents the syntactic structure of source code, enabling detailed comparisons.
- Tree Edit Distance: Measures the similarity between different code structures.
- Hash-Based Pruning: Optimizes the comparison process by reducing tree size while preserving essential structure.
- Multi-Language Support: Supports Python 3.13, Python 3 (universal grammar), Java 20, Java 24, and C++14 source code analysis.
Technologies Used
- Python: The core programming language for the tool.
- ANTLR: A parser generator for creating parse trees from source code.
- apted: A library for computing the tree edit distance (default algorithm).
- zss: A library for calculating the tree edit distance, alternatively to apted.
- NumPy: Used for efficient numerical operations.
Installation
For the installation pip is required, you can either clone the repository and install it locally or install it directly from PyPI.
- Clone the repository:
git clone https://github.com/EdsonEddy/csim.git
- Navigate to the project directory:
cd csim
- Install the package:
pip install .
Alternatively, you can install it directly from PyPI:
pip install csim
Version Compatibility
- Python: 3.10–3.12 (recommended 3.11)
- ANTLR4 Python Runtime: 4.13.2
- zss: 1.2.0
- apted: 1.0.3
- numpy: 1.26.4
Quick Start
New to csim? Start here: GETTING_STARTED.md
For detailed information about search strategies, see: docs/STRATEGIES.md
csim supports three main actions: report (for pairwise similarity analysis), group (for clustering similar files), and tree/view (for visualizing a file's normalized/pruned parse tree). The tool supports Python 3.13, Java 20, Java 24, and C++14 source code files.
General Command Structure
csim <action> --path <directory> [options]
Action 1: report - Generate Similarity Report
Generates a pairwise similarity report comparing all files in a directory.
csim report --path /path/to/directory
Example Output:
file1.py is similar to file2.py with similarity index: 0.95
file1.py is similar to file3.py with similarity index: 0.45
file2.py is similar to file3.py with similarity index: 0.50
Options:
--lang, -l: Programming language (default:python_3_13). Options:python_3_13,python_3,java_20,java_24,cpp_14--talg, -ta: Tree edit distance algorithm (default:apted). Options:zss,apted
Example with options:
csim report --path /path/to/directory --lang java_20 --talg zss
Action 2: group - Group Files by Similarity
Groups files by similarity using a specified threshold and strategy.
csim group --path /path/to/directory --threshold 0.8
Example Output:
Threshold: 0.8
Total files processed: 4
Group 1 (Average Similarity: 0.98):
./file1.py
./file2.py
Group 2 (Average Similarity: 0.95):
./file3.py
./file4.py
Strategy Options
The group action supports two strategies for finding similar files:
1. exhaustive (Default)
Compares every file against every other file (O(n²)). This is the most thorough approach but slower for large datasets.
csim group --path /path/to/directory --threshold 0.8 --strategy exhaustive
When to use each:
- exhaustive: Small datasets (< 100 files), when maximum precision is critical
Group Action Options
--threshold, -t: Similarity threshold (0.0 to 1.0). Required.--strategy, -s: Grouping strategy (default:exhaustive). Options:exhaustive--lang, -l: Programming language (default:python_3_13). Options:python_3_13,python_3,java_20,java_24,cpp_14--talg, -ta: Tree edit distance algorithm (default:apted). Options:zss,apted
Complete example:
csim group --path /path/to/directory --threshold 0.9 --strategy exhaustive --lang python_3_13 --talg zss
Action 3: tree (alias: view) - Visualize Parse Trees
Prints the normalized/pruned tree for a single file — the exact tree that gets passed to the tree edit distance algorithm. Useful for debugging how the normalization, collapsing, and hashing rules affect a specific file before it's compared against others.
csim tree --path /path/to/file.py --lang python_3_13
Example Output:
=== Normalized + Pruned Tree (input to Tree Edit Distance) ===
statements
function_def_raw
param [hashed:e3b0c442]
statements
STRING
if_stmt
comparison [hashed:93e10dca]
return_stmt [hashed:337adaa9]
assignment [hashed:118045cc]
primary [hashed:e1b0c7ab]
Total nodes after pruning: 24
Rule and token names are resolved for readability, LOOP marks nodes collapsed under control-flow equivalence (e.g. for/while), and [hashed:xxxxxxxx] marks subtrees that were hashed into a single node instead of compared structurally.
Options:
--path, -p: Path to a single source code file (required).--lang, -l: Programming language (default:python_3_13). Options:python_3_13,python_3,java_20,java_24,cpp_14--show-raw: Also print the raw ANTLR parse tree before normalization/pruning, for side-by-side comparison.
Example with --show-raw:
csim tree --path /path/to/file.py --lang python_3_13 --show-raw
Language Support
The tool supports the following programming languages:
Python 3.13:
csim report --path /path/to/python/files --lang python_3_13
Python 3 (universal grammar):
csim report --path /path/to/python/files --lang python_3
Same .py files as python_3_13, parsed with grammars-v4's "universal Python 2/3"
grammar, which publishes a C++ target -- giving a large speedup once the native
parser is built (see Native Parsers below). Grouping output is
byte-identical to python_3_13 on most real-world code (verified against
hundreds of real judge submissions); a narrow, understood exception remains for
files with very few top-level statements, where tree-size differences can
slightly overstate similarity — see csim/python_3/utils.py for the full
writeup. Does not parse positional-only parameters (/, PEP 570), the walrus
operator (:=, PEP 572), or match/case (PEP 634); csim falls back to
python_3_13 automatically for files using those, so results stay correct,
just slower for that subset.
Java 20:
csim report --path /path/to/java/files --lang java_20
Java 24 (experimental):
csim report --path /path/to/java/files --lang java_24
Java 24 uses an optimized grammar (grammars-v4/java/java) that parses much faster
than java_20 when the native parser is available (see Native Parsers
below), and supports the same modern Java syntax (records, sealed classes, pattern
matching, switch expressions, text blocks). However, its similarity/grouping
output is not yet tuned to match java_20 and can be significantly less accurate
(verified on real submissions — see CHANGELOG.md). Use java_20 for group/report
until this is resolved; java_24 is available for parse-speed experimentation only.
C++14:
csim report --path /path/to/cpp/files --lang cpp_14
Native Parsers
For java_20, java_24, cpp_14, and python_3, csim can use a compiled C++
ANTLR parser instead of the pure-Python one, giving a large speedup with
identical (or, for java_24, not-yet-identical -- see above) output.
python_3_13 always uses the pure-Python parser (no C++ target is available for
that grammar).
Check which backend is active for each language:
csim info
If a native library isn't present for a language, csim falls back to the pure-Python parser automatically — results are unaffected, only speed. Build the native parsers from source with:
scripts/build_native_parsers.sh
Set CSIM_DISABLE_NATIVE=1 to force the pure-Python parsers for every
language, e.g. for debugging or benchmarking.
Threshold Guidance
The similarity threshold represents the structural similarity of the code (based on the Abstract Syntax Tree). Choose appropriate thresholds based on your use case:
- 0.95+: Nearly identical code (likely plagiarism)
- 0.85-0.95: Very similar code (probable plagiarism)
- 0.70-0.85: Moderately similar code (review recommended)
- <0.70: Low similarity (likely independent work)
Using csim as a Python Module
You can also use csim programmatically within your Python code. The library provides low-level functions for advanced use cases:
from csim.utils import group_by_exhaustive_search, report_pairwise_similarity
# Example: Group files by similarity
file_names = ["file1.py", "file2.py", "file3.py"]
file_contents = [code1, code2, code3]
results = group_by_exhaustive_search(
file_names=file_names,
file_contents=file_contents,
lang="python_3_13",
threshold=0.8,
ted_algorithm="apted"
)
print(results)
Or use the legacy Compare class for simple pairwise comparisons:
from csim import Compare
code_a = "a = 5"
code_b = "c = 50"
similarity = Compare(name_a='example A', content_a=code_a, name_b='example B', content_b=code_b)
print(f"Similarity: {similarity}") # Output: Similarity: X.XX
Documentation
- Getting Started Guide - Quick tutorial for new users
- Search Strategies Guide - Detailed explanation of available search strategies
- ANTLR Parser Generation - For grammar customization
ANTLR4 Installation and Parser/Lexer Generation
This installation is not required—the generated files are already included in the project. If you'd like to review the steps to generate them yourself, see grammars/parser_gen_guide.md.
Note: The included generated files were produced by ANTLR 4.13.2 and are compatible with the pinned runtime listed above.
Contributing
Contributions are welcome! To contribute, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/new-feature). - Make your changes and commit them (
git commit -am 'Add new feature'). - Push to the branch (
git push origin feature/new-feature). - Open a Pull Request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Support
- Questions? Open a GitHub Discussion
- Found a bug? File a GitHub Issue
- Want to contribute? See Contributing section
References
For more information on the techniques and tools used in this project, refer to the following resources:
- ANTLR
- Parse Tree (Wikipedia)
- Tree Edit Distance (Wikipedia)
- Locality Sensitive Hashing (Wikipedia)
- MinHash (Wikipedia)
- zss (PyPI)
- Hashing (Python Docs)
- apted (GitHub)
Third-Party Licenses
This project utilizes the following third-party libraries:
ANTLR (ANother Tool for Language Recognition)
- Purpose: A parser generator used to create parse trees from source code.
- License: BSD 3-Clause
- Website: https://www.antlr.org/
- Repository: https://github.com/antlr/antlr4
ANTLR4-parser-for-Python-3.14 by RobEin
- Purpose: Python 3.14 grammar for ANTLR4
- License: MIT License
- Repository: https://github.com/RobEin/ANTLR4-parser-for-Python-3.14
zss (Zhang-Shasha)
- Purpose: Tree edit distance algorithm implementation for comparing tree structures
- License: MIT License
- Repository: https://github.com/timtadh/zhang-shasha
apted (All Path Tree Edit Distance)
- Purpose: Python APTED algorithm for the Tree Edit Distance, an alternative to zss
- License: MIT License
- Repository: https://github.com/JoaoFelipe/apted
Release files for csim 3.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| csim-3.2.0.tar.gz | 459.9 kB | Details |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| csim-3.2.0-py3-none-manylinux_2_28_x86_64.whl | Python 3 | none | Linux glibc 2.28+ x86-64 | Details |
| csim-3.2.0-py3-none-macosx_14_0_arm64.whl | Python 3 | none | macOS 14.0+ ARM64 | Details |
Total release size: 8.1 MB
Release files / csim-3.2.0.tar.gz
| Download URL | csim-3.2.0.tar.gz |
|---|---|
| Size | 459.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3416b4c9ed8b85ecd0cfafb0f574caae0dc8ece751b99db1bb5643b35dc4c46a
|
|
BLAKE2b-256 checksum How to use checksums |
9b93310d57ad1ca366a9b3be11cb5f793025e469e58f698ae87a7bd595d6930c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.
Transparency logRelease files / csim-3.2.0-py3-none-manylinux_2_28_x86_64.whl
| Download URL | csim-3.2.0-py3-none-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 5.5 MB |
| Tags | Linux glibc 2.28+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
7b0d8f818c6a508bf3bb650b6d2d342776bf715eae2cff550cd01174032b0ebd
|
|
BLAKE2b-256 checksum How to use checksums |
94f5b58e88bf5a42e3beb35b1341926dc4197a7fe1552c3e48503d79b59931e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.
Transparency logRelease files / csim-3.2.0-py3-none-macosx_14_0_arm64.whl
| Download URL | csim-3.2.0-py3-none-macosx_14_0_arm64.whl |
|---|---|
| Size | 2.1 MB |
| Tags | Python 3 macOS 14.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
5f0ddfd5aa03216dc8650e1a7edc32b8601a37e30c2ca4b66a6a846f3daf1e23
|
|
BLAKE2b-256 checksum How to use checksums |
7d7c660034b68a0c7254107576bfcca8e8aac62724bfa67a473105ee4d5454f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.
Transparency log