From 1baeb59a85929976c947fa66327a999786002f8a Mon Sep 17 00:00:00 2001 From: Bucchino Geoffrey Date: Sat, 25 Jun 2022 21:03:37 +0200 Subject: [PATCH] Add getGrams script --- getgrams.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 getgrams.py diff --git a/getgrams.py b/getgrams.py new file mode 100644 index 0000000..ed22886 --- /dev/null +++ b/getgrams.py @@ -0,0 +1,40 @@ +# coding: utf-8 + +from collections import Counter +from argparse import ArgumentParser + + +def checkArguments(): + args = ArgumentParser(description='Cryptanalyst') + args.add_argument('-f', '--filename', help='Text to analyze') + args.add_argument('-b', '--bigram', help='Get bigram', action="store_true") + args.add_argument('-t', '--trigram', help='Get trigram', action="store_true") + return args.parse_args() + +def readFile(filename): + """ + This read the file passed in argument and return the data of it + """ + text = str() + with open(filename, 'r') as f: + data = f.readlines() + + for entry in data: + text += entry + + return text + +def getLetters(text, pos): + # return Counter(text[idx : idx + pos] for idx in range(len(text) - 1)) + return Counter([text[idx: idx + pos] for idx in range(len(text) - 1)]) + +if __name__ == "__main__": + args = checkArguments() + text = readFile(args.filename) + + if args.bigram: + data = getLetters(text, 2) + if args.trigram: + data = getLetters(text, 3) + + print(data)