Amazing-Python-Scripts

Форк
0
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.
3
import os
4
from sklearn.feature_extraction.text import TfidfVectorizer
5
from sklearn.metrics.pairwise import cosine_similarity
6
student_files = [doc for doc in os.listdir() if doc.endswith('.txt')]
7
student_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

11
def vectorize(Text): return TfidfVectorizer().fit_transform(Text).toarray()
12

13

14
def similarity(doc1, doc2): return cosine_similarity([doc1, doc2])
15

16

17
# Vectorize the Textual Data
18
vectors = vectorize(student_notes)
19
s_vectors = list(zip(student_files, vectors))
20

21
# computing the similarity among students
22

23

24
def check_plagiarism():
25
    plagiarism_results = set()
26
    global s_vectors
27
    for student_a, text_vector_a in s_vectors:
28
        new_vectors = s_vectors.copy()
29
        current_index = new_vectors.index((student_a, text_vector_a))
30
        del new_vectors[current_index]
31
        for student_b, text_vector_b in new_vectors:
32
            sim_score = similarity(text_vector_a, text_vector_b)[0][1]
33
            student_pair = sorted((student_a, student_b))
34
            score = (student_pair[0], student_pair[1], sim_score)
35
            plagiarism_results.add(score)
36
    return plagiarism_results
37

38

39
for data in check_plagiarism():
40
    print(data)
41

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.