use the text and code : cipher.txt xun gmr jznqymxu qzhhxzupmur ytmq dnvhq vavpncd vlvhpq mq txl xh mb ytn anhncxud lmii vpphnqq cnyxx nqenamviid vbynh ytn rxipnu rixgnq ltmat gnavcn v ozgmivuy axcmurxzy evhyd bxh ymcnq ze ytn cxfncnuy qenvhtnvpnp gd exlnhbzi txiidlxxp lxcnu ltx tnienp hvmqn cmiimxuq xb pxiivhq yx bmrty qnkzvi tvhvqqcnuy vhxzup ytn axzuyhd python code : #!/usr/bin/env python3 from collections import Counter import re TOP_K = 20 N_GRAM = 3 # Generate all the n-grams for value n def ngrams(n, text): for i in range(len(text) -n + 1): # Ignore n-grams containing white space if not re.search(r'\s', text[i:i+n]): yield text[i:i+n] # Read the data from the ciphertext with open('ciphertext.txt') as f: text = f.read() # Count, sort, and print out the n-grams for N in range(N_GRAM): print("-------------------------------------") print("{}-gram (top {}):".format(N+1, TOP_K)) counts = Counter(ngrams(N+1, text)) # Count sorted_counts = counts.most_common(TOP_K) # Sort for ngram, count in sorted_counts: print("{}: {}".format(ngram, count)) # Print
use the text and code :
cipher.txt
xun gmr jznqymxu qzhhxzupmur ytmq dnvhq vavpncd vlvhpq mq txl xh mb ytn
anhncxud lmii vpphnqq cnyxx nqenamviid vbynh ytn rxipnu rixgnq ltmat gnavcn
v ozgmivuy axcmurxzy evhyd bxh ymcnq ze ytn cxfncnuy qenvhtnvpnp gd
exlnhbzi txiidlxxp lxcnu ltx tnienp hvmqn cmiimxuq xb pxiivhq yx bmrty qnkzvi
tvhvqqcnuy vhxzup ytn axzuyhd
python code :
#!/usr/bin/env python3
from collections import Counter
import re
TOP_K = 20
N_GRAM = 3
# Generate all the n-grams for value n
def ngrams(n, text):
for i in range(len(text) -n + 1):
# Ignore n-grams containing white space
if not re.search(r'\s', text[i:i+n]):
yield text[i:i+n]
# Read the data from the ciphertext
with open('ciphertext.txt') as f:
text = f.read()
# Count, sort, and print out the n-grams
for N in range(N_GRAM):
print("-------------------------------------")
print("{}-gram (top {}):".format(N+1, TOP_K))
counts = Counter(ngrams(N+1, text)) # Count
sorted_counts = counts.most_common(TOP_K) # Sort
for ngram, count in sorted_counts:
print("{}: {}".format(ngram, count)) # Print
Trending now
This is a popular solution!
Step by step
Solved in 3 steps