-
Notifications
You must be signed in to change notification settings - Fork 2
/
MINC_FragmentIntent.py
287 lines (275 loc) · 13.5 KB
/
MINC_FragmentIntent.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import sys
import os
import time, argparse
from bitmap import BitMap
import ParseConfigFile as parseConfig
import QueryParser as qp
import TupleIntent as ti
import re
import MINC_prepareJoinKeyPairs
from ParseConfigFile import getConfig
import random
import ReverseEnggQueries
import CFCosineSim_Parallel
#import MINC_QueryParser as MINC_QP
def seqIntentVectorFilesModifyCrawler(configDict):
splitDir = getConfig(configDict['BIT_FRAGMENT_SEQ_SPLITS'])
splitFileName = configDict['BIT_FRAGMENT_SEQ_SPLIT_NAME_FORMAT']
numFiles = int(configDict['BIT_FRAGMENT_SEQ_SPLIT_THREADS'])
sessionQueryDict = {} # key is session ID and value is line
queryCount = 0
prevSessName = None
prevQueryVector = None
#sessID = int(configDict['BIT_FRAGMENT_START_SESS_INDEX'])-1
sessID = -1
sessQueryID = float("-inf")
queryLimit = int(configDict['QUERY_LIMIT'])
for i in range(numFiles):
fileNamePerThread = splitDir+"/"+splitFileName+str(i)
with open(fileNamePerThread) as f:
for line in f:
line = line.strip()
if len(line.split(";")) > 3:
line = removeExcessDelimiters(line)
assert len(line.split(";")) == 3
tokens = line.split(";")
sessName = tokens[0].split(", ")[0].split(" ")[1]
if sessName != prevSessName:
if sessID in sessionQueryDict and len(sessionQueryDict[sessID]) == 0:
del sessionQueryDict[sessID] # empty previous session
else:
sessID+=1
prevSessName = sessName
prevQueryVector = None
if sessID >= int(configDict['BIT_FRAGMENT_START_SESS_INDEX']):
if sessID not in sessionQueryDict:
sessionQueryDict[sessID] = []
sessQueryID = 1 # since some query indices may be pruned in between
curQueryVector = BitMap.fromstring(tokens[2])
if prevQueryVector is None or CFCosineSim_Parallel.computeBitCosineSimilarity(curQueryVector, prevQueryVector) < 0.99:
newLine = "Session "+sessName+", Query "+str(sessQueryID) +";"+tokens[1]+";"+tokens[2]
sessionQueryDict[sessID].append(newLine)
sessQueryID += 1
prevQueryVector = curQueryVector
queryCount +=1
if queryLimit != 0 and queryCount > queryLimit: # 0 indicates no limit
break
if queryCount % 1000 == 0:
print ("Query count so far: "+str(queryCount)+", len(sessionQueryDict): "+str(len(sessionQueryDict)))
return sessionQueryDict
def seqIntentVectorFilesPruneCrawler(configDict):
splitDir = getConfig(configDict['BIT_FRAGMENT_SEQ_SPLITS'])
splitFileName = configDict['BIT_FRAGMENT_SEQ_SPLIT_NAME_FORMAT']
numFiles = int(configDict['BIT_FRAGMENT_SEQ_SPLIT_THREADS'])
sessionQueryDict = {} # key is session ID and value is line
queryCount = 0
prevSessName = None
prevQueryVector = None
repQuery = "False"
#sessID = int(configDict['BIT_FRAGMENT_START_SESS_INDEX'])-1
sessID = -1
relSessID = -1
queryLimit = int(configDict['QUERY_LIMIT'])
for i in range(numFiles):
fileNamePerThread = splitDir+"/"+splitFileName+str(i)
with open(fileNamePerThread) as f:
for line in f:
line = line.strip()
if len(line.split(";")) > 3:
line = removeExcessDelimiters(line)
assert len(line.split(";")) == 3
tokens = line.split(";")
sessName = tokens[0].split(", ")[0].split(" ")[1]
if sessName != prevSessName:
if repQuery == "True":
assert sessID in sessionQueryDict
del sessionQueryDict[sessID]
assert sessID not in sessionQueryDict
repQuery = "False"
else:
sessID+=1
prevSessName = sessName
prevQueryVector = None
if sessID >= int(configDict['BIT_FRAGMENT_START_SESS_INDEX']):
if sessID not in sessionQueryDict:
sessionQueryDict[sessID] = []
sessionQueryDict[sessID].append(line)
curQueryVector = BitMap.fromstring(tokens[2])
if prevQueryVector is not None and CFCosineSim_Parallel.computeBitCosineSimilarity(curQueryVector,
prevQueryVector) >= 0.99:
repQuery = "True"
prevQueryVector = curQueryVector
queryCount +=1
if queryLimit != 0 and queryCount > queryLimit: # 0 indicates no limit
break
if queryCount % 1000 == 0:
print ("Query count so far: " + str(queryCount) + ", len(sessionQueryDict): " + str(
len(sessionQueryDict)))
return sessionQueryDict
def seqIntentVectorFilesKeepCrawler(configDict):
splitDir = getConfig(configDict['BIT_FRAGMENT_SEQ_SPLITS'])
splitFileName = configDict['BIT_FRAGMENT_SEQ_SPLIT_NAME_FORMAT']
numFiles = int(configDict['BIT_FRAGMENT_SEQ_SPLIT_THREADS'])
sessionQueryDict = {} # key is session ID and value is line
queryCount = 0
prevSessName = None
#sessID = int(configDict['BIT_FRAGMENT_START_SESS_INDEX'])-1
sessID = -1
relSessID = -1
queryLimit = int(configDict['QUERY_LIMIT'])
for i in range(numFiles):
fileNamePerThread = splitDir+"/"+splitFileName+str(i)
with open(fileNamePerThread) as f:
for line in f:
line = line.strip()
if len(line.split(";")) > 3:
line = removeExcessDelimiters(line)
assert len(line.split(";")) == 3
tokens = line.split(";")
sessName = tokens[0].split(", ")[0].split(" ")[1]
if sessName != prevSessName:
sessID+=1
prevSessName = sessName
if sessID >= int(configDict['BIT_FRAGMENT_START_SESS_INDEX']):
if sessID not in sessionQueryDict:
sessionQueryDict[sessID] = []
sessionQueryDict[sessID].append(line)
queryCount +=1
if queryLimit != 0 and queryCount > queryLimit: # 0 indicates no limit
break
if queryCount % 10000 == 0:
print ("Query count so far: "+str(queryCount)+", len(sessionQueryDict): "+str(len(sessionQueryDict)))
return sessionQueryDict
def concatenateSeqIntentVectorFiles(configDict):
splitDir = getConfig(configDict['BIT_FRAGMENT_SEQ_SPLITS'])
splitFileName = configDict['BIT_FRAGMENT_SEQ_SPLIT_NAME_FORMAT']
numFiles = int(configDict['BIT_FRAGMENT_SEQ_SPLIT_THREADS'])
sessionQueryDict = {} # key is session ID and value is line
queryCount = 0
prevSessName = None
sessID = int(configDict['BIT_FRAGMENT_START_SESS_INDEX'])-1
queryLimit = int(configDict['QUERY_LIMIT'])
for i in range(numFiles):
fileNamePerThread = splitDir+"/"+splitFileName+str(i)
with open(fileNamePerThread) as f:
for line in f:
line = line.strip()
if len(line.split(";")) > 3:
line = removeExcessDelimiters(line)
assert len(line.split(";")) == 3
tokens = line.split(";")
sessName = tokens[0].split(", ")[0].split(" ")[1]
if sessName != prevSessName:
sessID+=1
prevSessName = sessName
if sessID not in sessionQueryDict:
sessionQueryDict[sessID] = []
sessionQueryDict[sessID].append(line)
queryCount +=1
if queryLimit != 0 and queryCount > queryLimit: # 0 indicates no limit
break
if queryCount % 10000 == 0:
print ("Query count so far: "+str(queryCount))
return sessionQueryDict
def createQuerySessions(sessionQueryDict, configDict):
sessQueryFile = getConfig(configDict['QUERYSESSIONS'])
try:
os.remove(sessQueryFile)
except OSError:
pass
for sessID in sessionQueryDict:
output_str = "Session "+str(sessID)+";"
for i in range(len(sessionQueryDict[sessID])):
output_str = output_str + sessionQueryDict[sessID][i].split(";")[1].split(": ")[1]+";"
ti.appendToFile(sessQueryFile, output_str)
def removeExcessDelimiters(sessQueryIntent):
tokens = sessQueryIntent.split(";")
ret_str = tokens[0]+";"
for i in range(1,len(tokens)-1):
if i == 1:
ret_str = ret_str+tokens[i]
else:
ret_str = ret_str+","+tokens[i]
ret_str = ret_str + ";"+tokens[len(tokens)-1]
return ret_str
def tryDeletionIfExists(fileName):
try:
os.remove(fileName)
except OSError:
pass
return
def createSingularityIntentVectors(sessionQueryDict, configDict):
intentFile = getConfig(configDict['BIT_FRAGMENT_INTENT_SESSIONS'])
concurrentFile = getConfig(configDict['CONCURRENT_QUERY_SESSIONS'])
tableIntentFile = getConfig(configDict['BIT_FRAGMENT_TABLE_INTENT_SESSIONS'])
schemaDicts = ReverseEnggQueries.readSchemaDicts(configDict)
tryDeletionIfExists(intentFile)
tryDeletionIfExists(concurrentFile)
tryDeletionIfExists(tableIntentFile)
queryCount = 0
queryIndex = 0
absCount = 0
numSessions = len(sessionQueryDict)
while len(sessionQueryDict)!=0:
keyList = sessionQueryDict.keys()
random.shuffle(keyList)
queryIndex += 1
for sessIndex in keyList:
sessQueryIntent = sessionQueryDict[sessIndex][0]
sessionQueryDict[sessIndex].remove(sessQueryIntent)
if len(sessionQueryDict[sessIndex]) == 0:
del sessionQueryDict[sessIndex]
#queryIndexRec = sessQueryIntent.split(";")[0].split(", ")[1].split(" ")[1]
#if queryIndexRec != str(queryIndex):
#print "queryIndexRec != queryIndex !!"
#assert queryIndexRec == str(queryIndex)
tokens = sessQueryIntent.split(";")
assert len(tokens) == 3
assert queryCount>=0
if queryCount == 0:
output_str = "Session " + str(sessIndex) + ", Query " + str(queryIndex) + ";" + tokens[1] + ";" + tokens[2]
output_table_str = "Session " + str(sessIndex) + ", Query " + str(queryIndex) + ";" + tokens[1] + ";" + \
tokens[2][schemaDicts.tableStartBitIndex:schemaDicts.tableStartBitIndex+schemaDicts.tableBitMapSize]
conc_str = "Session " + str(sessIndex) + ", Query " + str(queryIndex) + ";" + tokens[1].split(":")[1]
else:
output_str += "\nSession " + str(sessIndex) + ", Query " + str(queryIndex) + ";" + tokens[1] + ";" + tokens[2]
output_table_str += "\nSession " + str(sessIndex) + ", Query " + str(queryIndex) + ";" + tokens[1] + ";" + \
tokens[2][schemaDicts.tableStartBitIndex:schemaDicts.tableStartBitIndex + schemaDicts.tableBitMapSize]
conc_str += "\nSession " + str(sessIndex) + ", Query " + str(queryIndex) + ";" + tokens[1].split(":")[1]
queryCount += 1
absCount+=1
if queryCount % 100 == 0:
ti.appendToFile(intentFile, output_str)
ti.appendToFile(concurrentFile, conc_str)
ti.appendToFile(tableIntentFile, output_table_str)
queryCount = 0
if absCount % 10000 == 0:
print ("appended Session " + str(sessIndex) + ", Query " + str(queryIndex) + ", absQueryCount: " + str(absCount))
if queryCount > 0:
ti.appendToFile(intentFile, output_str)
ti.appendToFile(concurrentFile, conc_str)
ti.appendToFile(tableIntentFile, output_table_str)
print ("Created intent vectors for # Sessions: "+str(numSessions)+" and # Queries: "+str(absCount))
if __name__ == "__main__":
#configDict = parseConfig.parseConfigFile("MINC_configFile.txt")
parser = argparse.ArgumentParser()
parser.add_argument("-config", help="Config parameters file", type=str, required=True)
args = parser.parse_args()
configDict = parseConfig.parseConfigFile(args.config)
assert configDict["BIT_OR_WEIGHTED"] == "BIT"
fragmentIntentSessionsFile = getConfig(configDict['BIT_FRAGMENT_INTENT_SESSIONS'])
try:
os.remove(fragmentIntentSessionsFile)
except OSError:
pass
assert configDict['KEEP_PRUNE_MODIFY_CRAWLER_SESS'] == "KEEP" or \
configDict['KEEP_PRUNE_MODIFY_CRAWLER_SESS'] == "PRUNE" or \
configDict['KEEP_PRUNE_MODIFY_CRAWLER_SESS'] == "MODIFY"
if configDict['KEEP_PRUNE_MODIFY_CRAWLER_SESS'] == "KEEP":
sessionQueryDict = seqIntentVectorFilesKeepCrawler(configDict)
elif configDict['KEEP_PRUNE_MODIFY_CRAWLER_SESS'] == "PRUNE":
sessionQueryDict = seqIntentVectorFilesPruneCrawler(configDict)
elif configDict['KEEP_PRUNE_MODIFY_CRAWLER_SESS'] == "MODIFY":
sessionQueryDict = seqIntentVectorFilesModifyCrawler(configDict)
createQuerySessions(sessionQueryDict, configDict)
createSingularityIntentVectors(sessionQueryDict, configDict)