Add getGrams script
This commit is contained in:
		
							parent
							
								
									daa84059f1
								
							
						
					
					
						commit
						1baeb59a85
					
				
							
								
								
									
										40
									
								
								getgrams.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										40
									
								
								getgrams.py
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user