Exemplo n.º 1
0
def get_most_likely_values(pair_counts, input_data):
    """
    Utility for getting a short list of likely values
    currently used for debugging only
    :param pair_counts: pair counts, normalized
    :param ciphered_text:
    :return:
    """
    true_translation = true_translation_dictionary()
    top = 4
    most_likely = {}
    for ciphered_letter in alphabet:
        letter_list = []
        for deciphered_letter in alphabet:
            pair_key = (ciphered_letter, deciphered_letter)
            like = pair_counts[pair_key]
            occurrence = input_data['ciphered_text'].count(ciphered_letter)
            correct = true_translation[ciphered_letter] == deciphered_letter
            letter_list.append((deciphered_letter, like, occurrence, correct))
        letter_list = sorted(letter_list, key=lambda x: -x[1])[0:top]
        most_likely[ciphered_letter] = letter_list
    for ciphered_letter in alphabet:
        letter_list = most_likely[ciphered_letter]
        correct = [item[3] for item in letter_list]
        print ciphered_letter, correct, True in correct

    return most_likely
Exemplo n.º 2
0
def decipher_file():
    """
    Main pipeline to decipher the file
    :return:
    """
    input_data = get_input_data()
    word_data = get_word_data()

    translate = Translator()
    solve.get_paircounts_translation_iteratively(translate, input_data,
                                                 word_data)
    for iter in xrange(parameters['num_iterations_modify_letters']):
        solve.modify_each_letter(translate, input_data, word_data)

    logger.info('Final solution\n-------------------\n')
    true_translation = true_translation_dictionary()
    for k, v in translate.items():
        if k in input_data['ciphered_text']:
            logger.info("%s, %s, %s" % (k, v, (v == true_translation[k])))

    logger.debug("Finished decipher")
    return translate
Exemplo n.º 3
0
def decipher_file():
    """
    Main pipeline to decipher the file
    :return:
    """
    input_data = get_input_data()
    word_data = get_word_data()

    translate = Translator()
    solve.get_paircounts_translation_iteratively(
        translate, input_data, word_data)
    for iter in xrange(parameters['num_iterations_modify_letters']):
        solve.modify_each_letter(translate, input_data, word_data)

    logger.info('Final solution\n-------------------\n')
    true_translation = true_translation_dictionary()
    for k, v in translate.items():
        if k in input_data['ciphered_text']:
            logger.info("%s, %s, %s" % (k, v, (v == true_translation[k])))

    logger.debug("Finished decipher")
    return translate
Exemplo n.º 4
0
def get_maximum_likelihood_values(pair_counts, input_data):
    """
    Get the maximum likelihood values from the pair_counts
    for each letter
    :param pair_counts: pair counts, normalized
    :param input_data: dictionary holding the input data
    :return: dictionary of maximum likelihood values
    """
    true_translation = true_translation_dictionary()
    max_like = {}
    for ciphered_letter in alphabet:
        like_max = 0.0
        for deciphered_letter in alphabet:
            pair_key = (ciphered_letter, deciphered_letter)
            like = pair_counts[pair_key]
            if like > like_max:
                like_max = like
                occurrence = input_data['ciphered_text'].count(ciphered_letter)
                correct = true_translation[
                    ciphered_letter] == deciphered_letter
                max_like[ciphered_letter] = (
                    deciphered_letter, like, occurrence, correct)
    return max_like
Exemplo n.º 5
0
        solve.modify_each_letter(translate, input_data, word_data)

    logger.info('Final solution\n-------------------\n')
    true_translation = true_translation_dictionary()
    for k, v in translate.items():
        if k in input_data['ciphered_text']:
            logger.info("%s, %s, %s" % (k, v, (v == true_translation[k])))

    logger.debug("Finished decipher")
    return translate


if __name__ == "__main__":
    start = time.time()
    translate_solution = decipher_file()
    true_translation = true_translation_dictionary()
    ciphered_text = fileio.read_ciphered_text()
    success = True
    for letter in alphabet:
        if letter in ciphered_text:
            result = translate_solution(letter)
            expected = true_translation[letter]
            if result != expected:
                print "Incorrect: letter=%s, expected=%s, result=%s" \
                      % (letter, expected, result)
                success = False
    assert success
    fileio.write_solution(translate_solution)
    finish = time.time()
    runtime = finish - start
    logger.info("Success")
Exemplo n.º 6
0
    for iter in xrange(parameters['num_iterations_modify_letters']):
        solve.modify_each_letter(translate, input_data, word_data)

    logger.info('Final solution\n-------------------\n')
    true_translation = true_translation_dictionary()
    for k, v in translate.items():
        if k in input_data['ciphered_text']:
            logger.info("%s, %s, %s" % (k, v, (v == true_translation[k])))

    logger.debug("Finished decipher")
    return translate

if __name__ == "__main__":
    start = time.time()
    translate_solution = decipher_file()
    true_translation = true_translation_dictionary()
    ciphered_text = fileio.read_ciphered_text()
    success = True
    for letter in alphabet:
        if letter in ciphered_text:
            result = translate_solution(letter)
            expected = true_translation[letter]
            if result != expected:
                print "Incorrect: letter=%s, expected=%s, result=%s" \
                      % (letter, expected, result)
                success = False
    assert success
    fileio.write_solution(translate_solution)
    finish = time.time()
    runtime = finish - start
    logger.info("Success")