-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstringCompare.py
70 lines (65 loc) · 1.78 KB
/
stringCompare.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
#########################################################################
# File: stringCompare.py
# Descriptions: String comparison techniques for file path similarity
# Input: The arguments f1, f2 are strings of file path
# Output: Number of common file path components in f1 and f2
# Written By: Patanamon Thongtanunam ([email protected])
#########################################################################
def path2List(fileString):
return fileString.split("/")
def LCP(f1,f2):
f1 = path2List(f1)
f2 = path2List(f2)
common_path = 0
min_length = min(len(f1),len(f2))
for i in range(min_length):
if f1[i] == f2[i]:
common_path += 1
else:
break
return common_path
def LCSuff(f1,f2):
f1 = path2List(f1)
f2 = path2List(f2)
common_path = 0
r = range(min(len(f1),len(f2)))
r.reverse()
for i in r:
if f1[i] == f2[i]:
common_path += 1
else:
break
return common_path
def LCSubstr(f1,f2):
f1 = path2List(f1)
f2 = path2List(f2)
common_path = 0
if len( set(f1) & set(f2)) > 0:
mat = [[0 for x in range(len(f2)+1)] for x in range(len(f1)+1)]
for i in range(len(f1)+1):
for j in range(len(f2)+1):
if i == 0 or j == 0:
mat[i][j] = 0
elif f1[i-1] == f2[j-1]:
mat[i][j] = mat[i-1][j-1] + 1
common_path = max(common_path,mat[i][j])
else:
mat[i][j] = 0
return common_path
def LCSubseq(f1,f2):
f1 = path2List(f1)
f2 = path2List(f2)
if len( set(f1) & set(f2)) > 0:
L = [[0 for x in range(len(f2)+1)] for x in range(len(f1)+1)]
for i in range(len(f1)+1):
for j in range(len(f2)+1):
if i == 0 or j == 0:
L[i][j] = 0
elif f1[i-1] == f2[j-1]:
L[i][j] = L[i-1][j-1] + 1
else:
L[i][j] = max(L[i-1][j], L[i][j-1])
common_path = L[len(f1)][len(f2)]
else:
common_path = 0
return common_path