Skip to main content

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, Java, and C++ source code analysis.

Technologies Used

  • Python: The core programming language for the tool.
  • ANTLR: A parser generator for creating parse trees from source code.
  • zss: A library for calculating the tree edit distance.
  • apted: A library for computing the tree edit distance, alternatively to zss.
  • 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.

  1. Clone the repository:
    git clone https://github.com/EdsonEddy/csim.git
    
  2. Navigate to the project directory:
    cd csim
    
  3. 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 two main actions: report (for pairwise similarity analysis) and group (for clustering similar files). The tool supports Python, Java, and C++ 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). Options: python, java, cpp
  • --talg, -ta: Tree edit distance algorithm (default: zss). Options: zss, apted

Example with options:

csim report --path /path/to/directory --lang java --talg apted

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). Options: python, java, cpp
  • --talg, -ta: Tree edit distance algorithm (default: zss). Options: zss, apted

Complete example:

csim group --path /path/to/directory --threshold 0.9 --strategy exhaustive --lang python --talg apted

Language Support

The tool supports the following programming languages:

Python:

csim report --path /path/to/python/files --lang python

Java:

csim report --path /path/to/java/files --lang java

C++:

csim report --path /path/to/cpp/files --lang cpp

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",
    threshold=0.8,
    ted_algorithm="zss"
)

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

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:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/new-feature).
  3. Make your changes and commit them (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature/new-feature).
  5. Open a Pull Request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

References

For more information on the techniques and tools used in this project, refer to the following resources:

Third-Party Licenses

This project utilizes the following third-party libraries:

ANTLR (ANother Tool for Language Recognition)

ANTLR4-parser-for-Python-3.14 by RobEin

zss (Zhang-Shasha)

apted (All Path Tree Edit Distance)

Release files for csim 2.0.5

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

Source distribution (sdist)

Source distribution for csim 2.0.5
File Size Uploaded
csim-2.0.5.tar.gz 1.4 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for csim 2.0.5
File Interpreter ABI Platform
csim-2.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 1.6 MB

Release files / csim-2.0.5.tar.gz

Download URL csim-2.0.5.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
4b9a16ce345aa9fcdf3a955f6e46ab6a6aae01bbb2fdc457adc8187d435018f3
BLAKE2b-256 checksum
How to use checksums
d67c89cad93598e22537565688dc311d7b12a707f02304495d7e8686d823f02b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / csim-2.0.5-py3-none-any.whl

Download URL csim-2.0.5-py3-none-any.whl
Size 269.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56c0ab6787a040bd4fc814886f0b77b44d788e66665f62ccad70ced32bb9f28a
BLAKE2b-256 checksum
How to use checksums
8cafc2ef1ed4fc869beb073b2f2e95fa5e7b5d66b391649dc86f373e842f73f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

3.4.0

3 release files

3.3.0

3 release files

3.2.0

3 release files

3.1.1

3 release files

3.1.0

3 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.0.6

2 release files

This release

2.0.5 This release

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.7.0

2 release files

1.6.3

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

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