plagiat_full.v2.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # взаимная проверка всех файлов в указанной папке
  2. import os
  3. from difflib import SequenceMatcher
  4. from tqdm import tqdm
  5. import datetime
  6. import requests
  7. # download stopwords corpus, you need to run it once
  8. import nltk
  9. #nltk.download("stopwords")
  10. from nltk.corpus import stopwords
  11. import pymorphy2
  12. from string import punctuation
  13. from thefuzz import fuzz
  14. # ------------------------------- НАСТРОЙКИ ------------
  15. # директория файла (на уровень выше, для структуры репозиториев 2 сем. 2022-23)
  16. BASE_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
  17. # проверяемая директория
  18. LECTION_DIR = os.path.join(BASE_DIR, "EASvZI", "2022-23", "Самостоятельная_работа_1")
  19. # ------------------------------- / НАСТРОЙКИ ------------
  20. def log(str: str = None):
  21. global out_str
  22. if str == None:
  23. print()
  24. out_str += "\n"
  25. else:
  26. print(str)
  27. out_str += f"{str}\n"
  28. #Create lemmatizer and stopwords list
  29. morph = pymorphy2.MorphAnalyzer()
  30. russian_stopwords = stopwords.words("russian")
  31. #Preprocess function
  32. def preprocess_text(text):
  33. translator = str.maketrans(punctuation, ' '*len(punctuation))
  34. words = text.translate(translator)
  35. words = words.lower().split()
  36. # очистка от прилегающего к слову мусора (слово, "или так")
  37. clear_words = []
  38. for word in words:
  39. clear_word = ""
  40. for s in word:
  41. if not s in punctuation:
  42. clear_word = clear_word + s
  43. clear_words.append(clear_word)
  44. tokens = []
  45. tokens = [morph.parse(token)[0].normal_form for token in clear_words if token not in russian_stopwords\
  46. and token != " " \
  47. and token.strip() not in punctuation \
  48. ]
  49. text = " ".join(tokens)
  50. return tokens, text
  51. out_str = ""
  52. now = datetime.datetime.now().strftime('%d-%m-%Y %H:%M')
  53. log(f"Время проверки: {now}")
  54. files_paths = []
  55. files = os.listdir(LECTION_DIR)
  56. for file in files:
  57. file_path = os.path.join(LECTION_DIR, file)
  58. filename, fileext = os.path.splitext(file)
  59. if os.path.isfile(file_path) and (fileext=='.md'):
  60. files_paths.append(file_path)
  61. for file_1 in files_paths:
  62. for file_2 in files_paths:
  63. if (file_1 != file_2):
  64. small_filename_1 = str(file_1).replace(LECTION_DIR, "").strip("\\")
  65. small_filename_2 = str(file_2).replace(LECTION_DIR, "").strip("\\")
  66. try:
  67. with open(file_1, encoding="utf-8") as f_1:
  68. str1 = f_1.read()
  69. f_1.close()
  70. except:
  71. with open(file_1, encoding="cp1251") as f_1:
  72. str1 = f_1.read()
  73. f_1.close()
  74. try:
  75. with open(file_2, encoding="utf-8") as f_2:
  76. str2 = f_2.read()
  77. f_2.close()
  78. except:
  79. with open(file_2, encoding="cp1251") as f_2:
  80. str2 = f_2.read()
  81. f_2.close()
  82. str1_tokens, str1_uniq_text = preprocess_text(str1)
  83. str2_tokens, str2_uniq_text = preprocess_text(str2)
  84. # пересечение множеств
  85. min_tokens_len = min([len(set(str1_tokens)), len(set(str2_tokens))])
  86. c = list(set(str1_tokens) & set(str2_tokens))
  87. ratio = (1 - (len(c) / min_tokens_len)) * 100
  88. log(f"уникальность {small_filename_1} / {small_filename_2}: {ratio:.2f}%")
  89. log()
  90. with open(os.path.join(LECTION_DIR, "log.txt"), "w", encoding="utf-8") as f_log:
  91. f_log.write(out_str)
  92. f_log.close()