-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_diff_mdl_models.py
More file actions
265 lines (220 loc) · 7.54 KB
/
text_diff_mdl_models.py
File metadata and controls
265 lines (220 loc) · 7.54 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from difflib import SequenceMatcher
from os import walk,path
import os
import argparse
import sys
import datetime
import time
import progressbar
widgets=[
' [', progressbar.Timer(), '] ',
progressbar.Bar(),
' (', progressbar.ETA(), ') ',
]
class text_diff():
'''
#This class checks the difference between two mdkl files
Usage:
'''
def __init__(self, score, mode, output_path,file_directory='', file_to_compare='', file_to_compare_with='') -> None:
"""
Initializes fileDirectory
args:
fileDirectory : directory containing all mdl files
score : Threshold to consider which two file should be considered as duplicate
mode: Printing modes
Returns:
None
"""
#ADD check here
self.fileDirectory = file_directory
self.mode = mode
self.score_threshold = score
self.file_list = []
self.duplicate_files_dict = {}
self.file_to_compare = file_to_compare
self.file_to_compare_with = file_to_compare_with
self.output_path = output_path
print("Output File Path :" + str(self.output_path))
print("Similarity threshold :" + str(self.score_threshold))
print("Input Path :" + str(self.fileDirectory))
print("Input Files: "+ str(self.file_to_compare) + " "+ str(self.file_to_compare_with))
def get_files_from_directory(self):
'''
Gets the file names from the top level directory
args:
None
Returns:
List of file names in that directory
'''
for (dirpath, dirnames, filenames) in walk(self.fileDirectory):
self.file_list.extend(filenames)
break # gets the file from the top level directory only
def compare_files(self, file1, file2) -> None:
"""
compares two files given
args:
file1 : opened first file
file2 : opened second file
Returns:
measure of the sequences' similarity as a float in the range [0,1]
1= Exactly the same
0= Entirely different
"""
seqMatcher = SequenceMatcher(lambda x: x == " ", file1.split(), file2.split())
return seqMatcher.quick_ratio()
def compare_two_files(self) -> float:
"""
compares two files given and report similarity score
args:
Returns:
measure similarity score as a float in the range [0,1]
1= Exactly the same
0= Entirely different
"""
file1 = open(self.file_to_compare_with).read()
file2 = open(self.file_to_compare).read()
print(self.compare_files(file1, file2))
def compare_all_in_dir(self):
'''
gets all the files in a top level directory and compares all of them
args:
None
returns:
a dictionary with the file
'''
duplicate_files = []
file_counter =0
self.get_files_from_directory()
bar = progressbar.ProgressBar( min_value = 0,max_value = len(self.file_list),widgets=widgets)
for f1 in self.file_list:
file_counter += 1
# No need to compare duplicate files or create its own dictionary
if f1 in duplicate_files:
continue
bar_counter = 0
print ("Comparing "+ f1 +"\n")
print(str(file_counter)+ " out of "+ str(len(self.file_list)) )
bar.start()
file1 = open(self.fileDirectory + "/" + f1).read()
self.duplicate_files_dict[f1] = []
for f2 in self.file_list:
bar_counter+=1
if f2 in duplicate_files or f2 in self.duplicate_files_dict.keys():
continue
file2 = open(self.fileDirectory + "/" + f2).read()
score = self.compare_files(file1, file2)
if score > self.score_threshold:
duplicate_files.append(f2)
self.duplicate_files_dict[f1].append({f2:score})
bar.update(bar_counter)
bar.finish()
self.cache_results(f1)
return (self.duplicate_files_dict)
def print_report(self):
'''
print list of unique files
args:
mode :
unique defaults to unique
returns:
a dictionary with the file
'''
print("Total number of unique files " +
str(len(self.duplicate_files_dict)))
if (self.mode == 'unique'):
for key in self.duplicate_files_dict:
print(key)
if self.mode == 'duplicate':
for key, value in self.duplicate_files_dict.items():
if(len(self.duplicate_files_dict[key]) > 1):
print("File : " + key)
print("Duplicates : " + str(value))
def get_unique_file_with_path(self,file_path):
file_name = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M")
if(self.fileDirectory[-1]=='/'):
file_name = file_name +'-'+self.fileDirectory.split('/')[-2]
else:
file_name = file_name +'-'+self.fileDirectory.split('/')[-1]
file_name = file_name+".csv"
return (file_path+"/"+ file_name)
def create_directory(self):
'''
creates a directory called results in the current directory or as specified by output_path
args:
returns:
complete file path along with file name
file name: Current date and time upto min and directory where the file to be compared are located .csv
'''
file_path = "results"
if(self.output_path is not None):
file_path = self.output_path + "/results"
#Create Directory
access_rights = 0o777
try:
os.mkdir(file_path,access_rights)
except OSError:
print("Creation of directory failed | Directory Already Exists")
return file_path
def cache_results(self,file_to_cache):
day_time=datetime.datetime.now().strftime("%Y-%m-%d-%H:%M")
file_path = self.create_directory()
file_to_open = file_path + "/cache.csv"
file_to_write = open(file_to_open,"a")
file_to_write.write('DayTime, File Name ,Duplicate Files,Similarity Score\n')
file_to_write.write(str(day_time)+',' +file_to_cache )
#print(self.duplicate_files_dict[file_to_cache])
for val in self.duplicate_files_dict[file_to_cache]:
for k,v in val.items():
file_to_write.write( ' ,'+k+','+str(v) +'\n')
file_to_write.write( '\n' )
file_to_write.close()
def save_report(self):
'''
saves the comparision results of directory
args:
returns:
'''
file_path = self.create_directory()
file_to_open = self.get_unique_file_with_path(file_path)
if(path.isfile(file_to_open)):
print("File Already Exists")
else:
file_to_write = open(file_to_open,"w")
file_to_write.write('File Name ,Duplicate Files,Similarity Score\n')
for key, value in self.duplicate_files_dict.items():
file_to_write.write( key )
for val in value:
for k,v in val.items():
file_to_write.write( ' ,'+k+','+str(v) +'\n')
file_to_write.write( '\n' )
def main():
# Configure the option parser
usage = "usage: python %prog inputFilePath [inputFilePath2] "
parser = argparse.ArgumentParser(usage)
parser.add_argument('-s', '--score', dest="score", type=float,
help='threshold for comparing two strings ', default=0.8)
parser.add_argument('-i', '--inputPath', dest="inputPath", type=str,
help='Input directory path to compare files ')
parser.add_argument('--inputFile', dest="inputFile", type=str,
help='Input Files to compare ', nargs=2)
parser.add_argument('--mode', dest="mode", type=str,
help='Specify the mode to print results [unique | duplicate ]', default='duplicate')
parser.add_argument('-o', '--outputPath', dest="outputPath", type=str,
help='Output csv file (Stores File Duplicates with Score)')
args = parser.parse_args()
if len(sys.argv) == 0:
parser.print_help()
sys.exit(1)
if len(sys.argv) < 1:
parser.error("need to specify a input File Path")
if(args.inputPath is not None):
text_diff_obj = text_diff(args.score, args.mode,args.outputPath,args.inputPath )
text_diff_obj.compare_all_in_dir()
text_diff_obj.save_report()
#text_diff_obj.print_report()
elif(args.inputFile is not None):
text_diff_obj = text_diff(args.score, args.mode,args.inputPath, file_to_compare_with=args.inputFile[0],file_to_compare=args.inputFile[1] )
text_diff_obj.compare_two_files()
if __name__ == '__main__':
main()