Tuned default chunksize for keyword break
authorNeil Smith <neil.git@njae.me.uk>
Sun, 3 Nov 2013 23:06:02 +0000 (23:06 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Sun, 3 Nov 2013 23:06:02 +0000 (23:06 +0000)
cipher.py

index 8eb689ca6a55cbe18a9a4375e9580d689113dcce..c5cd6060e208dff51eb7a1cec59649558844a62a 100644 (file)
--- a/cipher.py
+++ b/cipher.py
@@ -429,7 +429,7 @@ def keyword_break(message, wordlist=keywords, metric=norms.euclidean_distance, t
     logger.info('Keyword break best fit with key {0} (wrap={1}) gives fit of {2} and decrypt starting: {3}'.format(best_keyword, best_wrap_alphabet, best_fit, sanitise(keyword_decipher(message, best_keyword))[:50]))
     return (best_keyword, best_wrap_alphabet), best_fit
 
     logger.info('Keyword break best fit with key {0} (wrap={1}) gives fit of {2} and decrypt starting: {3}'.format(best_keyword, best_wrap_alphabet, best_fit, sanitise(keyword_decipher(message, best_keyword))[:50]))
     return (best_keyword, best_wrap_alphabet), best_fit
 
-def keyword_break_mp(message, wordlist=keywords, metric=norms.euclidean_distance, target_counts=normalised_english_counts, message_frequency_scaling=norms.normalise):
+def keyword_break_mp(message, wordlist=keywords, metric=norms.euclidean_distance, target_counts=normalised_english_counts, message_frequency_scaling=norms.normalise, chunksize=500):
     """Breaks a keyword substitution cipher using a dictionary and frequency analysis
 
     >>> keyword_break_mp(keyword_encipher('this is a test message for the keyword decipherment', 'elephant', 1), wordlist=['cat', 'elephant', 'kangaroo']) # doctest: +ELLIPSIS
     """Breaks a keyword substitution cipher using a dictionary and frequency analysis
 
     >>> keyword_break_mp(keyword_encipher('this is a test message for the keyword decipherment', 'elephant', 1), wordlist=['cat', 'elephant', 'kangaroo']) # doctest: +ELLIPSIS
@@ -438,7 +438,7 @@ def keyword_break_mp(message, wordlist=keywords, metric=norms.euclidean_distance
     with Pool() as pool:
         helper_args = [(message, word, wrap, metric, target_counts, message_frequency_scaling) for word in wordlist for wrap in range(3)]
         # breaks = map(lambda kw: keyword_break_one(message, kw[0], kw[1], metric, target_counts, message_frequency_scaling), keys)
     with Pool() as pool:
         helper_args = [(message, word, wrap, metric, target_counts, message_frequency_scaling) for word in wordlist for wrap in range(3)]
         # breaks = map(lambda kw: keyword_break_one(message, kw[0], kw[1], metric, target_counts, message_frequency_scaling), keys)
-        breaks = pool.starmap(keyword_break_one, helper_args, 1000)
+        breaks = pool.starmap(keyword_break_one, helper_args, chunksize)
         return min(breaks, key=lambda k: k[1])
 
 def keyword_break_one(message, keyword, wrap_alphabet, metric, target_counts, message_frequency_scaling):
         return min(breaks, key=lambda k: k[1])
 
 def keyword_break_one(message, keyword, wrap_alphabet, metric, target_counts, message_frequency_scaling):