|
from sentence_transformers import SentenceTransformer, util |
|
|
|
|
|
model = SentenceTransformer('all-MiniLM-L6-v2') |
|
|
|
def check_plagiarism(text1, text2, threshold=0.8): |
|
""" |
|
Compare two texts and return similarity score and verdict. |
|
|
|
Parameters: |
|
text1 (str): First text (e.g., student answer) |
|
text2 (str): Second text (e.g., source material) |
|
threshold (float): Similarity threshold (default = 0.8) |
|
|
|
Returns: |
|
result (str): Similarity percentage and plagiarism verdict. |
|
""" |
|
try: |
|
embedding1 = model.encode(text1, convert_to_tensor=True) |
|
embedding2 = model.encode(text2, convert_to_tensor=True) |
|
|
|
similarity_score = util.cos_sim(embedding1, embedding2).item() |
|
similarity_percent = round(similarity_score * 100, 2) |
|
|
|
if similarity_score >= threshold: |
|
verdict = "⚠️ Potential Plagiarism" |
|
else: |
|
verdict = "✅ No Significant Plagiarism Detected" |
|
|
|
return f"Similarity: {similarity_percent}%\nResult: {verdict}" |
|
except Exception as e: |
|
return f"Error during plagiarism check: {str(e)}" |
|
|