def main(): n = 1 optlist, args = getopt.getopt(sys.argv[1:], "n:") for opt, val in optlist: if opt in ['-n']: n = int(val) for infile_name in args: with open(infile_name, "r") as infile: text = get_text(infile) print("%s\n" % (''.join(text))) split_text = [[] for i in range(n)] for i, c in enumerate(text): split_text[i % n].append(c) for i, l in enumerate(split_text): print("Position %d:" % (i)) print(" %s" % ("".join(l))) dist = get_ngram_freqs(l, 1) dist_l = [(dist[c], c) for c in dist] dist_l.sort(reverse=True) for ndx in range(min(5, len(dist))): c = dist_l[ndx][1] n_c = dist_l[ndx][0] i_c = ascii_uppercase.index(c) i_e = (i_c - 5) % len(ascii_uppercase) print(" %s: %d %s" % (c, n_c, ascii_uppercase[i_e]))
def process_file(infile_name, key_len): with open(infile_name, "r") as infile: text = get_text(infile) split_text = mod_split(text, key_len) for i, seg in enumerate(split_text): print("%d: %.5f" % (i, compute_coinicidence(seg)))
def main(): for infile_name in sys.argv[1:]: print("\n%s:" % (infile_name)) with open(infile_name, "r") as infile: text = [ascii_uppercase.index(c) for c in get_text(infile)] for i in range(len(ascii_uppercase)): shift_text = [(x - i) % len(ascii_uppercase) for x in text] print(ascii_uppercase[i] + " (== %2d): " % (i) + ''.join([ascii_uppercase[i] for i in shift_text]))
def process_file(infile, key_len): text_nums = numberize_text(get_text(infile)) split_nums = mod_split(text_nums, key_len) format_str = " ".join(["%.5f"] * key_len) for k_i in range(len(ascii_uppercase)): curr_split = [shift_text_nums(l, -k_i) for l in split_nums] coincs = [] for l in curr_split: coinc = compute_coincidence_base(l) coincs.append(coinc) coinc_str = format_str % (tuple(coincs)) print("%s: %s" % ("%2d %s" % (k_i, ascii_uppercase[k_i]), coinc_str))
def process_file(infile_name): with open(infile_name, "r") as infile: text = get_text(infile) text_str = ''.join(text) n = 3 while True: n_dist = get_ngram_freqs(text, n) n_list = [g for g in n_dist if n_dist[g] > 1] if len(n_list) > 0: for g in n_list: ndxs = [m.start() for m in re.finditer(g, text_str)] diffs = [t[0] - t[1] for t in zip(ndxs[1:], ndxs)] print("%s: %s" % (g, " ".join([str(d) for d in diffs]))) else: break n += 1
def main(): a = 1 b = 0 optlist,args = getopt.getopt(sys.argv[1:],"a:b:") for opt,val in optlist: if opt in ["-a"]: a = int(val) elif opt in ["-b"]: b = int(val) for infile_name in args: with open(infile_name,"r") as infile: text = get_text(infile) text = [apply_affine(c,a,b) for c in text] text = [ascii_uppercase[i] for i in text] print("".join(text))
def main(): encipher = True key = None optlist, args = getopt.getopt(sys.argv[1:], "dk:") for opt, val in optlist: if opt in ["-d"]: encipher = False elif opt in ["-k"]: key = val.upper() for infile_name in args: with open(infile_name, "r") as infile: text = ''.join(get_text(infile)) if encipher: print(vig_encipher(text, key)) else: print(vig_decipher(text, key))
def test_get_text(self): self.assertTrue(n.get_text("ideal_post")[:9] == 'attention')
def test_get_text(self): self.assertTrue(n.get_text("ideal_post")[:9]=='attention')