Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- from typing import Dict
- import copy
- def validate(word):
- return ''.join(i for i in word if i.isalpha())
- def match_words(words: list, reg: str) -> list:
- return [w for w in words if re.match(reg, w)]
- def count_words(text: str) -> tuple:
- words = [validate(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 count_chars(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 is_checkable(word: str, key: Dict) -> bool:
- return any(key[k] is not None and k in word for k in key)
- def get_unknown_chars(key: Dict):
- alph = 'абвгдеёжзийклмнопрстуфчцчшщъыьэюяңөү'
- return ''.join((c for c in alph if c not in key.values()))
- def get_unknown_keys(key: Dict) -> str:
- return ''.join(c for c in key if key[c] is None)
- def check_pattern(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 generate_pattern(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_unknown_keys(key)}]'
- print(reg)
- return reg + '$'
- def test():
- print("TEST")
- return "Bye!!!!"
- def main():
- encoded_text = open('D:/Codes/hwPy/teory/text.txt', '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 = count_chars(sample_text)
- counted_encoding_chars = count_chars(encoded_text)
- _, encoded_words = count_words(encoded_text)
- _, sample_words = count_words(sample_text)
- key = dict()
- for ec in counted_encoding_chars:
- key[ec[0]] = None
- key[counted_encoding_chars[0][0]] = 'а'
- counted_encoding_chars_dict = dict(counted_encoding_chars)
- counted_chars_dict = dict(counted_chars)
- print(counted_chars)
- print(counted_encoding_chars)
- unknown_chars = ''.join([c[0] for c in counted_chars[2:]])
- print(unknown_chars)
- print(key)
- # bias = 0.05
- must_words = ["баатыр", 'манас', 'каныкей']
- for word in must_words:
- matches = match_words(encoded_words, generate_pattern(word, key, counted_encoding_chars))
- print(matches)
- if len(matches) == 1:
- for i in range(len(word)):
- key[matches[0][i]] = word[i]
- while all(map(lambda x: x is not None, key.keys())):
- print(key)
- past_key = copy.deepcopy(key)
- examples = []
- for word in encoded_words:
- if is_checkable(word, key):
- reg = ''
- for s in word:
- if key.get(s, None) is None:
- reg += f'[{get_unknown_chars(key)}]'
- else:
- reg += key[s]
- reg += '$'
- # print(word, reg)
- matches = match_words(sample_words, reg)
- if word == 'лңк':
- print(matches, reg)
- matches = list(filter(lambda x: check_pattern(x, word), matches))
- # if matches:
- # print(matches)
- if len(matches) == 1 and any(map(lambda s: s not in key.values(), matches[0])):
- # print(matches[0])
- err = 0.0
- is_valid = True
- for i in range(len(matches[0])):
- if key[word[i]] is None:
- err += abs(counted_encoding_chars_dict[word[i]] * 1.0 / counted_chars_dict[matches[0][i]] - 1)
- if matches[0][i] in list(key.values()):
- is_valid = False
- break
- if is_valid:
- examples.append((word, matches[0], err/len(word)))
- if len(examples) == 0:
- break
- examples.sort(key=lambda x: x[2])
- for i in range(len(examples[0][0])):
- key[examples[0][0][i]] = examples[0][1][i]
- print(examples[0])
- 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
- # print(''.join([k for k in key.values() if k is not None]))
- # print(''.join([k for k in key.keys() if key[k] is not None]))
- print(text)
- # print(key)
- # print(sample_words)
- # print(list(key.values()).count(None))
- print(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement