async def dictionary_attack(self, cipher_text): """Schedules the sub-tasks to perform a dictionary attack on the given cipher-text.""" keys = deque() candidate_key = CandidateKey(cipher_text, {}, {}, self.frequencies) tasks = self.dictionary_attack_tasks(candidate_key, keys) while len(tasks) > 0: new_tasks = set() for task in asyncio.as_completed(tasks): sub_tasks = await task new_tasks = new_tasks.union(sub_tasks) tasks = new_tasks return Scorer(self.dictionary, self.frequencies).best_key([candidate_key] + list(keys))
def exam(data: list, src_lang: str, dest_lang: str, random_test: bool = False) -> None: print( f"Let's start with a {src_lang.upper()} to {dest_lang.upper()} test!") scorer = Scorer() for elem in data: if random_test and random() >= 0.5: _lang = src_lang src_lang = dest_lang dest_lang = _lang try: ### put this into a function os.system('clear') src_word = elem[src_lang] dest_word = elem[dest_lang] comment = elem.get('comment', str()) answer = str() scorer.print_stats() first_attempt = True while answer != dest_word: answer = input( f"\n------\nTranslate {src_word if not comment else f'{src_word} ({comment})'} from {src_lang.upper()} to {dest_lang.upper()}:\n\n" ).strip() if answer != dest_word: print('\nBad guess, m**********r') if first_attempt: scorer.failure() first_attempt = False if first_attempt: scorer.success() print('\n') except KeyError: print(f"{elem} doesn't have the appropriate keys!") scorer.print_final_score()
def __init__(self): self.root = TrieNode() self.scorer = Scorer()