Esempio n. 1
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        best_cnt = 0
        best_perm = ""
        for perm in (get_permutations(VOWELS_LOWER) + get_permutations(VOWELS_UPPER)):
            cnt = 0
            for word in self.apply_transpose(self.build_transpose_dict(perm)).split():
                if is_word(self.valid_words,word):
                    cnt += 1
            if cnt > best_cnt:
                best_cnt = cnt
                best_perm = perm
        if best_cnt == 0:
            return self.message_text
        else:
            return self.apply_transpose(self.build_transpose_dict(best_perm))
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        perm_list = get_permutations('aeiou')
        tranpose_dict_list = []
        decrypted_message_list = []
        perm_list = get_permutations('aeiou')

        new_dict = {}
        word_list = self.get_valid_words()

        # storing each permutation to the transposed dictionary list
        for perm in perm_list:
            tranpose_dict_list.append(self.build_transpose_dict(perm))

        # storing each decrypted message to the decrypted message list
        for dic in tranpose_dict_list:
            decrypted_message = self.apply_transpose(dic)
            decrypted_message_list.append(decrypted_message)

        for each_mes in decrypted_message_list:

            decrypted_words = each_mes.split()
            n = 0
            for word in decrypted_words:

                if is_word(word_list, word):
                    n += 1
                else:
                    continue

            new_dict[each_mes] = n

        # the next 3 lines of code just determines the key which corresponding to the
        # maximum of 'n' in the new_dict(dictionary)
        key_list = list(new_dict.keys())
        val_list = list(new_dict.values())
        best_decrypted_message = key_list[val_list.index(max(
            new_dict.values()))]
        ##################################################################

        return best_decrypted_message
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        all_list = get_permutations('aeiou')
        maxcountwords = 0
        key = []
        for transpose_dict in all_list:
            number_of_real_words = 0
            enc_dict = self.build_transpose_dict(transpose_dict)
            decrypted_message = self.apply_transpose(enc_dict)
            decrypted_message = decrypted_message.split(' ')
            for singleword in decrypted_message:
                if is_word(self.valid_words, singleword):
                    number_of_real_words += 1
            if number_of_real_words > maxcountwords:
                maxcountwords = number_of_real_words
                key = transpose_dict
        enc_dict = self.build_transpose_dict(key)
        return self.apply_transpose(enc_dict)
Esempio n. 4
0
    def decrypt_message(self):
        #punc = string.punctuation
        #s = list(self.message_text)
        #words = ''.join([o for o in s if not o in punc]).split()
        vowel_lower = 'aeoiu'
        vowel_upper = 'AEOIU'
        permutation = get_permutations('aeoiu')
        (subs_text, max_num) = ('', 0)

        for seq in permutation:
            valid_num = 0
            dic = self.build_transpose_dict(seq)
            de_text = ''

            for letter in self.message_text:
                if letter in vowel_lower or letter in vowel_upper:
                    new_letter = dic[letter]
                else:
                    new_letter = letter
                de_text += new_letter

            s = list(de_text)
            punc = string.punctuation
            words = ''.join([o for o in s if not o in punc]).split()

            for word in words:
                if is_word(self.valid_words, word):
                    valid_num += 1
            if valid_num > max_num:
                max_num = valid_num
                subs_text = de_text
        if max_num == 0:
            return self.message_text
        else:
            return subs_text
Esempio n. 5
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        valid_count = {}
        perm_list = get_permutations('aeiou')
        for seq in perm_list:
            count = 0
            d = self.build_transpose_dict(seq)
            new_message = self.apply_transpose(d)
            for word in new_message.split(' '):
                if is_word(self.valid_words, word):
                    count += 1
            valid_count[seq] = count
        for a, b in valid_count.items():
            if b == max(valid_count.values()) and b != 0:
                return self.apply_transpose(self.build_transpose_dict(a))
        return self.message_text
Esempio n. 6
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        list_decrypted = []
        list_word_count = []
        real_word_count = 0
        perm_vowels = get_permutations(VOWELS_LOWER)
        for a in perm_vowels:
            transpose_dict = self.build_transpose_dict(a)
            transformed = self.get_message_text().apply_transpose(
                transpose_dict)
            list_decrypted.append(transformed)
            for b in transformed.split():
                real_word_count += 1 if is_word(self.valid_words, b) else 0
                list_word_count.append((b, real_word_count))
            real_word_count = 0
        best_answer = sorted(list_word_count, key=lambda tup: tup[1])[-1]
        return list_decrypted[best_answer[1] - 1]
Esempio n. 7
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        best_perms = ''
        perms = get_permutations(VOWELS_LOWER)
        word_n = 0
        for perm in perms:
            new_message = self.apply_transpose(self.build_transpose_dict(perm))
            n = 0
            for word in new_message.split(' '):
                if is_word(self.valid_words, word):
                    n += 1
            if n > word_n:
                word_n = n
                best_perms = new_message
        return best_perms
Esempio n. 8
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        permutation_list = get_permutations(VOWELS_LOWER)
        best_count = 0
        for list_option in permutation_list:
            count = 0
            transpose_dictionary = self.build_transpose_dict(list_option)
            decrypted_message = self.apply_transpose(transpose_dictionary)
            for word in decrypted_message.split(" "):
                if is_word(self.valid_words, word):
                    count += 1
            if count >= best_count:
                best_count = count
                best_permutation = list_option
        if best_count > 0:
            return self.apply_transpose(
                self.build_transpose_dict(best_permutation))
        else:
            return self.get_message_text()
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        max_valid_words = 1
        decrypted_message = ""
        for permutation in get_permutations('aeiou'):
            valid_words = 0
            dict_map = SubMessage.build_transpose_dict(self, permutation)
            decrypt_message = SubMessage.apply_transpose(self, dict_map)
            words = decrypt_message.split()
            for i in words:
                if is_word(self.valid_words, i):
                    valid_words += 1
            if valid_words > max_valid_words:
                max_valid_words = valid_words
                decrypted_message = decrypt_message
        return decrypted_message
Esempio n. 10
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        max_num_words = 0
        vowel_permutations = get_permutations('aeiou')
        for single_permutation in vowel_permutations:
            num_words = 0
            transpose_dict = self.build_transpose_dict(single_permutation)
            decrypted_message = self.apply_transpose(transpose_dict)

            for word in decrypted_message.split(' '):
                if is_word(self.valid_words, word) == True:
                    num_words = num_words + 1
                #If number of valid words is higher than previous max, store shift value and message
                if num_words > max_num_words:
                    max_num_words = num_words
                    best_decryption = decrypted_message

        return best_decryption
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        best = ["", 0]
        for perm in get_permutations("aeiou"):
            wordCount = 0
            dict = self.build_transpose_dict(perm)
            tempMessage = self.apply_transpose(dict).split(' ')
            for word in tempMessage:
                if is_word(self.get_valid_words(), word):
                    wordCount += 1
            if wordCount > best[1]:
                best[0] = ' '.join(tempMessage)
                best[1] = wordCount
        if best[1] == 0:
            return self.get_message_text()
        else:
            return best[0]
Esempio n. 12
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        max = [0, 0]
        possible_permutations = get_permutations(VOWELS_LOWER)
        for i in range(len(possible_permutations)):
            count = 0
            possible_decrypted_message = self.apply_transpose(
                self.build_transpose_dict(possible_permutations[i]))
            list_of_decrypted_words = possible_decrypted_message.split(" ")
            for word in list_of_decrypted_words:
                if (is_word(self.valid_words, word)):
                    count += 1
            if (count > max[0]):
                max = [count, i]
        return self.apply_transpose(
            self.build_transpose_dict(possible_permutations[max[1]]))
Esempio n. 13
0
    def decrypt_message(self):
        """
        Attempt to decrypt the encrypted message

        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.

        If no good permutations are found (i.e. no permutations result in
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message

        Hint: use your function from Part 4A
        """
        decrypt_dict = {}
        word_list = load_words(WORDLIST_FILENAME)
        for perm in get_permutations("aeiou"):
            vword = 0
            trans_dict = self.build_transpose_dict(perm)
            encrypt_msg = self.apply_transpose(trans_dict)
            for word in encrypt_msg.split():
                if is_word(word_list, word):
                    vword += 1
            decrypt_dict[vword] = encrypt_msg

        return (decrypt_dict[max(decrypt_dict.keys())]
                if max(decrypt_dict.keys()) >= 1 else self.get_message_text)
Esempio n. 14
0
def get_permutations(sequence):
    '''
    Enumerate all permutations of a given string

    sequence (string): an arbitrary string to permute. Assume that it is a
    non-empty string.  

    You MUST use recursion for this part. Non-recursive solutions will not be
    accepted.

    Returns: a list of all permutations of sequence

    Example:
    >>> get_permutations('abc')
    ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']

    Note: depending on your implementation, you may return the permutations in
    a different order than what is listed here.
    '''
    if len(sequence) == 1:
        return [sequence]
    first_letter = sequence[0]
    other_letters = sequence[1:]
    perm_other_letters = get_permutations(other_letters)

    perm = []
    for i in range(len(sequence)):
        for temp in perm_other_letters:
            new_perm = temp[:i] + first_letter + temp[i:]
            if new_perm not in perm:
                perm.append(new_perm)
    return perm
Esempio n. 15
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        vowels = 'aeiou'
        perms = get_permutations(vowels)
        valid_words = {}
        decry_message = {}

        for perm in perms:
            tranpose_dict = self.build_transpose_dict(perm)
            decrypted = self.apply_transpose(tranpose_dict)
            words = decrypted.split(' ')
            decry_message[perm] = decrypted
            for word in words:
                valid_words[perm] = valid_words.get(perm, 0) + is_word(
                    self.valid_words, word)
        best = max(valid_words, key=valid_words.get)
        return decry_message[best]
Esempio n. 16
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        permutations = get_permutations("aeiou")
        highest_word_count = 0
        decrypted_message = ""
        
        for permutation in permutations:
            decryption_attempt = self.apply_transpose(self.build_transpose_dict(permutation))
            decrypted_dict = decryption_attempt.split(" ")
            i = 0
            for word in decrypted_dict:
                if is_word(self.valid_words, word):
                    i += 1
            if i > highest_word_count:
                highest_word_count = i
                decrypted_message = "".join(decryption_attempt)
                
        return decrypted_message
Esempio n. 17
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        result_dict = {}
        for v_p in get_permutations(VOWELS_LOWER):
            trans_dict = self.build_transpose_dict(v_p)
            d_text = self.apply_transpose(trans_dict)
            text_word_list = d_text.split()
            num_word = 0
            for word in text_word_list:
                if is_word(self.valid_words, word):
                    num_word += 1
            result_dict[num_word] = v_p
        best_num_word = max(result_dict.keys())
        best_permutation = result_dict[best_num_word]
        if best_num_word == 0:
            return self.message_text
        else:
            return self.apply_transpose(self.build_transpose_dict(best_permutation))
Esempio n. 18
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''

        prev_max = 0
        res = ""
        for perm in get_permutations(VOWELS_LOWER):
            _sum = 0
            curr_text = self.apply_transpose(self.build_transpose_dict(perm))
            for word in curr_text.split():
                if is_word(self.get_valid_words(), word):
                    _sum += 1
            if _sum > prev_max:
                prev_max = _sum
                res = curr_text

        if max(_sum, prev_max) == 0:
            return self.get_message_text()

        return res
Esempio n. 19
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        permutations = map(self.build_transpose_dict,
                           get_permutations("aeiou"))
        decode = lambda p: self.apply_transpose(p)
        score = lambda text: sum(
            is_word(self.valid_words, word) for word in text.split())
        decodeAndScore = lambda p: score(decode(p))
        perm, decoded, score = max(
            ((p, decode(p), decodeAndScore(p)) for p in permutations),
            key=lambda x: x[2])
        print(decoded, score)
        return decoded
Esempio n. 20
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        permutations = get_permutations('aeiou')
        permutation_list = []
        for perms in permutations:
            lettercasedict = {}
            for letter in range(0, len(VOWELS_UPPER)):
                lettercasedict[VOWELS_UPPER[letter]] = perms[letter].upper()
            for letter in range(0, len(VOWELS_LOWER)):
                lettercasedict[VOWELS_LOWER[letter]] = perms[letter].lower()
            print(lettercasedict)
            encryptedmessage = ""
            for letter in self.message_text:
                encryptedmessage += lettercasedict.get(letter, " ")
            if encryptedmessage in self.valid_words:
                return encryptedmessage

        # check if the word is present
        return self.message_text
Esempio n. 21
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        max_valid_words_num = 0
        permutations_list = get_permutations(VOWELS_LOWER)
        for this_permutation in permutations_list:
            message_transposed = self.apply_transpose(
                self.build_transpose_dict(this_permutation))
            valid_words_num = 0
            for word in message_transposed.split(' '):
                if is_word(self.valid_words, word):
                    valid_words_num += 1
            if valid_words_num > max_valid_words_num:
                max_valid_words_num = valid_words_num
                message_decrypted = message_transposed
        return message_decrypted
Esempio n. 22
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        vowel_lists = get_permutations(VOWELS_LOWER)
        max_words = 0
        best_permutation = ""
        best_message = self.message_text
        for vowel_list in vowel_lists:
            curr_words = 0
            transposed_text = self.apply_transpose(
                self.build_transpose_dict(vowel_list))
            for word in transposed_text.split():
                if is_word(self.valid_words, word):
                    curr_words += 1
            if curr_words > max_words:
                max_words = curr_words
                best_permutation = vowel_list
                best_message = transposed_text
        return best_message
Esempio n. 23
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        most_en_words = 0
        most_decrypted_msg = self.get_message_text()
        for permutation in get_permutations(VOWELS_LOWER):
            transpose_dict = self.build_transpose_dict(permutation)
            decrypted_msg = self.apply_transpose(transpose_dict)
            current_en_words = 0
            for word in decrypted_msg.split():
                if is_word(self.get_valid_words(), word):
                    current_en_words += 1
            if current_en_words > most_en_words:
                most_en_words = current_en_words
                most_decrypted_msg = decrypted_msg

        return most_decrypted_msg
Esempio n. 24
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        best = [0, 0]
        word_list = self.get_valid_words()
        vowel_perms = get_permutations("aeiou")
        for perm in vowel_perms:
            v_dict = self.build_transpose_dict(perm)
            new_phrase = self.apply_transpose(v_dict)
            new_words = new_phrase.split()
            count = 0
            for word in new_words:
                if is_word(word_list, word):
                    count += 1
                if count > best[0]:
                    best = [count, new_phrase]

        return best[1]
Esempio n. 25
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message

        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.

        If no good permutations are found (i.e. no permutations result in
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message

        Hint: use your function from Part 4A
        '''
        max_num = 0
        best_perm = ''
        for i in get_permutations(VOWELS_LOWER):  # 遍历所有排列方式
            str = self.apply_transpose(self.build_transpose_dict(i))
            valid_num = 0
            list = str.split()
            for j in list:
                if is_word(self.get_valid_words(), j):
                    valid_num = valid_num + 1  # 计算单词合法数
            if valid_num > max_num:
                best_perm = i  # 记录最佳的
            max_num = max(max_num, valid_num)
        return self.apply_transpose(self.build_transpose_dict(best_perm))
Esempio n. 26
0
    def decrypt_message(self):
        # Variables to store decrypt result and permutation list
        best_score = 0
        best_permutation = ''
        permutation_list = get_permutations(VOWELS_LOWER)
        # print('Permutation list:', permutation_list, '| List length:', len(permutation_list))

        # Start going through the permutation list and decrypt
        for e in permutation_list:
            # Score tracking and get the iteration message
            itr_score = 0
            message = self.apply_transpose(self.build_transpose_dict(e)).split(" ")
            
            # Start scoring
            for char in message:
                if is_word(self.valid_words, char):
                    itr_score += 1

            # Update best score by far
            if itr_score > best_score:
                best_score = itr_score
                best_permutation = e

        # Now use best permutation found to decrypt the message, if found any
        if best_score:
            return self.apply_transpose(self.build_transpose_dict(best_permutation))

        # Else just keep the message as is
        else:
            return self.get_message_text()
Esempio n. 27
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.
        
        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''

        cipher_scan_results = {}
        vowel_permutations = get_permutations("aeiou")

        for vowel_permutation in vowel_permutations:
            deciphered_text = self.apply_transpose(
                self.build_transpose_dict(vowel_permutation))

            word_matches = []
            for word in deciphered_text.split():
                word_matches.append(is_word(self.valid_words, word))
            cipher_scan_results[sum(word_matches)] = deciphered_text

        if max(cipher_scan_results) == 0:
            return self.message_text
        else:
            return cipher_scan_results[max(cipher_scan_results)]
Esempio n. 28
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        vowels_permuation_list = get_permutations(VOWELS_LOWER)
        max_valid_words = 0
        decrypted_message_text = self.message_text
        for vowels_permutation in vowels_permuation_list:
            self.build_transpose_dict(vowels_permutation)
            text = self.apply_transpose(self.transpose_dict)

            num_valid_words = get_num_valid_words(text, self.valid_words)
            if max_valid_words < num_valid_words:
                max_valid_words = num_valid_words
                decrypted_message_text = text

        return decrypted_message_text
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message

        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.

        If no good permutations are found (i.e. no permutations result in
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message

        Hint: use your function from Part 4A
        '''
        vowels = 'aeiou'
        max_matched = 0
        for permutation in get_permutations(vowels):
            transpose_dict = SubMessage.build_transpose_dict(self, permutation)
            message_decrypted = SubMessage.apply_transpose(
                self, transpose_dict)
            message_decrypted_list = message_decrypted.split(' ')
            number_matched = 0
            for word in message_decrypted_list:
                if is_word(SubMessage.get_valid_words(self), word):
                    number_matched += 1
                if number_matched > max_matched:
                    max_matched = number_matched
                    message_decrypted_best = message_decrypted
                    print(message_decrypted_best)
                    """
Esempio n. 30
0
    def decrypt_message(self):
        '''
        Attempt to decrypt the encrypted message 
        
        Idea is to go through each permutation of the vowels and test it
        on the encrypted message. For each permutation, check how many
        words in the decrypted text are valid English words, and return
        the decrypted message with the most English words.
        
        If no good permutations are found (i.e. no permutations result in 
        at least 1 valid word), return the original string. If there are
        multiple permutations that yield the maximum number of words, return any
        one of them.

        Returns: the best decrypted message    
        
        Hint: use your function from Part 4A
        '''
        max_score = self.get_score(self.text)
        best_message = self.text
        for s in get_permutations(VOWELS_LOWER):
            decoded = self.apply_transpose(self.build_transpose_dict(s))
            score = self.get_score(decoded)
            if score > max_score:
                max_score = score
                best_message = decoded
        return best_message