Matheel 0.1.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- matheel/__init__.py +1 -0
- matheel/cli.py +20 -0
- matheel/similarity.py +65 -0
- matheel-0.1.6.dist-info/METADATA +115 -0
- matheel-0.1.6.dist-info/RECORD +8 -0
- matheel-0.1.6.dist-info/WHEEL +5 -0
- matheel-0.1.6.dist-info/entry_points.txt +2 -0
- matheel-0.1.6.dist-info/top_level.txt +1 -0
matheel/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .similarity import get_sim_list
|
matheel/cli.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import click
|
|
2
|
+
from .similarity import get_sim_list
|
|
3
|
+
|
|
4
|
+
@click.group()
|
|
5
|
+
def main():
|
|
6
|
+
"""Matheel CLI - Compute Code Similarity"""
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
@main.command()
|
|
10
|
+
@click.argument('zipfile', type=click.Path(exists=True))
|
|
11
|
+
@click.option('--ws', default=0.7, help='Semantic Similarity Weight')
|
|
12
|
+
@click.option('--wl', default=0.3, help='Levenshtein Distance Weight')
|
|
13
|
+
@click.option('--wj', default=0.0, help='Jaro-Winkler Distance Weight')
|
|
14
|
+
@click.option('--model', default='uclanlp/plbart-java-cs', help='Sentence Transformer Model')
|
|
15
|
+
@click.option('--threshold', default=0.0, help='Similarity Threshold')
|
|
16
|
+
@click.option('--num', default=10, help='Number of Results to Display')
|
|
17
|
+
def compare(zipfile, ws, wl, wj, model, pooling, threshold, num):
|
|
18
|
+
"""Bulk similarity from ZIP file"""
|
|
19
|
+
results = get_sim_list(zipfile, ws, wl, wj, model, threshold, num)
|
|
20
|
+
click.echo(results.to_string(index=False))
|
matheel/similarity.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import zipfile
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from rapidfuzz.distance import Levenshtein, JaroWinkler
|
|
4
|
+
from sentence_transformers import SentenceTransformer, util, models
|
|
5
|
+
from typing import List
|
|
6
|
+
|
|
7
|
+
def load_model(model_name: str):
|
|
8
|
+
model = SentenceTransformer(model_name)
|
|
9
|
+
return model
|
|
10
|
+
|
|
11
|
+
def extract_and_read_compressed_file(file_path):
|
|
12
|
+
with zipfile.ZipFile(file_path, 'r') as z:
|
|
13
|
+
file_names = z.namelist()
|
|
14
|
+
codes = [z.read(file).decode('utf-8', errors='ignore') for file in file_names]
|
|
15
|
+
return file_names, codes
|
|
16
|
+
|
|
17
|
+
def paraphrase_mining_with_combined_score(
|
|
18
|
+
model,
|
|
19
|
+
sentences: List[str],
|
|
20
|
+
weight_semantic: float,
|
|
21
|
+
weight_levenshtein: float,
|
|
22
|
+
weight_jaro_winkler: float,
|
|
23
|
+
):
|
|
24
|
+
embeddings = model.encode(sentences, convert_to_tensor=True)
|
|
25
|
+
paraphrases = util.paraphrase_mining_embeddings(embeddings, score_function=util.cos_sim)
|
|
26
|
+
|
|
27
|
+
results = []
|
|
28
|
+
for score, i, j in paraphrases:
|
|
29
|
+
lev_ratio = Levenshtein.normalized_similarity(sentences[i], sentences[j])
|
|
30
|
+
jw_ratio = JaroWinkler.normalized_similarity(sentences[i], sentences[j])
|
|
31
|
+
combined_score = (
|
|
32
|
+
weight_semantic * score +
|
|
33
|
+
weight_levenshtein * lev_ratio +
|
|
34
|
+
weight_jaro_winkler * jw_ratio
|
|
35
|
+
)
|
|
36
|
+
results.append((combined_score, i, j))
|
|
37
|
+
return sorted(results, reverse=True)
|
|
38
|
+
|
|
39
|
+
def get_sim_list(zipped_file, Ws, Wl, Wj, model_name, threshold, number_results):
|
|
40
|
+
file_names, codes = extract_and_read_compressed_file(zipped_file)
|
|
41
|
+
model = load_model(model_name)
|
|
42
|
+
code_pairs = paraphrase_mining_with_combined_score(model, codes, Ws, Wl, Wj)
|
|
43
|
+
|
|
44
|
+
pairs_results = [
|
|
45
|
+
{
|
|
46
|
+
'file_name_1': file_names[i],
|
|
47
|
+
'file_name_2': file_names[j],
|
|
48
|
+
'similarity_score': round(score, 2)
|
|
49
|
+
}
|
|
50
|
+
for score, i, j in code_pairs if score >= threshold
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
similarity_df = pd.DataFrame(pairs_results)
|
|
54
|
+
return similarity_df.head(number_results)
|
|
55
|
+
|
|
56
|
+
def calculate_similarity(code1, code2, Ws, Wl, Wj, model_name):
|
|
57
|
+
model = load_model(model_name)
|
|
58
|
+
embedding1 = model.encode(code1)
|
|
59
|
+
embedding2 = model.encode(code2)
|
|
60
|
+
sim_similarity = util.cos_sim(embedding1, embedding2).item()
|
|
61
|
+
lev_ratio = Levenshtein.normalized_similarity(code1, code2)
|
|
62
|
+
jaro_winkler_ratio = JaroWinkler.normalized_similarity(code1, code2)
|
|
63
|
+
overall_similarity = Ws * sim_similarity + Wl * lev_ratio + Wj * jaro_winkler_ratio
|
|
64
|
+
|
|
65
|
+
return overall_similarity
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: matheel
|
|
3
|
+
Version: 0.1.6
|
|
4
|
+
Summary: Matheel: A CLI and Python package for source-code similarity detection.
|
|
5
|
+
Author: Fahad Ebrahim
|
|
6
|
+
License-Expression: CC-BY-NC-4.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/FahadEbrahim/matheel
|
|
8
|
+
Project-URL: Source, https://github.com/FahadEbrahim/matheel
|
|
9
|
+
Requires-Python: >=3.9
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
Requires-Dist: click
|
|
12
|
+
Requires-Dist: pandas
|
|
13
|
+
Requires-Dist: numpy
|
|
14
|
+
Requires-Dist: rapidfuzz
|
|
15
|
+
Requires-Dist: sentence-transformers
|
|
16
|
+
Requires-Dist: sentencepiece
|
|
17
|
+
|
|
18
|
+
This is the repository for the demonstration paper "Matheel: A Hybrid Source Code Plagiarism Detection Software".
|
|
19
|
+
|
|
20
|
+
# Matheel
|
|
21
|
+
|
|
22
|
+
Matheel is a Python package designed to detect source code similarity. It integrates semantic similarity models with traditional edit distance, providing a robust approach to detecting similarities among source code snippets.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- **Semantic Similarity:** Uses Pre-Trained models.
|
|
29
|
+
- **Edit-distance Metrics:** Integrates Levenshtein and Jaro-Winkler similarity scores.
|
|
30
|
+
- **Combined Weighted Similarity:** Adjustable weights for semantic and syntactic similarity.
|
|
31
|
+
- **Easy CLI & Python API:** Suitable for both interactive and automated workflows.
|
|
32
|
+
- **Interactive UI:** Gradio-based user interface.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
Install via pip:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install matheel
|
|
42
|
+
```
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
### CLI Usage
|
|
48
|
+
|
|
49
|
+
Compare files within a compressed ZIP archive:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
matheel compare codes.zip --model buelfhood/unixcoder-base-unimodal-ST --threshold 0.5 --num 100
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Python API Usage
|
|
56
|
+
|
|
57
|
+
To calculate similarities programmatically:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from matheel.similarity import get_sim_list
|
|
61
|
+
|
|
62
|
+
# Define parameters
|
|
63
|
+
zip_file = "sample_codes.zip"
|
|
64
|
+
Ws, Wl, Wj = 0.7, 0.2, 0.1
|
|
65
|
+
model_name = "buelfhood/unixcoder-base-unimodal-ST"
|
|
66
|
+
threshold = 0.5
|
|
67
|
+
number_results = 100
|
|
68
|
+
|
|
69
|
+
# Get similarity results
|
|
70
|
+
results = get_sim_list(zip_file, Ws, Wl, Wj, model_name, threshold, number_results)
|
|
71
|
+
|
|
72
|
+
# Display results
|
|
73
|
+
print(results)
|
|
74
|
+
```
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Gradio GUI:
|
|
78
|
+
|
|
79
|
+
The gradio_app folder contains a notebook that allows you to run the Gradio through a Jupyter Notebook.
|
|
80
|
+
Also, a demo is available hosted on [Huggingface Spaces](https://huggingface.co/spaces/buelfhood/Matheel).
|
|
81
|
+
|
|
82
|
+
## Using Gradio API:
|
|
83
|
+
The tool can be used through the Gradio API as per the following call:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
#pip install gradio_client
|
|
87
|
+
from gradio_client import Client, handle_file
|
|
88
|
+
|
|
89
|
+
client = Client("buelfhood/Matheel")
|
|
90
|
+
result = client.predict(
|
|
91
|
+
zipped_file=handle_file('zip file path'),
|
|
92
|
+
Ws=0.7,
|
|
93
|
+
Wl=0.3,
|
|
94
|
+
Wj=0,
|
|
95
|
+
model_name="buelfhood/unixcoder-base-unimodal-ST",
|
|
96
|
+
threshold=0,
|
|
97
|
+
number_results=10,
|
|
98
|
+
api_name="/get_sim_list"
|
|
99
|
+
)
|
|
100
|
+
print(result)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
This project is licensed under the [Creative Commons Attribution-NonCommercial 4.0 International License](https://creativecommons.org/licenses/by-nc/4.0/).
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## Acknowledgement:
|
|
112
|
+
|
|
113
|
+
- The demo uses code written by SBERT. [Webpage](https://www.sbert.net/index.html), [Repo](https://github.com/UKPLab/sentence-transformers).
|
|
114
|
+
- The code is built with Gradio. [Webpage](https://www.gradio.app/). [Repo](https://github.com/gradio-app/gradio)
|
|
115
|
+
- The code uses RapidFuzz for the edit distance. [Webpage](https://rapidfuzz.github.io/RapidFuzz/). [Repo](https://github.com/rapidfuzz/RapidFuzz)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
matheel/__init__.py,sha256=IGTxSHmW4GkSnkMH04o2MZurh1kNNKOxy5cpPKn6nX8,37
|
|
2
|
+
matheel/cli.py,sha256=9_4NvwtT2NNPpj3AAI9uacuzz3QOedhiZTUAZ5nRQbU,883
|
|
3
|
+
matheel/similarity.py,sha256=EQaG2Jd62U1l272CTz0M_b4MMuNeko7jfHxIdROuCrc,2427
|
|
4
|
+
matheel-0.1.6.dist-info/METADATA,sha256=RgsRTeRonMupprqetCoiwa3zJ2dHinxsqQhc0WMFOrU,3250
|
|
5
|
+
matheel-0.1.6.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
|
6
|
+
matheel-0.1.6.dist-info/entry_points.txt,sha256=M7AqiEw50rIO2f0FO92lfj3X-9B782xadpJJG1TDA7g,45
|
|
7
|
+
matheel-0.1.6.dist-info/top_level.txt,sha256=ZROsCC8faA3Fo-N8a4Z9anPQ_zm7g0mAssKGoPcoPgY,8
|
|
8
|
+
matheel-0.1.6.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
matheel
|