-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
441 lines (367 loc) · 15.6 KB
/
test.py
File metadata and controls
441 lines (367 loc) · 15.6 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import sys
import copy
import string
import math
from simhash import Simhash # python-Simhash; might be 128-bit?? [add dependency]
import heapq
import tkinter as tk
import os.path as path
import subprocess
# REMINDER: TRY TO REFACTOR ALL INSTANCES OF LEVENSHTEIN AT SOME POINT
# LHDiff paper: https://www.cs.usask.ca/~croy/papers/2013/LHDiffFullPaper-preprint.pdf
# very useful paper: http://www.xmailserver.org/diff2.pdf
def InputGUI(frame, message, col):
tk.Label(frame, text=message).grid(row=0, column=col, pady=(0,5))
entry = tk.Entry(frame, width=30)
entry.grid(row=1, column=col, padx=10)
return entry
def ButtonGUI(oldEntry, newEntry, oldText, newText, error):
global fpOld, fpNew
fpOld = oldEntry.get().strip()
fpNew = newEntry.get().strip()
# Clears the previous message
error.config(text="")
if fpOld == "" or fpNew == "":
error.config(text="Please enter both files")
return
oldFile = File("old", fpOld)
newFile = File("new", fpNew)
if not(oldFile) and not(newFile):
error.config(text="Old and new files not found, please try again")
return
elif not(oldFile):
error.config(text="Old file not found, please try again")
return
elif not(newFile):
error.config(text="New file not found, please try again")
return
oldText.delete("1.0", tk.END)
oldText.insert("1.0", oldFile)
newText.delete("1.0", tk.END)
newText.insert("1.0", newFile)
file1 = Normalize(oldFile)
file2 = Normalize(newFile)
lhDiff = LHDiff(file1, file2)
print(lhDiff)
def File(x, fp):
# Folder with the GUI's directory.
dirRoot = path.dirname(path.abspath(__file__))
#fp = input("Enter the "+x+" file: ")
if (x == "old"):
#New_File_Versions Folder.
dirData = path.join(dirRoot, "Old_File_Versions")
dirPath = path.join(dirData, fp)
elif (x == "new"):
#Old_File_Versions Folder.
dirData = path.join(dirRoot, "New_File_Versions")
dirPath = path.join(dirData, fp)
try:
file = open(dirPath, "r")
return file.read()
except FileNotFoundError:
return 0
# PREPROCESSING: stream normalized lines from file with lazy evaluation
def Normalize(text: str):
#Generator that yields normalized lines one at a time
f = text.splitlines()
normalizedLines = []
for i in f:
cleaned = " ".join(i.split())
normalized = cleaned.lower().strip()
normalizedLines.append(normalized)
return normalizedLines
# add a compact on-demand line cache to avoid materializing all lines
class LineCache:
def __init__(self, path, encoding='utf-8'):
self.path = path
self.encoding = encoding
# open file in binary mode and build offsets (small memory: 8 bytes * #lines)
self._f = open(path, 'rb')
self._offsets = []
pos = self._f.tell()
line = self._f.readline()
while line:
self._offsets.append(pos)
pos = self._f.tell()
line = self._f.readline()
self._len = len(self._offsets)
def __len__(self):
return self._len
def __getitem__(self, idx):
if idx < 0:
idx += self._len
if idx < 0 or idx >= self._len:
raise IndexError("LineCache index out of range")
self._f.seek(self._offsets[idx])
bline = self._f.readline()
try:
line = bline.decode(self.encoding, errors='replace')
except Exception:
line = bline.decode('latin-1', errors='replace')
cleaned = " ".join(line.split())
return cleaned.lower().strip()
def close(self):
try:
self._f.close()
except Exception:
pass
# Operations
MATCH = 'M'
ADD = 'A'
DELETE = 'D'
CHANGE = 'C'
# standard levenshtein function (should factorize out UnixDiff segment later)
def normalLevenshtein(s1, s2):
m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)] # DP TABLE
# Initialization of table
for i in range(m + 1):
dp[i][0] = i
for j in range(n + 1):
dp[0][j] = j
# Fill table
for i in range(1, m + 1):
for j in range(1, n + 1):
cost = 0 if s1[i - 1] == s2[j - 1] else 1
dp[i][j] = min(
dp[i - 1][j] + 1, # DELETE
dp[i][j - 1] + 1, # ADD
dp[i - 1][j - 1] + cost # CHANGE
)
return dp[m][n] / max(len(s1), len(s2))
# frequency vector construction
def charFrequency(text):
freq = {char: 0 for char in string.ascii_lowercase} # frequency vector as dictionary
for char in text:
if char in freq:
freq[char] += 1
return freq
# CONTEXT SCORE = (A DOT B) / (VLEN(A)VLEN(B))
# character based approach in accordance with LHDiff paper (no tokenization)
def cosineSimilarity(s1, s2):
# make vectors
freq1 = charFrequency(s1)
freq2 = charFrequency(s2)
dotProduct = sum(freq1[char] * freq2[char] for char in string.ascii_lowercase)
vlen1 = math.sqrt(sum(freq1[char] ** 2 for char in string.ascii_lowercase))
vlen2 = math.sqrt(sum(freq2[char] ** 2 for char in string.ascii_lowercase))
if vlen1 == 0 or vlen2 == 0:
return 0.0
return dotProduct / (vlen1 * vlen2)
def get_commit_message_for_line(filepath, line_num):
"""Get the commit message that last modified a specific line using git blame."""
try:
# git blame returns: <hash> (<author> <date> <time> <tz> <line_num>) <line_content>
result = subprocess.run(
['git', 'blame', '-L', f'{line_num},{line_num}', '--porcelain', filepath],
cwd=subprocess.os.path.dirname(filepath) or '.',
capture_output=True,
text=True,
timeout=5
)
if result.returncode != 0:
return None
lines = result.stdout.strip().split('\n')
if not lines or not lines[0]:
return None
# Extract commit hash from first line
commit_hash = lines[0].split()[0]
# Get full commit message
msg_result = subprocess.run(
['git', 'log', '-1', '--pretty=%B', commit_hash],
cwd=subprocess.os.path.dirname(filepath) or '.',
capture_output=True,
text=True,
timeout=5
)
return msg_result.stdout.strip() if msg_result.returncode == 0 else None
except Exception:
return None
def LHDiff(file1, file2):
# Materialize only when needed (DP requires indexing)
# use on-demand file-backed cache (much smaller peak memory than storing all lines)
f1 = len(file1)
f2 = len(file2)
f1 = len(file1)
f2 = len(file2)
distances = []
actions = []
# TABLE CONSTRUCTION (AND INITIALIZATION)
for i in range(f1 + 1):
distances.append([0] * (f2 + 1))
actions.append(['-'] * (f2 + 1))
distances[0][0] = 0 # table's top-left entry is "empty" (we'll be iterating through 1 to n instead of 0 to n - 1)
actions [0][0] = MATCH
# x, y axis setup - horizontal is adding, vertical is deleting
for n1 in range(1, f1 + 1):
distances[n1][0] = n1
actions[n1][0] = DELETE
for n2 in range(1, f2 + 1):
distances[0][n2] = n2
actions[0][n2] = ADD
# TRAVERSAL
# fill in table based of matches or "cheapest" operation
for n1 in range(1, f1 + 1):
for n2 in range(1, f2 + 1):
if file1[n1 - 1] == file2[n2 - 1]:
distances[n1][n2] = distances[n1 - 1][n2 - 1]
actions[n1][n2] = MATCH
else:
delete = (distances[n1 - 1][n2] + 1, DELETE)
add = (distances[n1][n2 - 1] + 1, ADD)
change = (distances[n1 - 1][n2 - 1] + 2, CHANGE) # substitution == delete + insert (cost of 2) | DIAGONAL movement in table
distances[n1][n2], actions[n1][n2] = min([delete, add, change], key=lambda x: x[0]) # traverse by assigned numeric value
# BACKTRACE
leftList = []
rightList = []
mappings = [] # 1-to-1s
n1 = f1
n2 = f2
while n1 > 0 or n2 > 0:
action = actions[n1][n2]
# take most efficient route back to top-left of table, recording mappings and edits along the way
if action == MATCH:
if file1[n1 - 1] != "" or file2[n2 - 1] != "": # multiple empty string checks to omit them from being mapped
mappings.append((n1, n2))
n1 -= 1
n2 -= 1
elif action == ADD:
if file2[n2 - 1] != "":
rightList.append((n2, file2[n2 - 1]))
n2 -= 1
elif action == DELETE:
if file1[n1 - 1] != "":
leftList.append((n1, file1[n1 - 1]))
n1 -= 1
elif action == CHANGE:
n1 -= 1
n2 -= 1
else:
assert False, "unreachable" # fail state
leftList.reverse()
rightList.reverse()
mappings.reverse()
# MAKING CANDIDATE LISTS (streamed, keep top-K per left line to save memory)
# create Simhash tuples for unmapped lines
hashLeft = [(ln, Simhash(text)) for ln, text in leftList]
hashRight = [(ln, Simhash(text)) for ln, text in rightList]
K = 15 # simhash comparison "constant"
finalCandidates = []
# For each left-line, stream over right-lines and keep K best by hamming distance using heapq.nsmallest
for left_ln, left_hash in hashLeft:
pairs_iter = ((left_ln, right_ln, bin(left_hash.value ^ right_hash.value).count('1'))
for right_ln, right_hash in hashRight)
best_hashes = heapq.nsmallest(K, pairs_iter, key=lambda x: x[2])
# compute combined similarity immediately for the kept candidates (avoid storing huge candidate matrix)
for leftLine, rightLine, _sim in best_hashes:
# CONTENT SIMILARITY SCORE
contentSim = 1 - normalLevenshtein(file1[leftLine - 1], file2[rightLine - 1])
score = 0.6 * contentSim
# CONTEXT SIMILARITY SCORE (build context strings using generator + join to be slightly lighter)
leftContextInterval = range(max(leftLine - 4, 1), min(leftLine + 4, f1) + 1)
rightContextInterval = range(max(rightLine - 4, 1), min(rightLine + 4, f2) + 1)
leftContext = "\n".join(file1[n - 1] for n in leftContextInterval if file1[n - 1] != "")
rightContext = "\n".join(file2[m - 1] for m in rightContextInterval if file2[m - 1] != "")
contextSim = cosineSimilarity(leftContext, rightContext)
score += 0.4 * contextSim
if score > 0.45:
finalCandidates.append([leftLine, rightLine, score])
#SELECT BEST MAPPINGS (INJECTIVE A -> B with no repeats or overlapping)
finalCandidates.sort(reverse=True, key=lambda x: x[2]) #IMPORTANT TO SORT THE CANDIDATES IN DESCENDING ORDER
f = 0
while f < len(finalCandidates):
finalCandidates = [map for map in finalCandidates if not (finalCandidates.index(map) != f and map[0] == finalCandidates[f][0])] #removes inferior left to right mappings
finalCandidates = [map for map in finalCandidates if not (finalCandidates.index(map) != f and finalCandidates[f][1] == map[1])] #removes mappings to same line on right
f += 1
for m in range(len(finalCandidates)):
mappings.append((finalCandidates[m][0], finalCandidates[m][1]))
# REMOVE NEWLY MAPPED LINES from candidate lists
l = 0
while l < len(leftList):
for c in range(len(finalCandidates)):
if leftList[l][0] == finalCandidates[c][0]:
leftList.pop(l)
l -= 1
break
l += 1
r = 0
while r < len(rightList):
for c in range(len(finalCandidates)):
if rightList[r][0] == finalCandidates[c][1]:
rightList.pop(r)
r -= 1
break
r += 1
#REMOVE NON-CONSECUTIVE RIGHT LIST LINES (Not eligible for line split detection)
r = 0
while r < len(rightList):
current = rightList[r][0]
hasLeftNeighbour = r > 0 and rightList[r - 1][0] == current - 1
hasRightNeighbour = r < len(rightList) - 1 and rightList[r + 1][0] == current + 1
if not (hasLeftNeighbour or hasRightNeighbour):
rightList.pop(r)
else:
r += 1
# NEXT: LINE SPLIT DETECTION
# admittedly kind of a mess (most convoluted section): trying to iterate through the unmapped lines from right list, their concatenations
maxLineSplitSim = 0
if leftList:
for l in leftList:
lineSplitsRight = []
for i in range(len(rightList) - 1):
maxLineSplitSim = 0
concatenate = rightList[i][1]
hasRightNeighbour = rightList[i][0] == rightList[i+1][0] - 1
subLineSplits = [rightList[i][0]]
for j in range(1, min(8, len(rightList) - i)): # concatenate a maximum of 8 lines
if hasRightNeighbour:
if rightList[i + j][0] - rightList[i][0] <= 8: # make sure line being concatenated is within the 8 limit
concatenate += rightList[i + j][1]
else:
break
distance = 1 - normalLevenshtein(l[1], concatenate)
if distance >= maxLineSplitSim: # [swap distance > max with distance >= max]
maxLineSplitSim = distance
else:
break
subLineSplits.append(rightList[i + j][0])
else:
break
subLineSplits.insert(0, maxLineSplitSim) # levenshtein score to front for easy access
lineSplitsRight.append(subLineSplits)
if maxLineSplitSim > 0.85: # VERY HIGH THRESHOLD FOR LINE SPLIT MAPPINGS
lineSplitsRight.sort(reverse=True)
mappings.append((l[0], lineSplitsRight[0][1:])) #add best multi-line mapping to specific left list line to list
#might need to consider implementing logic for edge cases where leftlist lines overlap on their mappings to rightlist line splits? (unlikely)
# From some tests, many line splits are not mapped because parts of them are directly mapped instead (threshold for regular mappings might be too low)
mappings.sort()
return mappings
if __name__ == '__main__':
root = tk.Tk()
root.title("LHdiff")
top = tk.Frame(root)
top.pack(fill="x", padx=10, pady=10)
top.grid_columnconfigure(0, weight=1)
top.grid_columnconfigure(1, weight=1)
oldEntry = InputGUI(top, "Enter the old file: ", 0)
newEntry = InputGUI(top, "Enter the new file: ", 1)
error = tk.Label(top, text="", fg="red")
error.grid(row=3, column=0, columnspan=2)
bottom = tk.Frame(root)
bottom.pack(fill="both", expand=True)
left = tk.Frame(bottom)
right = tk.Frame(bottom)
left.pack(side="left", fill="both", expand=True)
right.pack(side="right", fill="both", expand=True)
tk.Label(left, text="Old File:", font=("Arial", 16, "bold")).pack(anchor="w")
tk.Label(right, text="New File:", font=("Arial", 16, "bold")).pack(anchor="w")
oldText = tk.Text(left, wrap="word")
oldText.pack(side="left", fill="both", expand=True)
newText = tk.Text(right, wrap="word")
newText.pack(side="right", fill="both", expand=True)
tk.Button(
top,
text="Enter",
command=lambda:ButtonGUI(oldEntry, newEntry, oldText, newText, error)
).grid(row=2, column=0, columnspan=2, pady=10)
root.mainloop()