From: Neil Smith Date: Tue, 6 Mar 2018 16:50:55 +0000 (+0000) Subject: All tests now running and passing X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=df271527a6e3e4d10da9f9be3d1e9ebea9171493;p=cipher-tools.git All tests now running and passing --- diff --git a/cipher/affine.py b/cipher/affine.py index 6ba90e6..8a9e584 100644 --- a/cipher/affine.py +++ b/cipher/affine.py @@ -1,5 +1,5 @@ -from utilities import * -from language_models import * +from support.utilities import * +from support.language_models import * from logger import logger diff --git a/cipher/amsco.py b/cipher/amsco.py index 4eeeb59..3d4e49b 100644 --- a/cipher/amsco.py +++ b/cipher/amsco.py @@ -2,9 +2,9 @@ from enum import Enum import multiprocessing import itertools -from utilities import * -from language_models import * -from column_transposition import transpositions +from support.utilities import * +from support.language_models import * +from cipher.column_transposition import transpositions, transpositions_of from logger import logger diff --git a/cipher/autokey.py b/cipher/autokey.py index b84f0a9..ffdc3a9 100644 --- a/cipher/autokey.py +++ b/cipher/autokey.py @@ -1,6 +1,7 @@ -from utilities import * -from language_models import * import multiprocessing +from support.utilities import * +from support.language_models import * +from cipher.caesar import caesar_encipher_letter, caesar_decipher_letter from logger import logger diff --git a/cipher/bifid.py b/cipher/bifid.py index 4a33456..478b239 100644 --- a/cipher/bifid.py +++ b/cipher/bifid.py @@ -1,7 +1,7 @@ import multiprocessing -from utilities import * -from language_models import * -from keyword_cipher import KeywordWrapAlphabet +from support.utilities import * +from support.language_models import * +from cipher.keyword_cipher import KeywordWrapAlphabet, keyword_cipher_alphabet_of from logger import logger diff --git a/cipher/cadenus.py b/cipher/cadenus.py index 0d3b415..1e569af 100644 --- a/cipher/cadenus.py +++ b/cipher/cadenus.py @@ -1,6 +1,7 @@ -from utilities import * -from language_models import * from itertools import chain +from support.utilities import * +from support.language_models import * +from cipher.column_transposition import transpositions_of from logger import logger diff --git a/cipher/caesar.py b/cipher/caesar.py index 4d9a722..ec878fc 100644 --- a/cipher/caesar.py +++ b/cipher/caesar.py @@ -1,5 +1,6 @@ -from utilities import * -from language_models import * +from support.utilities import * +from support.language_models import * + from logger import logger def caesar_encipher_letter(accented_letter, shift): diff --git a/cipher/column_transposition.py b/cipher/column_transposition.py index a141ff2..7e0fc28 100644 --- a/cipher/column_transposition.py +++ b/cipher/column_transposition.py @@ -1,7 +1,8 @@ -from utilities import * -from language_models import * +import math import multiprocessing from itertools import chain +from support.utilities import * +from support.language_models import * from logger import logger @@ -254,7 +255,7 @@ def scytale_break_mp(message, max_key_length=20, fitness=Ptrigrams) # doctest: +ELLIPSIS (5, -997.0129085...) """ - with Pool() as pool: + with multiprocessing.Pool() as pool: helper_args = [(message, trans, False, True, fitness) for trans in [[col for col in range(math.ceil(len(message)/rows))] diff --git a/cipher/hill.py b/cipher/hill.py index 75048f9..24bac7f 100644 --- a/cipher/hill.py +++ b/cipher/hill.py @@ -1,9 +1,10 @@ -from utilities import * -from language_models import * import multiprocessing import numpy as np from numpy import matrix from numpy import linalg +from support.utilities import * +from support.language_models import * +from cipher.affine import modular_division_table from logger import logger diff --git a/cipher/keyword_cipher.py b/cipher/keyword_cipher.py index 68c8904..30fd256 100644 --- a/cipher/keyword_cipher.py +++ b/cipher/keyword_cipher.py @@ -1,8 +1,8 @@ -from utilities import * -from language_models import * from enum import Enum # from itertools import starmap import multiprocessing +from support.utilities import * +from support.language_models import * from logger import logger diff --git a/cipher/pocket_enigma.py b/cipher/pocket_enigma.py index 557bda3..a51955a 100644 --- a/cipher/pocket_enigma.py +++ b/cipher/pocket_enigma.py @@ -1,5 +1,5 @@ -from utilities import * -from language_models import * +from support.utilities import * +from support.language_models import * from logger import logger diff --git a/cipher/polybius.py b/cipher/polybius.py index 79aeb38..965c3bb 100644 --- a/cipher/polybius.py +++ b/cipher/polybius.py @@ -1,8 +1,7 @@ -from utilities import * -from language_models import * import multiprocessing - -from keyword_cipher import KeywordWrapAlphabet +from support.utilities import * +from support.language_models import * +from cipher.keyword_cipher import KeywordWrapAlphabet, keyword_cipher_alphabet_of from logger import logger diff --git a/cipher/railfence.py b/cipher/railfence.py index 78154aa..7d6ac31 100644 --- a/cipher/railfence.py +++ b/cipher/railfence.py @@ -1,8 +1,9 @@ -from utilities import * -from language_models import * +import math from enum import Enum -from itertools import starmap -from itertools import zip_longest +from itertools import starmap, zip_longest +from support.utilities import * +from support.language_models import * + from logger import logger diff --git a/cipher/vigenere.py b/cipher/vigenere.py index dcf4a2b..f54fe6f 100644 --- a/cipher/vigenere.py +++ b/cipher/vigenere.py @@ -1,8 +1,8 @@ -from utilities import * -from language_models import * from enum import Enum from itertools import starmap, cycle import multiprocessing +from support.utilities import * +from support.language_models import * from logger import logger diff --git a/run_tests b/run_tests new file mode 100755 index 0000000..42a8108 --- /dev/null +++ b/run_tests @@ -0,0 +1,3 @@ +#!/bin/bash + +python3 -m unittest discover test diff --git a/support/language_models.py b/support/language_models.py index 53a383d..791d083 100644 --- a/support/language_models.py +++ b/support/language_models.py @@ -1,12 +1,11 @@ import string import random import collections -import unicodedata import itertools from math import log10 import os -import norms +import support.norms def datafile(name, sep='\t'): """Read key,value pairs from file. @@ -17,13 +16,13 @@ def datafile(name, sep='\t'): yield [splits[0], int(splits[1])] english_counts = collections.Counter(dict(datafile('count_1l.txt'))) -normalised_english_counts = norms.normalise(english_counts) +normalised_english_counts = support.norms.normalise(english_counts) english_bigram_counts = collections.Counter(dict(datafile('count_2l.txt'))) -normalised_english_bigram_counts = norms.normalise(english_bigram_counts) +normalised_english_bigram_counts = support.norms.normalise(english_bigram_counts) english_trigram_counts = collections.Counter(dict(datafile('count_3l.txt'))) -normalised_english_trigram_counts = norms.normalise(english_trigram_counts) +normalised_english_trigram_counts = support.norms.normalise(english_trigram_counts) with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'words.txt'), 'r') as f: keywords = [line.rstrip() for line in f] @@ -112,9 +111,9 @@ def cosine_distance_score(text): >>> cosine_distance_score('abcabc') # doctest: +ELLIPSIS 0.73777... """ - # return norms.cosine_distance(english_counts, + # return support.norms.cosine_distance(english_counts, # collections.Counter(sanitise(text))) - return 1 - norms.cosine_similarity(english_counts, + return 1 - support.norms.cosine_similarity(english_counts, collections.Counter(sanitise(text))) diff --git a/support/lettercount.py b/support/lettercount.py index 4a7082d..c095cef 100644 --- a/support/lettercount.py +++ b/support/lettercount.py @@ -1,8 +1,9 @@ import collections import string +from utilities import sanitise -def sanitise(text): - return [l.lower() for l in text if l in string.ascii_letters] +# def sanitise(text): +# return [l.lower() for l in text if l in string.ascii_letters] corpora = ['shakespeare.txt', 'sherlock-holmes.txt', 'war-and-peace.txt'] counts = collections.defaultdict(int) diff --git a/support/make-cracking-dictionary.py b/support/make-cracking-dictionary.py index 37de917..5d84e61 100644 --- a/support/make-cracking-dictionary.py +++ b/support/make-cracking-dictionary.py @@ -1,4 +1,5 @@ -import language_models +# import language_models +import utilities american = set(open('/usr/share/dict/american-english', 'r').readlines()) british = set(open('/usr/share/dict/british-english', 'r').readlines()) @@ -11,7 +12,7 @@ words = american | british | cracklib # for w in words: # sanitised_words.add(language_models.sanitise(w)) -sanitised_words = set(language_models.sanitise(w) for w in words) +sanitised_words = set(utilities.sanitise(w) for w in words) sanitised_words.discard('') diff --git a/support/text_prettify.py b/support/text_prettify.py index d3a6ffa..60963f8 100644 --- a/support/text_prettify.py +++ b/support/text_prettify.py @@ -1,6 +1,6 @@ -from segment import segment -from utilities import cat, sanitise import string +from support.segment import segment +from support.utilities import cat, sanitise def tpack(text, width=100): diff --git a/support/utilities.py b/support/utilities.py index ca984a3..3125a94 100644 --- a/support/utilities.py +++ b/support/utilities.py @@ -1,5 +1,6 @@ import string import collections +import unicodedata from itertools import zip_longest # join a a list of letters into a string diff --git a/test/test_doctests.py b/test/test_doctests.py index 31f2a17..0af5050 100644 --- a/test/test_doctests.py +++ b/test/test_doctests.py @@ -1,7 +1,33 @@ import unittest import doctest -import cipher + +import cipher.caesar +import cipher.affine +import cipher.keyword_cipher +import cipher.polybius +import cipher.column_transposition +import cipher.railfence +import cipher.cadenus +import cipher.hill +import cipher.amsco +import cipher.bifid +import cipher.autokey +import cipher.pocket_enigma + def load_tests(loader, tests, ignore): - tests.addTests(doctest.DocTestSuite(cipher)) + + tests.addTests(doctest.DocTestSuite(cipher.caesar)) + tests.addTests(doctest.DocTestSuite(cipher.affine)) + tests.addTests(doctest.DocTestSuite(cipher.keyword_cipher)) + tests.addTests(doctest.DocTestSuite(cipher.polybius)) + tests.addTests(doctest.DocTestSuite(cipher.column_transposition)) + tests.addTests(doctest.DocTestSuite(cipher.railfence)) + tests.addTests(doctest.DocTestSuite(cipher.cadenus)) + tests.addTests(doctest.DocTestSuite(cipher.hill)) + tests.addTests(doctest.DocTestSuite(cipher.amsco)) + tests.addTests(doctest.DocTestSuite(cipher.bifid)) + tests.addTests(doctest.DocTestSuite(cipher.autokey)) + tests.addTests(doctest.DocTestSuite(cipher.pocket_enigma, + extraglobs={'pe': cipher.pocket_enigma.PocketEnigma(1, 'a')})) return tests