Esempio n. 1
0
 def test_stripped_text_properties(self):
     stripped_text1 = alphabetic.StrippedText("LET US GO! IT IS TIME. ",
                                              alphabetic.Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
     self.assertEqual(stripped_text1.ends_of_words, {2: 0, 4: 1, 6: 2, 8: 3, 10: 4, 14: 5})
     self.assertEqual(stripped_text1.stripped_part, [" ", " ", "! ", " ", " ", ". ", ""])
     self.assertEqual(stripped_text1.get_non_stripped_text(), "LET US GO! IT IS TIME. ")
     self.assertEqual(stripped_text1.positions["L"], set([0]))
     self.assertEqual(stripped_text1.positions["E"], set([1, 14]))
     stripped_text2 = alphabetic.StrippedText("  LET US GO! IT IS TIME",
                                              alphabetic.Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
     self.assertEqual(stripped_text2.ends_of_words, {-1: 0, 2: 1, 4: 2, 6: 3, 8: 4, 10: 5, 14: 6})
     self.assertEqual(stripped_text2.stripped_part, ["  ", " ", " ", "! ", " ", " ", ""])
     self.assertEqual(stripped_text2.get_non_stripped_text(), "  LET US GO! IT IS TIME")
     stripped_text2[3] = "F"
     self.assertEqual(stripped_text2[3], "F")
     self.assertEqual(stripped_text2.positions["F"], set([3]))
Esempio n. 2
0
def generate_log_distribution_from_file(filename, alphabet, n_gram_length, additional_chars=""):
    new_alphabet = alphabetic.Alphabet(alphabet.complete_alphabet + list(additional_chars))
    f = open(filename, "r")
    lines = f.readlines()
    freqs = {}
    for i in lines:
        freqs[i.split()[0]] = log(float(i.split()[1]))

    for i in alphabetic.n_gram_dict(new_alphabet, n_gram_length):
        if i not in freqs:
            freqs[i] = 0

    return freqs
Esempio n. 3
0
 def setUp(self):
     self.alphabet = alphabetic.Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Esempio n. 4
0
                swap_permutation_and_update_decryption(
                    current_decryption, current_guessed_encoding_key, swap)
                common.update_frequency(current_frequencies, frequency_change)
                current_dist += dist_change
                aux = current_order[swapp[0]]
                current_order[swapp[0]] = current_order[swapp[1]]
                current_order[swapp[1]] = aux

                if current_dist > max_dist:
                    max_dist = current_dist
                    max_state = copy.deepcopy(current_guessed_decoding_key)
                    print(current_decryption.get_non_stripped_text())
    return get_reverse_permutation(alphabet, max_state)


alphabett = alphabetic.Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
plain = alphabetic.StrippedText(
    data_generator.get_string_cleared(
        data_generator.generate_random_excerpt("../data/1984.txt",
                                               2000)).upper(), alphabett)

standard2 = data_generator.get_log_distribution_from_json(
    "../data/bigram_log_distributions_uppercase.json")
standard1 = data_generator.get_log_distribution_from_json(
    "../data/monogram_log_distributions_uppercase.json")

all_printable = data_generator.get_string_cleared(string.printable + " ")
upper_printable = all_printable[:10] + all_printable[36:]
complete_alphabet = alphabetic.Alphabet(
    data_generator.get_string_cleared(string.printable + " "))
upper_alphabet = alphabetic.Alphabet(upper_printable)
Esempio n. 5
0
 def setUp(self):
     self.alphabet = alphabetic.Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
     self.coprimes = extended.get_coprimes(self.alphabet.length)
Esempio n. 6
0
 def test_operators(self):
     self.assertEqual(alphabetic.Alphabet("ABC")[1], "B")
     self.assertTrue("B" in alphabetic.Alphabet("ABC"))
     self.assertFalse("D" in alphabetic.Alphabet("ABC"))