-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
267 lines (189 loc) · 7.62 KB
/
main.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
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
"""
File: main.py
-------------------
Final Project: Commentz-Walter String Matching Algorithm
Course: CS 166
Author: Christina Gilbert
Group: Christina Gilbert, Eric Ehizokhale, Jake Rachleff
Main file for testing runtimes of Aho-Corasick vs Rabin Karp vs
Commentz Walter algorithms for plagarism using k-shingles of a test
file against a corpus of other files.
"""
import pathlib
import rabin_karp
import time
import random
from enum import Enum
from collections import namedtuple
from structures import ACAuto
from structures import CWAuto
TEST_FILE = "corpus/testfile"
CORPUS_PREFIX = "article_scraper/articles/"
SHINGLE_CONSTANT = 50
RANDOM = False
OUTFILE_PATH = "outfile.txt"
ac_match_count = 0
##### GENERAL UTILITIES #####
class Algorithm(Enum):
aho_corasick = 0
rabin_karp = 1
commentz_walter = 2
def files_in_directory(dirname):
"""CITATION: Taken from the starter code of a CS41 assignment
Return a list of filenames in the given directory.
@param dirname: name of directory from which to acquire files.
@return: list of strings representing names of files in the given directory
"""
p = pathlib.Path(dirname)
if not p.is_dir():
raise NotADirectoryError("`{d}` is not a directory".format(d=dirname))
return [str(child) for child in p.iterdir() if child.is_file()]
##### AHO CORASICK #####
def build_automaton(shingles, automaton):
"""Return an Aho-Corasick Automaton for a list of shingles
@param shingles: list of k-shingles
@return: Aho-Corasick Automaton for shingles
"""
a = ACAuto() if automaton == Algorithm.aho_corasick else CWAuto()
for shingle in shingles:
a.add_word(shingle)
a.create_failure_links()
return a
# a = ahocorasick.Automaton()
# for index, shingle in enumerate(shingles):
# a.add_word(shingle, (index, shingle))
# a.make_automaton()
# return a
def get_random_shingles(text, k):
"""//TODO DOCUMENT
@param text: string to convert to shingles
@param k: length of each single
@return: list of shingles
"""
length = len(text)
all_shingles = [text[i:i+k] for i in range(length) if i + k < length]
random_shingles = random.sample(all_shingles, int(len(all_shingles)/SHINGLE_CONSTANT))
return random_shingles
def get_shingles(text, k):
"""Return a list of the k-singles of a text file
@param text: string to convert to shingles
@param k: length of each single
@return: list of shingles
"""
length = len(text)
return [text[i:i+k] for i in range(length) if i + k < length]
def ac_match_callback(index, value):
"""Callback function for ac.find_all
Prints all matches and increments count.
@param index: index in string T of match
@param value: tuple (index, value) number of shingle, text of shingle
"""
global ac_match_count
ac_match_count += 1
def run_aho_corasick(shingles, file_names):
"""Uses Aho-Corasick automaton to find all matches
@param shingles: list of shingles of test document
@param file_names: list of all file_names to be checked for shingles
@return: (time, matches) time elapsed to match all shingles to all files
total number of matches found in text
"""
#start the timer on aho-corasick
start_time = time.time()
ac_total_matches = 0
ac = build_automaton(shingles, Algorithm.aho_corasick)
for file_name in file_names:
#print(file_name)
text = ''.join([line.rstrip('\n') for line in open(file_name)])
ac_total_matches += ac.report_all_matches(text)
elapsed_time = time.time() - start_time
return Result(elapsed_time, ac_total_matches)
##### RABIN KARP #####
def run_rabin_karp(test_file_text, shingles, shingle_len, file_names):
"""Uses Rabin-Karp algorithm to find all matches
@param shingles: list of shingles of test document
@param file_names: list of all file_names to be checked for shingles
@param shingle_len: length of shingles
@return: Result(time, matches) time elapsed to match all shingles to all files
total number of matches found in text
"""
#TODO: Decide if this should come before or after timer
shingles = set(shingles)
#start the timer on rabin-karp
start_time = time.time()
pattern_set = rabin_karp.rabin_karp_pattern_set(test_file_text, shingle_len)
rc_matches_count = 0
for file_name in file_names:
text = ''.join([line.rstrip('\n') for line in open(file_name)])
rc_matches_count += rabin_karp.rabin_karp_get_matches(text, shingle_len, shingles, pattern_set)
elapsed_time = time.time() - start_time
return Result(elapsed_time, rc_matches_count)
##### COMMENTZ WALTER #####
def run_commentz_walter(shingles, file_names):
"""Uses Commentz-Walter algorithm to find all matches
@param shingles: list of shingles of test document
@param file_names: list of all file_names to be checked for shingles
@return: Result(time, matches) time elapsed to match all shingles to all files
total number of matches found in text
"""
start_time = time.time()
cw_total_matches = 0
cw = build_automaton(shingles, Algorithm.commentz_walter)
for file_name in file_names:
text = ''.join([line.rstrip('\n') for line in open(file_name)])
cw_total_matches += cw.report_all_matches(text)
elapsed_time = time.time() - start_time
return Result(elapsed_time, cw_total_matches)
##### MAIN #####
def run_tests(file_names, test_file_text, shingle_len, shingles, algorithm):
""" Runs all tests on algorithm and prints and returns the runtime
@param file_names: list of all file_names to be checked for shingles
@param test_file_text: text of the file we're testing against
@param shingle_len
@param algorithm: Algorithm to be tested
@return: time elapsed to match all shingles to all files
"""
if(algorithm == Algorithm.aho_corasick):
print("#### AHO-CORASICK ####")
result = run_aho_corasick(shingles, file_names)
if(algorithm == Algorithm.rabin_karp):
print("#### RABIN-KARP ####")
result = run_rabin_karp(test_file_text, shingles, shingle_len, file_names)
if(algorithm == Algorithm.commentz_walter):
print("#### COMMENTZ-WALTER ####")
result = run_commentz_walter(shingles, file_names)
print("ELAPSED TIME: {time}".format(time=result.runtime))
print("TOTAL MATCHES: {matches}".format(matches=result.matches))
return result.runtime
def write_to_outfile(string_to_write):
try:
new_file = open(OUTFILE_PATH, 'a')
new_file.write(string_to_write)
new_file.write('\n')
new_file.close()
except:
print('Error creating file {filename}'.format(filename = OUTFILE_PATH))
def run_all_tests(file_names, test_file_text, shingles, shingle_len):
print("SHINGLE LENGTH: {len}".format(len=shingle_len))
ac_runtime = run_tests(file_names, test_file_text, shingle_len, shingles, Algorithm.aho_corasick)
rk_runtime = run_tests(file_names, test_file_text, shingle_len, shingles, Algorithm.rabin_karp)
cw_runtime = run_tests(file_names, test_file_text, shingle_len, shingles, Algorithm.commentz_walter)
write_to_outfile("SHINGLE LENGTH: {len}".format(len=shingle_len))
write_to_outfile(str(ac_runtime))
write_to_outfile(str(rk_runtime))
write_to_outfile(str(cw_runtime))
print("-- -- -- -- -- -- -- -- -- -- -- ")
if __name__ == '__main__':
Result = namedtuple('Result', ['runtime', 'matches'])
corpuses = []
corpuses.append("all_articles2.3MB")
#test of document we want to detect plagarism in
test_file_text = ''.join([line.rstrip('\n') for line in open(TEST_FILE)])
for corpus in corpuses:
write_to_outfile("CORPUS: {corpus}".format(corpus=corpus))
#filenames of all other files
file_names = files_in_directory(CORPUS_PREFIX + corpus)
for i in range(5, 15):
shingles = get_shingles(test_file_text, i) if not RANDOM else get_random_shingles(test_file_text, i)
print("RANDOM: {random}".format(random=RANDOM))
print("CORPUS: {corpus}".format(corpus=corpus))
run_all_tests(file_names, test_file_text, shingles, i)