Matheel 0.1.0__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.
Potentially problematic release.
This version of Matheel might be problematic. Click here for more details.
- Matheel/__init__.py +1 -0
- Matheel/cli.py +21 -0
- Matheel/similarity.py +61 -0
- matheel-0.1.0.dist-info/METADATA +16 -0
- matheel-0.1.0.dist-info/RECORD +8 -0
- matheel-0.1.0.dist-info/WHEEL +5 -0
- matheel-0.1.0.dist-info/entry_points.txt +2 -0
- matheel-0.1.0.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,21 @@
|
|
|
1
|
+
import click
|
|
2
|
+
from .similarity import get_sim_list
|
|
3
|
+
|
|
4
|
+
@click.command()
|
|
5
|
+
@click.argument('zipfile', type=click.Path(exists=True))
|
|
6
|
+
@click.option('--ws', default=0.7, help='Semantic Similarity Weight')
|
|
7
|
+
@click.option('--wl', default=0.3, help='Levenshtein Distance Weight')
|
|
8
|
+
@click.option('--wj', default=0.0, help='Jaro-Winkler Distance Weight')
|
|
9
|
+
@click.option('--model', default='uclanlp/plbart-java-cs', help='Sentence Transformer Model')
|
|
10
|
+
@click.option('--pooling', default='mean', type=click.Choice(['mean', 'cls', 'max']), help='Pooling Type')
|
|
11
|
+
@click.option('--threshold', default=0.0, help='Similarity Threshold')
|
|
12
|
+
@click.option('--num', default=10, help='Number of Results to Display')
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def main(zipfile, ws, wl, wj, model, pooling, threshold, num):
|
|
16
|
+
"""Matheel CLI - Compute Code Similarity"""
|
|
17
|
+
results = get_sim_list(zipfile, ws, wl, wj, model, pooling, threshold, num)
|
|
18
|
+
click.echo(results.to_string(index=False))
|
|
19
|
+
|
|
20
|
+
if __name__ == "__main__":
|
|
21
|
+
main()
|
Matheel/similarity.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
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, pooling_type: str):
|
|
8
|
+
transformer = models.Transformer(model_name)
|
|
9
|
+
pooling = models.Pooling(
|
|
10
|
+
transformer.get_word_embedding_dimension(),
|
|
11
|
+
pooling_mode=pooling_type
|
|
12
|
+
)
|
|
13
|
+
model = SentenceTransformer(modules=[transformer, pooling])
|
|
14
|
+
return model
|
|
15
|
+
|
|
16
|
+
def extract_and_read_compressed_file(file_path):
|
|
17
|
+
with zipfile.ZipFile(file_path, 'r') as z:
|
|
18
|
+
file_names = z.namelist()
|
|
19
|
+
codes = [z.read(file).decode('utf-8', errors='ignore') for file in file_names]
|
|
20
|
+
return file_names, codes
|
|
21
|
+
|
|
22
|
+
def paraphrase_mining_with_combined_score(
|
|
23
|
+
model,
|
|
24
|
+
sentences: List[str],
|
|
25
|
+
weight_semantic: float,
|
|
26
|
+
weight_levenshtein: float,
|
|
27
|
+
weight_jaro_winkler: float,
|
|
28
|
+
):
|
|
29
|
+
embeddings = model.encode(sentences, convert_to_tensor=True)
|
|
30
|
+
paraphrases = util.paraphrase_mining_embeddings(embeddings, score_function=util.cos_sim)
|
|
31
|
+
|
|
32
|
+
results = []
|
|
33
|
+
for score, i, j in paraphrases:
|
|
34
|
+
lev_ratio = Levenshtein.normalized_similarity(sentences[i], sentences[j])
|
|
35
|
+
jw_ratio = JaroWinkler.normalized_similarity(sentences[i], sentences[j])
|
|
36
|
+
combined_score = (
|
|
37
|
+
weight_semantic * score +
|
|
38
|
+
weight_levenshtein * lev_ratio +
|
|
39
|
+
weight_jaro_winkler * jw_ratio
|
|
40
|
+
)
|
|
41
|
+
results.append((combined_score, i, j))
|
|
42
|
+
return sorted(results, reverse=True)
|
|
43
|
+
|
|
44
|
+
def get_sim_list(
|
|
45
|
+
zipped_file, Ws, Wl, Wj, model_name, pooling_type, threshold, number_results
|
|
46
|
+
):
|
|
47
|
+
file_names, codes = extract_and_read_compressed_file(zipped_file)
|
|
48
|
+
model = load_model(model_name, pooling_type)
|
|
49
|
+
code_pairs = paraphrase_mining_with_combined_score(model, codes, Ws, Wl, Wj)
|
|
50
|
+
|
|
51
|
+
pairs_results = [
|
|
52
|
+
{
|
|
53
|
+
'file_name_1': file_names[i],
|
|
54
|
+
'file_name_2': file_names[j],
|
|
55
|
+
'similarity_score': round(score, 2)
|
|
56
|
+
}
|
|
57
|
+
for score, i, j in code_pairs if score >= threshold
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
similarity_df = pd.DataFrame(pairs_results)
|
|
61
|
+
return similarity_df.head(number_results)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: Matheel
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Matheel: A CLI and Python package for source-code similarity detection.
|
|
5
|
+
Home-page: https://github.com/FahadEbrahim/Matheel
|
|
6
|
+
Author: Fahad Ebrahim
|
|
7
|
+
License: CC BY-NC 4.0
|
|
8
|
+
Project-URL: Source, https://github.com/FahadEbrahim/Matheel
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: click
|
|
11
|
+
Requires-Dist: gradio
|
|
12
|
+
Requires-Dist: pandas
|
|
13
|
+
Requires-Dist: numpy
|
|
14
|
+
Requires-Dist: rapidfuzz
|
|
15
|
+
Requires-Dist: sentence-transformers
|
|
16
|
+
Requires-Dist: sentencepiece
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Matheel/__init__.py,sha256=IGTxSHmW4GkSnkMH04o2MZurh1kNNKOxy5cpPKn6nX8,37
|
|
2
|
+
Matheel/cli.py,sha256=jE6hVceQzffDjb3O8uXZ0JKyq04wZ-2bDMvWhWL6fss,961
|
|
3
|
+
Matheel/similarity.py,sha256=_Hf9zqMpwkg33CPVkBFj_H0J6z0kWqrqgd6nmwq2UdU,2175
|
|
4
|
+
matheel-0.1.0.dist-info/METADATA,sha256=gkRsUw4jXq3gucuSNy6nvOlQNEOPbs3wU6r6boPyhSM,505
|
|
5
|
+
matheel-0.1.0.dist-info/WHEEL,sha256=1tXe9gY0PYatrMPMDd6jXqjfpz_B-Wqm32CPfRC58XU,91
|
|
6
|
+
matheel-0.1.0.dist-info/entry_points.txt,sha256=OW_yRMODxmEzkhbRTiGHQ81o0v-LjAZfJVygzNDJWQE,45
|
|
7
|
+
matheel-0.1.0.dist-info/top_level.txt,sha256=0v2lVa2HxwF1IOHrCCrMdyiNDHpCXfRmm_2PxG8voSo,8
|
|
8
|
+
matheel-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Matheel
|