plagiat.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """
  2. https://www.cyberforum.ru/python-graphics/thread2712141.html
  3. """
  4. import os
  5. from difflib import SequenceMatcher
  6. from tqdm import tqdm
  7. # директория файла
  8. BASE_DIR = os.path.abspath(os.path.dirname(__file__))
  9. print()
  10. files_paths = []
  11. dirs = os.listdir(BASE_DIR)
  12. for dir in dirs:
  13. dir_path = os.path.join(BASE_DIR, dir)
  14. if os.path.isdir(dir_path) and (dir != "__pycache__"):
  15. files = os.listdir(dir_path)
  16. for file in files:
  17. file_path = os.path.join(BASE_DIR, dir, file)
  18. filename, fileext = os.path.splitext(file)
  19. if os.path.isfile(file_path) and (fileext=='.md'):
  20. files_paths.append(file_path)
  21. print(f"Всего файлов: {len(files_paths)}")
  22. for file_1 in tqdm(files_paths):
  23. for file_2 in files_paths:
  24. if (file_1 != file_2):
  25. try:
  26. with open(file_1, encoding="utf-8") as f_1:
  27. str1 = f_1.read()
  28. except:
  29. with open(file_1, encoding="cp1251") as f_1:
  30. str1 = f_1.read()
  31. f_1.close()
  32. with open(file_1, 'w', encoding="utf-8") as f_1:
  33. f_1.write(str1)
  34. f_1.close()
  35. try:
  36. with open(file_2, encoding="utf-8") as f_2:
  37. str2 = f_2.read()
  38. except:
  39. print()
  40. print(file_2)
  41. print("Неверная кодировка")
  42. with open(file_2, encoding="cp1251") as f_2:
  43. str2 = f_2.read()
  44. f_2.close()
  45. with open(file_2, 'w', encoding="utf-8") as f_2:
  46. f_2.write(str2)
  47. f_2.close()
  48. ratio = int(SequenceMatcher(None, str1.lower(), str2.lower()).ratio() * 100)
  49. if (ratio > 70):
  50. print(file_2)
  51. print(file_1)
  52. print(f"ratio = {ratio}")
  53. print()
  54. print("success")