-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprocess.py
193 lines (167 loc) · 5.35 KB
/
process.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
from multiprocessing import Pool
import time
from tqdm import *
from apted import APTED
from apted.helpers import Tree
import pandas as pd
import numpy as np
import glob, os
import itertools
from bs4 import BeautifulSoup
import bs4
from collections import OrderedDict, Callable, defaultdict
class DefaultOrderedDict(OrderedDict):
# Source: http://stackoverflow.com/a/6190500/562769
def __init__(self, default_factory=None, *a, **kw):
if (default_factory is not None and
not isinstance(default_factory, Callable)):
raise TypeError('first argument must be callable')
OrderedDict.__init__(self, *a, **kw)
self.default_factory = default_factory
def __getitem__(self, key):
try:
return OrderedDict.__getitem__(self, key)
except KeyError:
return self.__missing__(key)
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
self[key] = value = self.default_factory()
return value
def __reduce__(self):
if self.default_factory is None:
args = tuple()
else:
args = self.default_factory,
return type(self), args, None, None, self.items()
def copy(self):
return self.__copy__()
def __copy__(self):
return type(self)(self.default_factory, self)
def __deepcopy__(self, memo):
import copy
return type(self)(self.default_factory,
copy.deepcopy(self.items()))
def __repr__(self):
return 'OrderedDefaultDict(%s, %s)' % (self.default_factory,
OrderedDict.__repr__(self))
def defaultVal():
return [[],0]
def makeTree(file):
with open(file,"r") as f:
S = f.read()
S = S.strip()
S = S.replace("\n","")
#S = S.replace(" ","")
S = S.replace("\t","")
S = S.replace("\r","")
soup = BeautifulSoup(S, "html.parser")
return soup
def recursiveChildBfs(bs):
root = bs
stack = [root]
count=0
parrent = [None]
while len(stack) != 0:
node = stack.pop(0)
pnode = parrent.pop(0)
if node is not bs:
if node.name!=None:
yield node.name+"~"+str(count),pnode
else:
yield node.name,pnode
if hasattr(node, 'children'):
for child in node.children:
stack.append(child)
parrent.append(node.name+"~"+str(count))
count+=1
def visit(tagdict,c,tree):
tree+="{"
tree+=c.split("~")[0]
for i in tagdict[c][0]:
tree = visit(tagdict,i,tree)
tree+="}"
return tree
def generateTree(file):
html = makeTree(file)
tagdict = DefaultOrderedDict(defaultVal)
for c,p in recursiveChildBfs(html):
if c!=None:
tagdict[p][0].append(c)
tagdict[p][1]+=1
tree = "{"
for x,y in zip(list(tagdict.keys())[1::],list(tagdict.values())[1::]):
tree+=x.split("~")[0]
for c in y[0]:
#tree+="{"
#tree+=c
tree = visit(tagdict,c,tree)
tree+="}"
tree+="}"
break
nNodes = 0
for x in tagdict.keys():
nNodes+=tagdict[x][1]
return tree,nNodes
def calculateDomDiveristy(X):
tree1,n1 = generateTree(X[0])
tree2,n2 = generateTree(X[1])
t1 = Tree.from_text(tree1)
t2 = Tree.from_text(tree2)
apted = APTED(t1, t2)
ted = apted.compute_edit_distance()
DD = ted/max(n1,n2)
return DD,X,ted
if __name__ == '__main__':
c = input("Enter choice Q or C:").lower()
if c=="q":
path = "./Q_Result/"
exclude = [path+"index.html",path+"temp.html"]
states = [file for file in glob.glob(path+"*.html") if file not in exclude]
elif c=="c":
path = "./doms/"
exclude = [path+"temp.html"]
states = [file for file in glob.glob(path+"*.html") if file not in exclude]
else:
print("wrong choice")
exit(1)
val = []
def lol():
return -1
Q = defaultdict(lol)
with Pool() as p:
max_ = len(states)
with tqdm(total=max_) as pbar:
pairs = [x for x in itertools.product(states,states) if x[0]!=x[1]]
blacklist = []
T = []
for x,y in pairs:
if (x,y) not in blacklist:
T.append((x,y))
blacklist.append((y,x))
for i, v in enumerate(p.imap_unordered(calculateDomDiveristy, T)):
val.append(v[0])
if Q["--".join(v[1])]==-1:
if v[2]<=3:
Q["--".join(v[1])] = 1
Q["--".join(v[1][::-1])] = 2
pbar.update()
print("\nDom Diversity: ",np.mean(val))
blacklistQ = set()
statesQU = set()
for x in Q.keys():
p = x.split("--")
if Q[x]==1:
blacklistQ.add(p[1])
elif Q[x]==2:
blacklistQ.add(p[0])
for x in Q.keys():
p = x.split("--")
if p[0] not in blacklistQ:
statesQU.add(p[0])
if p[1] not in blacklistQ:
statesQU.add(p[1])
print("DOM uniquness: ",1-(len(statesQU)/len(states)))
with open(c+"-diversity.txt","w") as ans:
ans.write(str(np.mean(val))+"\n")
ans.write(str(1-(len(statesQU)/len(states)))+"\n")