Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog
- import re
- from typing import Dict
- import sys
- # Создаем корневое окно
- root = tk.Tk()
- root.title("Decoder")
- # root.geometry('600x500')
- # root.resizable(width=False, height=False)
- text1 = tk.Text(root)
- text1.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
- # Создаем полосу прокрутки
- scrollbar = tk.Scrollbar(root)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- # Привязываем полосу прокрутки к текстовому виджету
- text1.config(yscrollcommand=scrollbar.set)
- scrollbar.config(command=text1.yview)
- def create_symbols(s: str) -> list:
- s = s.lower()
- d = {}
- tot = 0
- for c in s:
- if c.isalpha() and c not in "quote":
- d[c] = d.get(c, 0) + 1
- tot += 1
- for k in d:
- d[k] /= tot * 0.01
- si = list(d.items())
- si.sort(key=lambda x: -x[1])
- return si
- def check_word(word):
- return ''.join(i for i in word if i.isalpha())
- def match_word(match: str, word: str) -> bool:
- return all(
- (match[i] == match[j]) == (word[i] == word[j]) for i in range(len(word) - 1) for j in range(i, len(word)))
- def count_words(text: str) -> tuple:
- words = [check_word(w) for w in text.lower().split()]
- word_dict = {}
- tot = 0
- for word in words:
- words_same_size = word_dict.setdefault(len(word), {})
- words_same_size[word] = words_same_size.get(word, 0) + 1
- tot += 1
- word_dict[len(word)] = words_same_size
- for k in word_dict:
- for j in word_dict[k]:
- word_dict[k][j] /= tot * 0.01
- for k in word_dict:
- si = list(word_dict[k].items())
- si.sort(key=lambda x: -x[1])
- word_dict[k] = si
- return word_dict, set(words)
- def find_words(words: list, reg: str) -> list:
- return [w for w in words if re.match(reg, w)]
- def check(word: str, key: Dict) -> bool:
- return any(key[k] is not None and k in word for k in key)
- def get_keys(key: Dict) -> str:
- return ''.join(c for c in key if key[c] is None)
- def get_chars(key: Dict):
- alph = 'абвгдеёжзийклмнопрстуфчцчшщъыьэюяңөү'
- return ''.join((c for c in alph if c not in key.values()))
- def create_pattern_signs(word, key, counted_encoding_chars):
- reg = ''
- for s in word:
- if s in key.values():
- reg += list(key.keys())[list(key.values()).index(s)]
- elif s in 'нкы':
- reg += f'[{"".join(c[0] for c in counted_encoding_chars[1:4])}]'
- else:
- reg += f'[{get_keys(key)}]'
- return reg + '$'
- def my_func():
- file_path = filedialog.askopenfilename(
- filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
- )
- if file_path:
- with open(file_path, "r", encoding='utf-8') as file:
- content = file.read()
- # Добавляем содержимое файла в текстовый виджет
- text1.delete("1.0", tk.END)
- text1.insert(tk.END, content)
- encoded_text = open(file_path, 'r', encoding='UTF-8').read()
- sample_text = open('D:/Codes/hwPy/teory/Manas-eposu-2010-S-Karalaev.txt', 'r', encoding='UTF-8').read().replace(
- 'www.el-sozduk.kg', '')
- counted_chars = create_symbols(sample_text)
- counted_encoding_chars = create_symbols(encoded_text)
- _, encoded_words = count_words(encoded_text)
- _, sample_words = count_words(sample_text)
- # инициализация словаря ключей
- key = {char[0]: None for char in counted_encoding_chars}
- key[counted_encoding_chars[0][0]] = 'а'
- counted_encoding_chars_dict = dict(counted_encoding_chars)
- counted_chars_dict = dict(counted_chars)
- # обновление словаря ключей на основе обязательных слов
- must_words = ["каныкей", 'деп', 'бейбак']
- for word in must_words:
- pattern = create_pattern_signs(word, key, counted_encoding_chars)
- matches = find_words(encoded_words, pattern)
- if len(matches) == 1:
- for i, char in enumerate(matches[0]):
- if key[char] is None:
- key[char] = word[i]
- # обновление словаря ключей на основе анализа закодированного текста
- while None in key.values():
- examples = []
- for word in encoded_words:
- if not check(word, key):
- continue
- pattern = ''.join([f'[{get_chars(key)}]' if key.get(s) is None else key[s] for s in word]) + '$'
- matches = [match for match in (find_words(sample_words, pattern)) if match_word(match, word)]
- matches = [match for match in matches if any([s not in key.values() for s in match])]
- if len(matches) == 1:
- err = sum(
- [abs(counted_encoding_chars_dict[word[i]] * 1.0 / counted_chars_dict[matches[0][i]] - 1) for i in
- range(len(matches[0])) if key[word[i]] is None]) / len(word)
- examples.append((word, matches[0], err))
- if not examples:
- break
- examples.sort(key=lambda x: x[2])
- for i, char in enumerate(examples[0][0]):
- if key[char] is None and examples[0][1][i] not in key.values():
- key[char] = examples[0][1][i]
- text = ''
- for w in encoded_text:
- w = w.lower()
- if w.isalpha():
- text += key[w] if key.get(w, None) is not None else 'X'
- else:
- text += w
- output_text.insert('end', f'{text}')
- # def create_text_widget(parent):
- # # create a text widget with vertical scrollbar
- # text_widget = tk.Text(parent)
- # scrollbar = tk.Scrollbar(parent)
- # scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- # text_widget.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
- # text_widget.config(yscrollcommand=scrollbar.set)
- # scrollbar.config(command=text_widget.yview)
- # return text_widget
- #
- #
- # def create_output_widget(parent):
- # # create a text widget with vertical scrollbar
- # output_widget = tk.Text(parent)
- # scrollbar = tk.Scrollbar(parent)
- # scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- # output_widget.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- # output_widget.config(yscrollcommand=scrollbar.set)
- # scrollbar.config(command=output_widget.yview)
- #
- # def decode_text():
- # sample_text = open('D:/Codes/hwPy/teory/Manas-eposu-2010-S-Karalaev.txt', 'r', encoding='UTF-8').read().replace(
- # 'www.el-sozduk.kg', '')
- #
- # counted_chars = create_symbols(sample_text)
- # counted_encoding_chars = create_symbols(encoded_text)
- #
- # _, encoded_words = count_words(encoded_text)
- # _, sample_words = count_words(sample_text)
- #
- # # инициализация словаря ключей
- # key = {char[0]: None for char in counted_encoding_chars}
- # key[counted_encoding_chars[0][0]] = 'а'
- #
- # counted_encoding_chars_dict = dict(counted_encoding_chars)
- # counted_chars_dict = dict(counted_chars)
- #
- # # обновление словаря ключей на основе обязательных слов
- # must_words = ["каныкей", 'деп', 'бейбак']
- # for word in must_words:
- # pattern = create_pattern_signs(word, key, counted_encoding_chars)
- # matches = find_words(encoded_words, pattern)
- # if len(matches) == 1:
- # for i, char in enumerate(matches[0]):
- # if key[char] is None:
- # key[char] = word[i]
- #
- # # обновление словаря ключей на основе анализа закодированного текста
- # while None in key.values():
- # examples = []
- # for word in encoded_words:
- # if not check(word, key):
- # continue
- # pattern = ''.join([f'[{get_chars(key)}]' if key.get(s) is None else key[s] for s in word]) + '$'
- # matches = [match for match in (find_words(sample_words, pattern)) if match_word(match, word)]
- # matches = [match for match in matches if any([s not in key.values() for s in match])]
- # if len(matches) == 1:
- # err = sum(
- # [abs(counted_encoding_chars_dict[word[i]] * 1.0 / counted_chars_dict[matches[0][i]] - 1) for i
- # in
- # range(len(matches[0])) if key[word[i]] is None]) / len(word)
- # examples.append((word, matches[0], err))
- #
- # if not examples:
- # break
- # examples.sort(key=lambda x: x[2])
- # for i, char in enumerate(examples[0][0]):
- # if key[char] is None and examples[0][1][i] not in key.values():
- # key[char] = examples[0][1][i]
- #
- # text = ''
- # for w in encoded_text:
- # w = w.lower()
- # if w.isalpha():
- # text += key[w] if key.get(w, None) is not None else 'X'
- # else:
- # text += w
- # return text
- #
- # def print_text():
- # text = text_widget.get("1.0", tk.END)
- # update_text(text)
- # def decode():
- # output_text.delete("1.0", tk.END)
- # text = text_widget.get("1.0", tk.END)
- # decoded_text = decode_text(text)
- # output_text.insert(tk.END, decoded_text)
- #
- # decode_button = tk.Button(parent, text="Decode", command=decode)
- # decode_button.pack()
- # output_text = create_text_widget(parent)
- # return output_text
- #
- # output_widget, update_output_text = create_output_widget(root)
- #
- # def update_text(text):
- # output_widget.delete("1.0", tk.END)
- # output_widget.insert(tk.END, text)
- #
- #
- #
- # decode_button = tk.Button(root, text="Decode", command=create_output_widget)
- # decode_button.pack()
- # text_widget = create_text_widget(root)
- def display_func():
- output = tk.Text(root, height=40, width=80)
- output.pack()
- my_func(output)
- # перенаправляем стандартный вывод в текстовый виджет
- sys.stdout = output
- # вызываем функцию my_func
- my_func()
- # возвращаем стандартный вывод на место
- sys.stdout = sys.__stdout__
- # текстовое поле для вывода
- output_text = tk.Text(root, height=20, width=80)
- output_text.pack()
- # добавляем кнопку для дешифровки
- decrypt_button = tk.Button(root, text="Choose your file", command=my_func)
- decrypt_button.pack(pady=10)
- # Запускаем главный цикл окна
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement