-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities_A3.py
224 lines (197 loc) · 7.74 KB
/
utilities_A3.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# --------------------------
# CP460 Fall 2019
# Assignment 3
# Utilities
# DO NOT CHANGE THIS FILE
# --------------------------
import string
import math
# Included Functions
# 1- file_to_text(fileName)
# 2- text_to_file(fileName)
# 3- get_lower()
# 4- shift_string(s,n,d)
# 5- load_dictionary(dictFile)
# 6- text_to_words(text)
# 7- analyze_text(text, dictFile)
# 8- is_plaintext(text, dictFile, threshold)
# 9- new_matrix(r,c,pad)
# 10- text_to_blocks(text,size)
# 11- get_adfgvx_square()
# 12- index_matrix(element,matrix)
# -----------------------------------------------------------
# Parameters: fileName (string)
# Return: contents (string)
# Description: Utility function to read contents of a file
# Can be used to read plaintext or ciphertext
# -----------------------------------------------------------
def file_to_text(fileName):
inFile = open(fileName, 'r')
contents = inFile.read()
inFile.close()
return contents
# -----------------------------------------------------------
# Parameters: text (string)
# filename (string)
# Return: none
# Description: Utility function to write any given text to a file
# If file already exist, previous content will be over-written
# -----------------------------------------------------------
def text_to_file(text, filename):
outFile = open(filename, 'w')
outFile.write(text)
outFile.close()
return
# -----------------------------------------------------------
# Parameters: None
# Return: alphabet (string)
# Description: Return a string of lower case alphabet
# -----------------------------------------------------------
def get_lower():
return "".join([chr(ord('a') + i) for i in range(26)])
# -------------------------------------------------------------------
# Parameters: s (string): input string
# n (int): number of shifts
# d (str): direction ('l' or 'r')
# Return: s (after applying shift
# Description: Shift a given string by n shifts (circular shift)
# as specified by direction, l = left, r= right
# if n is negative, multiply by 1 and change direction
# -------------------------------------------------------------------
def shift_string(s, n, d):
if d != 'r' and d != 'l':
print('Error (shift_string): invalid direction')
return ''
if n < 0:
n = n * -1
d = 'l' if d == 'r' else 'r'
n = n % len(s)
if s == '' or n == 0:
return s
s = s[n:] + s[:n] if d == 'l' else s[-1 * n:] + s[:-1 * n]
return s
# -----------------------------------------------------------
# Parameters: dictFile (string): filename
# Return: list of words (list)
# Description: Reads a given dictionary file
# dictionary file is assumed to be formatted: each word in a separate line
# Returns a list of strings, each pertaining to a dictionary word
# -----------------------------------------------------------
def load_dictionary(dictFile):
inFile = open(dictFile, 'r', encoding=" ISO-8859-15")
dictList = inFile.readlines()
i = 0
for word in dictList:
dictList[i] = word.strip('\n')
i += 1
inFile.close()
return dictList
# -------------------------------------------------------------------
# Parameters: text (string)
# Return: list of words (list)
# Description: Reads a given text
# Each word is saved as an element in a list.
# Returns a list of strings, each pertaining to a word in file
# Gets rid of all punctuation at the start and at the end
# -------------------------------------------------------------------
def text_to_words(text):
wordList = []
lines = text.split('\n')
for line in lines:
line = line.strip('\n')
line = line.split(' ')
for i in range(len(line)):
if line[i] != '':
line[i] = line[i].strip(string.punctuation)
wordList += [line[i]]
return wordList
# -----------------------------------------------------------
# Parameters: text (string)
# dictFile (string): dictionary file
# Return: (#matches, #mismatches)
# Description: Reads a given text, checks if each word appears in dictionary
# Returns a tuple of number of matches and number of mismatches.
# Words are compared in lowercase.
# -----------------------------------------------------------
def analyze_text(text, dictFile):
dictList = load_dictionary(dictFile)
wordList = text_to_words(text)
matches = 0
mismatches = 0
for word in wordList:
if word.lower() in dictList:
matches += 1
else:
mismatches += 1
return (matches, mismatches)
# -----------------------------------------------------------
# Parameters: text (string)
# dictFile (string): dictionary file
# threshold (float): number between 0 to 1
# Return: True/False
# Description: Check if a given file is a plaintext
# If #matches/#words >= threshold --> True
# otherwise --> False
# If invalid threshold given, default is 0.9
# An empty string is assumed to be non-plaintext.
# -----------------------------------------------------------
def is_plaintext(text, dictFile, threshold):
if text == '':
return False
result = analyze_text(text, dictFile)
percentage = result[0] / (result[0] + result[1])
if threshold < 0 or threshold > 1:
threshold = 0.9
if percentage >= threshold:
return True
return False
# -----------------------------------------------------------
# Parameters: r: #rows (int)
# c: #columns (int)
# pad (str,int,double)
# Return: empty matrix (2D List)
# Description: Create an empty matrix of size r x c
# All elements initialized to pad
# Default row and column size is 2
# -----------------------------------------------------------
def new_matrix(r, c, pad):
r = r if r >= 2 else 2
c = c if c >= 2 else 2
return [[pad] * c for i in range(r)]
# -----------------------------------------------------------------------------
# Parameters: text (string)
# size (int)
# Return: list of strings
# Description: Break a given string into strings of given size
# Result is provided in a list
# ------------------------------------------------------------------------------
def text_to_blocks(text, size):
return [text[i * size:(i + 1) * size] for i in range(math.ceil(len(text) / size))]
# ----------------------------------------------------
# Parameters: None
# Return: ADFGVX Square (2D list)
# Description: Returns a 2D List
# representing the polybius square to be used
# in ADFGVX cipher
# ---------------------------------------------------
def get_adfgvx_square():
return [['F', 'L', '1', 'A', 'O', '2'],
['J', 'D', 'W', '3', 'G', 'U'],
['C', 'I', 'Y', 'B', '4', 'P'],
['R', '5', 'Q', '8', 'V', 'E'],
['6', 'K', '7', 'Z', 'M', 'X'],
['S', 'N', 'H', '0', 'T', '9']]
# -----------------------------------------------------------
# Parameters: element (str)
# matrix (2D List)
# Return: [r,c]
# Description: returns position of a string element in a 2D
# List, r = row number, c = column number
# if not found --> return [-1,-1]
# -----------------------------------------------------------
def index_matrix(element, matrix):
for r in range(len(matrix)):
row = matrix[r]
if element in row:
return [r, row.index(element)]
return [-1, -1]