123456789101112131415161718192021222324252627282930313233343536 |
- #pip install -U scikit-learn
- #Make sure all the .txt files that need to be checked are in the same directory as the script
- import os
- from sklearn.feature_extraction.text import TfidfVectorizer
- from sklearn.metrics.pairwise import cosine_similarity
- user_files = [doc for doc in os.listdir() if doc.endswith('.txt')]
- user_notes = [open(_file, encoding='utf-8').read()
- for _file in user_files]
- def vectorize(Text): return TfidfVectorizer().fit_transform(Text).toarray()
- def similarity(doc1, doc2): return cosine_similarity([doc1, doc2])
- vectors = vectorize(user_notes)
- s_vectors = list(zip(user_files, vectors))
- plagiarism_results = set()
- def check_plagiarism():
- global s_vectors
- for student_a, text_vector_a in s_vectors:
- new_vectors = s_vectors.copy()
- current_index = new_vectors.index((student_a, text_vector_a))
- del new_vectors[current_index]
- for student_b, text_vector_b in new_vectors:
- sim_score = similarity(text_vector_a, text_vector_b)[0][1]
- student_pair = sorted((student_a, student_b))
- score = (student_pair[0], student_pair[1], sim_score)
- plagiarism_results.add(score)
- return plagiarism_results
- for data in check_plagiarism():
- print(data)
|