-
Notifications
You must be signed in to change notification settings - Fork 7
/
frequency_analysis.py
51 lines (42 loc) · 1.84 KB
/
frequency_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import argparse
from library.statistics import *
def main():
parser = argparse.ArgumentParser(description='Calculates letter frequencies of given cipher text in a cipher file.')
parser.add_argument('CipherFile',
help='The cipher file to analyze.')
parser.add_argument("-s", "--spaces", action='store_true',
help="Counts spaces as a valid cipher character instead of ignoring them."
"", required=False)
parser.add_argument("-p", "--punctuation", action='store_true',
help="Counts punctuation as valid cipher characters instead of ignoring them."
"", required=False)
args = parser.parse_args()
cipherfile = args.CipherFile
with open(cipherfile, 'r') as cf:
ciphertext = cf.read()
cipherlettercounts = build_ngram_counts(ciphertext, 1, args.spaces, args.punctuation)
cipherdigramcounts = build_ngram_counts(ciphertext, 2, args.spaces, args.punctuation)
ciphertrigramcounts = build_ngram_counts(ciphertext, 3, args.spaces, args.punctuation)
cipherquadgramcounts = build_ngram_counts(ciphertext, 4, args.spaces, args.punctuation)
print("**** Cipher Text ****")
print("")
print(ciphertext)
print("")
print("Letter Counts:")
for c in cipherlettercounts:
print("{0} = {1}".format(c, cipherlettercounts[c]))
print("")
print("Digram Counts:")
for c in cipherdigramcounts:
print("{0} = {1}".format(c, cipherdigramcounts[c]))
print("")
print("Trigram Counts:")
for c in ciphertrigramcounts:
print("{0} = {1}".format(c, ciphertrigramcounts[c]))
print("")
print("Quadgram Counts:")
for c in cipherquadgramcounts:
print("{0} = {1}".format(c, cipherquadgramcounts[c]))
print("")
if __name__ == "__main__":
main()