Amazing-Python-Scripts
39 строк · 1.5 Кб
1
2# OS Module for loading paths of textfiles. TfidfVectorizer to perform word embedding on the textual data and cosine similarity to compute the plagiarism.
3import os
4from sklearn.feature_extraction.text import TfidfVectorizer
5from sklearn.metrics.pairwise import cosine_similarity
6student_files = [doc for doc in os.listdir() if doc.endswith('.txt')]
7student_notes = [open(File).read() for File in student_files]
8# Two lambda functions, one to convert the text to arrays of numbers and the other one to compute the similarity between them.
9
10
11def vectorize(Text): return TfidfVectorizer().fit_transform(Text).toarray()
12
13
14def similarity(doc1, doc2): return cosine_similarity([doc1, doc2])
15
16
17# Vectorize the Textual Data
18vectors = vectorize(student_notes)
19s_vectors = list(zip(student_files, vectors))
20
21# computing the similarity among students
22
23
24def check_plagiarism():
25plagiarism_results = set()
26global s_vectors
27for student_a, text_vector_a in s_vectors:
28new_vectors = s_vectors.copy()
29current_index = new_vectors.index((student_a, text_vector_a))
30del new_vectors[current_index]
31for student_b, text_vector_b in new_vectors:
32sim_score = similarity(text_vector_a, text_vector_b)[0][1]
33student_pair = sorted((student_a, student_b))
34score = (student_pair[0], student_pair[1], sim_score)
35plagiarism_results.add(score)
36return plagiarism_results
37
38
39for data in check_plagiarism():
40print(data)
41