Advertisement
Intermediate Time: 2–3 weeks Computer Science

Code Plagiarism Detection System

Build a code plagiarism detector that analyzes AST structure, token sequences, and semantic similarity to detect copied code.

ASTToken AnalysisPythonNLPSimilarityEducation
DifficultyIntermediate
Duration2–3 weeks
Components10 items
Steps4 steps

Introduction

Build a code plagiarism detector that analyzes AST structure, token sequences, and semantic similarity to detect copied code. This comprehensive guide covers everything from design through implementation, testing, and deployment.

Theory & Background

Tokenize source code (remove whitespace/comments, convert identifiers to generic tokens: INT, ID, OP, etc.). Calculate Longest Common Subsequence (LCS) of token sequences. Similarity = 2 × LCS_length / (tokens_A + tokens_B). This is the core of JPlag and MOSS algorithms. Advantage: token normalization makes superficial changes (variable renaming, comment adding, whitespace changes) transparent — only structural changes matter.

Advertisement

Components & Requirements

10 components required for this project.

#ComponentPurposeQty
1Python 3.10+Implementation languagex1
2ast module (stdlib)Python AST parsingx1
3javalang (Java parser)Java code parsingx1
4sentence-transformersSemantic code embeddingsx1
5difflib (stdlib)Sequence similarity (SequenceMatcher)x1
6FlaskWeb interface for submissionsx1
7CodeBERT (HuggingFace)Code-aware embeddingsx1
8Matplotlib + NetworkXSimilarity visualization graphx1
9Celery + RedisAsync batch comparisonx1
10MongoDBSubmission and result storagex1

Step-by-Step Implementation

Follow these 4 steps carefully.

1
Token-Based Similarity (JPlag Method)

Tokenize source code (remove whitespace/comments, convert identifiers to generic tokens: INT, ID, OP, etc.). Calculate Longest Common Subsequence (LCS) of token sequences. Similarity = 2 × LCS_length / (tokens_A + tokens_B). This is the core of JPlag and MOSS algorithms. Advantage: token normalization makes superficial changes (variable renaming, comment adding, whitespace changes) transparent — only structural changes matter.

2
AST-Based Structural Analysis

Parse code to AST. Compare AST structures using tree edit distance: minimum operations (insert, delete, relabel node) to transform tree A into tree B. Lower edit distance = higher similarity. Hash AST subtrees using Merkle hashing — identical subtrees produce identical hashes. Count matching subtrees. Advantage over token comparison: captures code structure independent of textual representation.

3
Semantic Similarity with CodeBERT

CodeBERT is pre-trained on code-text pairs. Generate code embeddings: tokenize code, pass through CodeBERT, use [CLS] token embedding as code vector. Cosine similarity between code vectors measures semantic similarity. This captures algorithmic intent even when implementation differs significantly. Example: recursive and iterative factorial implementations are functionally equivalent — cosine similarity > 0.85 despite syntactic differences.

4
Multi-Technique Fusion

Combine all three techniques for robust detection: token similarity (40% weight), AST structural similarity (35% weight), semantic embedding similarity (25% weight). Final score = 0.4×token + 0.35×AST + 0.25×semantic. Flag pairs above threshold (0.7) for review. Present evidence: highlighted matching token sequences, highlighted matching AST subtrees, and a diff view showing structural changes between suspicious submissions.

Code & Implementation

Core code for plagiarism_detector.py:

plagiarism_detector.py Python

Testing & Troubleshooting

Test Code Plagiarism Detection System by verifying each subsystem individually before full integration.

!
Troubleshooting Tips

Verify power voltages, check ground connections, use serial monitor for debug.

Real-World Applications

*Academic integrity enforcement in courses
*Open-source license violation detection
*Code review similarity alerts in enterprise
*Software patent prior art analysis
*CTF (Capture The Flag) plagiarism detection
*Homework grading automation
*Competitive programming submission fairness
*Code review plagiarism in competitive submissions

Extensions & Next Steps

  • Add cross-language plagiarism detection (Python vs Java)
  • Implement incremental detection for large repositories
  • Build a visualization showing exactly which code blocks match
  • Add API for integration with LMS (Moodle, Canvas)
  • Implement watermarking to track code lineage

Interactive Playground

Coming Soon

An interactive simulator will be available here — simulate circuits and run code in-browser without hardware.

Frequently Asked Questions

How do students commonly try to evade plagiarism detectors?
Common obfuscation techniques: variable renaming (token normalization defeats this), adding/removing blank lines and comments (defeated by tokenization), reordering independent code blocks (detected by AST analysis), converting between loops (for ↔ while — AST handles this partially), translating between languages (CodeBERT semantic embeddings partially detect this), inserting dead code (token density analysis detects padding), and using AI to paraphrase code (hardest to detect — semantic embeddings help).
Advertisement