-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.py
More file actions
697 lines (583 loc) · 27.6 KB
/
Collection.py
File metadata and controls
697 lines (583 loc) · 27.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import datetime
import nltk
import string
import os
import math
import matplotlib.pyplot as plt
from itertools import groupby
from threading import Thread, RLock
from queue import Queue
nbThreadMap = 2
nbThreadReduce = 2
def intToVBCode(number):
""" Convert an integer into a byte array in Variable Byte Code (VBC) """
if not isinstance(number, int):
raise TypeError("number converted in VB code must be an integer")
if number < 0:
raise ValueError("number converted in VB code must be positive")
b = number % 128
a = number // 128
result = [128 + b]
while a > 0:
b = a % 128
a = a // 128
result.insert(0, b)
return bytearray(result)
def VBCodeToFirstInt(file):
""" Read an open file to get the first number in VB code into an integer """
value = 0
byte = file.read(1)
if byte==b'':
return None
while int.from_bytes(byte, byteorder='big') < 128:
value = 128 * value + int.from_bytes(byte, byteorder='big')
byte = file.read(1)
value = 128 * value + (int.from_bytes(byte, byteorder='big') - 128)
return value
class Collection:
""" Main class to deal with collections """
def __init__(self, indexLocation = None):
self.indexLocation = indexLocation # Location on hard-drive to save the inverted index
self.termId = {}
self.termLen = 0
self.docId = {}
self.docLen = 0
self.list = []
self.invertedIndex = []
self.commonWords = []
self._getCommonWords()
if self.indexLocation is not None and not os.path.exists(self.indexLocation):
os.makedirs(self.indexLocation)
def _getCommonWords(self):
""" Get common words from a file in the CACM folder """
with open("Data/CACM/common_words", mode='r') as file:
self.commonWords = file.read().splitlines()
self.commonWords += list(string.punctuation)
def _indexToBinary(self, file):
""" Convert the inverted index in VB Code to be saved in an open file """
for indexTerm in self.invertedIndex:
vbcode = bytearray([])
term_code = intToVBCode(indexTerm[0])
previous_posting_id = 0
for posting in indexTerm[1]:
posting_id_diff = posting[0] - previous_posting_id
previous_posting_id = posting[0]
term_code.extend(intToVBCode(posting_id_diff))
term_code.extend(intToVBCode(posting[1]))
# An integer is put at the beginning of this byte array for the current term in order to know how many
# postings there are before the next term
vbcode.extend(intToVBCode(len(indexTerm[1])))
vbcode.extend(term_code)
file.write(vbcode)
def _binaryToIndex(self, file):
""" Read an open file in VB Code to get the inverted index """
nbPostings = VBCodeToFirstInt(file)
while nbPostings is not None:
termId = VBCodeToFirstInt(file)
postings = []
previousPostingId = 0
for i in range(nbPostings):
postingId = VBCodeToFirstInt(file) + previousPostingId
postingCount = VBCodeToFirstInt(file)
postings.append((postingId, postingCount))
previousPostingId = postingId
self.invertedIndex.append((termId, postings))
nbPostings = VBCodeToFirstInt(file)
def saveIndex(self):
""" Save the inverted index on hard-drive """
if self.indexLocation is not None:
# Save invertedIndex in variable byte code
with open(self.indexLocation + "/invertedIndex", mode="wb") as file:
self._indexToBinary(file)
# Save termId
_termById = {self.termId[term]: term for term in self.termId}
_termLen = len(_termById)
with open(self.indexLocation + "/termId", mode="w") as file:
for termId in range(_termLen):
file.write(str(termId) + " " + str(_termById[termId]) + "\n")
# Save docId
_docById = {self.docId[doc]: doc for doc in self.docId}
with open(self.indexLocation + "/docId", mode="w") as file:
for docId in _docById:
file.write(str(docId) + " " + str(_docById[docId]) + "\n")
else:
print("No location specified to save inverted index.")
def loadIndex(self):
""" Load inverted index from hard-drive """
if self.indexLocation is not None:
# Load invertedIndex
self.invertedIndex = []
with open(self.indexLocation + "/invertedIndex", mode="rb") as file:
self._binaryToIndex(file)
# Load termId
self.termId = {}
with open(self.indexLocation + "/termId", mode="r") as file:
line = file.readline().replace("\n", "")
while line != "":
termId = line.split(" ")[0]
term = line.split(" ")[1]
self.termId[term] = int(termId)
line = file.readline().replace("\n", "")
self.termLen = len(self.termId)
# Load docId
self.docId = {}
with open(self.indexLocation + "/docId", mode="r") as file:
line = file.readline().replace("\n", "")
while line != "":
docId = line.split(" ")[0]
doc = line.split(" ")[1]
self.docId[doc] = int(docId)
line = file.readline().replace("\n", "")
self.docLen = len(self.docId)
else:
print("No location specified to load inverted index.")
def getTermId(self, term):
return self.termId[term]
class CACMCollection(Collection):
def __init__(self, indexLocation = "indexCACM"):
Collection.__init__(self, indexLocation)
def answerQuestion(self):
""" To answer the questions from the exercise about collections """
common_words = self.commonWords
nb_documents = 0
tokens = []
vocabulary_frequency = {}
half_tokens = []
half_vocabulary_frequency = {}
with open("Data/CACM/cacm.all", mode="r") as all:
readLine = False
for line in all:
if line[:1] == ".":
readLine = False
if readLine:
new_tokens = [x for x in nltk.wordpunct_tokenize(line.lower()) if x not in common_words]
tokens += new_tokens
if nb_documents % 2 == 1:
half_tokens += new_tokens
if line[:2] == ".I":
nb_documents += 1
if line[:2] in [".T", ".W", ".K"]:
readLine = True
for token in tokens:
if token not in vocabulary_frequency:
vocabulary_frequency[token] = 1
else:
vocabulary_frequency[token] += 1
nb_tokens = len(tokens)
size_vocabulary = len(vocabulary_frequency)
for token in half_tokens:
if token not in half_vocabulary_frequency:
half_vocabulary_frequency[token] = 1
else:
half_vocabulary_frequency[token] += 1
nb_half_tokens = len(half_tokens)
size_half_vocabulary = len(half_vocabulary_frequency)
b = math.log(size_vocabulary / size_half_vocabulary) / math.log(nb_tokens / nb_half_tokens)
k = size_vocabulary/(math.pow(nb_tokens, b))
token_estimation = 1000000
estimated_size = int(k * math.pow(token_estimation, b))
print('\nExercice 1')
print(f'{nb_tokens} tokens found in this collection.')
print('\nExercice 2')
print(f'{size_vocabulary} words in the vocabulary.')
print('\nExercice 3')
print('For half of the collection, we get:')
print(f'{nb_half_tokens} tokens')
print(f'{size_half_vocabulary} words in the vocabulary.')
print("Parameters for Heaps' law are therefore:")
print(f" k = {k} b = {b}")
print('\nExercice 4')
print(f"With Heaps' law, vocabulary size for {token_estimation} tokens would be {estimated_size} words.")
frequencies = list(vocabulary_frequency.values())
frequencies.sort()
frequencies = frequencies[::-1]
ranks = range(1, len(frequencies) + 1)
logfrequencies = [math.log(x) for x in frequencies]
logranks = [math.log(x) for x in ranks]
f, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot(ranks, frequencies)
ax1.set_title('Frequencies in relation to Ranks')
ax2.scatter(logranks, logfrequencies)
ax2.set_title('log(Frequencies) in relation to log(Ranks)')
plt.show()
def constructIndex(self):
""" Construct inverted index for CACM Collection (in memory) """
# Class to describe a CACM document
class _Document:
def __init__(self, ID):
self.ID = ID
self.title = ""
self.summary = ""
self.keywords = ""
# Common words
common_words = self.commonWords
# Reading all documents line by line
all = open("Data/CACM/cacm.all", mode="r")
documents = []
readTitle = False
readSummary = False
readKeywords = False
for line in all:
if line[:1] == ".":
readTitle = False
readSummary = False
readKeywords = False
if readTitle:
document.title = document.title + "\n" + line.lower()
if readSummary:
document.summary = document.summary + "\n" + line.lower()
if readKeywords:
document.keywords = document.keywords + "\n" + line.lower()
if line[:2] == ".I":
document = _Document(int(line.split(" ")[-1].replace("\n","")))
documents.append(document)
if line[:2] == ".T":
readTitle = True
readSummary = False
readKeywords = False
if line[:2] == ".W":
readTitle = False
readSummary = True
readKeywords = False
if line[:2] == ".K":
readTitle = False
readSummary = False
readKeywords = True
self.docLen = len(documents)
# Token identification and list of term id / doc id
for document in documents:
self.docId[document.ID] = document.ID
documentTokens = []
documentTokens += nltk.wordpunct_tokenize(document.title)
documentTokens += nltk.wordpunct_tokenize(document.summary)
documentTokens += nltk.wordpunct_tokenize(document.keywords)
documentTokens = [x for x in documentTokens if not x in common_words]
for token in documentTokens:
if token in self.termId:
termId = self.termId[token]
else:
termId = self.termLen
self.termLen += 1
self.termId[token] = termId
self.list.append((termId,document.ID))
self.list.sort()
# Inverted index creation
self.invertedIndex = [(key, [x[1] for x in group]) for key, group in groupby(self.list, key=lambda x: x[0])]
self.invertedIndex = [(y[0], sorted(set([(z, y[1].count(z)) for z in y[1]]))) for y in self.invertedIndex]
def queryTest(self):
""" To get queries and awaited responses for test """
class Query:
def __init__(self, id):
self.id = id
self.query = ""
self.results = []
with open("Data/CACM/query.text", mode="r") as queryText:
queries = []
read_query = False
for queryLine in queryText:
if queryLine[:1] == ".":
read_query = False
if read_query:
query.query = (query.query + " " + queryLine.lower()).replace("\n", "")
while query.query[0] == " ":
query.query = query.query[1:]
if queryLine[:2] == ".I":
query = Query(int(queryLine.split(" ")[-1].replace("\n", "")))
with open("Data/CACM/qrels.text", mode="r") as qrelsText:
for qrelsLine in qrelsText:
if int(qrelsLine.split(" ")[0].replace("\n", "")) == query.id:
query.results.append(int(qrelsLine.split(" ")[1].replace("\n", "")))
queries.append(query)
if queryLine[:2] == ".W":
read_query = True
return queries
class CS276Collection(Collection):
def __init__(self, indexLocation = "indexCS276"):
Collection.__init__(self, indexLocation)
def answerQuestion(self):
""" To answer the questions from the exercise about collections """
common_words = self.commonWords
nb_documents = 0
tokens = []
vocabulary_frequency = {}
half_tokens = []
half_vocabulary_frequency = {}
for blockID in range(10):
documentsNames = os.listdir("Data/CS276/pa1-data/" + str(blockID))
for documentName in documentsNames:
with open("Data/CS276/pa1-data/" + str(blockID) + "/" + documentName, mode="r") as documentFile:
nb_documents += 1
documentContent = documentFile.read().replace("\n", " ")
new_tokens = [x for x in nltk.wordpunct_tokenize(documentContent.lower()) if x not in common_words]
tokens += new_tokens
if nb_documents % 2 == 1:
half_tokens += new_tokens
for token in tokens:
if token not in vocabulary_frequency:
vocabulary_frequency[token] = 1
else:
vocabulary_frequency[token] += 1
nb_tokens = len(tokens)
size_vocabulary = len(vocabulary_frequency)
for token in half_tokens:
if token not in half_vocabulary_frequency:
half_vocabulary_frequency[token] = 1
else:
half_vocabulary_frequency[token] += 1
nb_half_tokens = len(half_tokens)
size_half_vocabulary = len(half_vocabulary_frequency)
del half_tokens
del half_vocabulary_frequency
b = math.log(size_vocabulary / size_half_vocabulary) / math.log(nb_tokens / nb_half_tokens)
k = size_vocabulary/(math.pow(nb_tokens, b))
token_estimation = 1000000
estimated_size = int(k * math.pow(token_estimation, b))
print('\nExercice 1')
print(f'{nb_tokens} tokens found in this collection.')
print('\nExercice 2')
print(f'{size_vocabulary} words in the vocabulary.')
print('\nExercice 3')
print('For half of the collection, we get:')
print(f'{nb_half_tokens} tokens')
print(f'{size_half_vocabulary} words in the vocabulary.')
print("Parameters for Heaps' law are therefore:")
print(f" k = {k} b = {b}")
print('\nExercice 4')
print(f"With Heaps' law, vocabulary size for {token_estimation} tokens would be {estimated_size} words.")
frequencies = list(vocabulary_frequency.values())
frequencies.sort()
frequencies = frequencies[::-1]
ranks = range(1, len(frequencies) + 1)
logfrequencies = [math.log(x) for x in frequencies]
logranks = [math.log(x) for x in ranks]
f, (ax1, ax2) = plt.subplots(2, 1)
ax1.plot(ranks, frequencies)
ax1.set_title('Frequencies in relation to Ranks')
ax2.scatter(logranks, logfrequencies)
ax2.set_title('log(Frequencies) in relation to log(Ranks)')
plt.show()
def parseBlock(self, blockID):
""" Create a partial inverted index (in memory) for one block """
global nbThreadMap, nbThreadReduce
self.invertedIndex = []
# Common words list
common_words = open("Data/CACM/common_words", mode='r').read().splitlines()
common_words += list(string.punctuation)
print("Generating index for block " + str(blockID) + "...")
list_after_mapper = []
docLen = self.docLen
docId = self.docId
termLen = self.termLen
termId = self.termId
locker_list = RLock()
locker_doc = RLock()
locker_term = RLock()
locker_index = RLock()
class Mapper(Thread):
""" Mapper for the map reduce to get tokens in documents """
def __init__(self, documentsNames):
super().__init__()
self.tasks = documentsNames
self.daemon = True
self.start()
def run(self):
nonlocal list_after_mapper, docLen, docId, termLen, termId, common_words
while True:
documentName = self.tasks.get()
try:
# Open the document
documentFile = open("Data/CS276/pa1-data/" + str(blockID) + "/" + documentName, mode="r")
documentContent = documentFile.read().replace("\n", " ")
documentFile.close()
# Add this document's name to the list of doc id
with locker_doc:
doc_id = docLen
docId[str(blockID) + "/" + documentName] = doc_id
docLen += 1
# Tokenize document content
documentTokens = nltk.wordpunct_tokenize(documentContent)
documentTokens = [x for x in documentTokens if not x in common_words]
for token in documentTokens:
with locker_term:
if token in termId:
term_id = termId[token]
else:
term_id = termLen
termLen += 1
termId[token] = term_id
with locker_list:
list_after_mapper.append((term_id, doc_id))
finally:
# Mark this task as done, whether an exception happened or not
self.tasks.task_done()
class MapperPool:
""" Pool of mappers consuming tasks from a queue (of document names) """
def __init__(self, num_threads):
self.tasks = Queue(num_threads)
for _ in range(num_threads):
Mapper(self.tasks)
def add_task(self, documentName):
""" Add a task to the queue """
self.tasks.put(documentName)
def wait_completion(self):
""" Wait for completion of all the tasks in the queue """
self.tasks.join()
# Loading all documents of the current block in a mapper
documentsNames = os.listdir("Data/CS276/pa1-data/" + str(blockID))
mapper_pool = MapperPool(nbThreadMap)
for name in documentsNames:
mapper_pool.add_task(name)
mapper_pool.wait_completion()
self.docId = docId
self.docLen = docLen
self.termId = termId
self.termLen = termLen
list_after_mapper.sort()
list_before_reducer = [(key, [x[1] for x in group]) for key, group in groupby(list_after_mapper, key=lambda x: x[0])]
invertedIndex = []
class Reducer(Thread):
""" Reducer for the map reduce to get have a list of postings for each term """
def __init__(self, postings):
super().__init__()
self.tasks = postings
self.daemon = True
self.start()
def run(self):
nonlocal invertedIndex
while True:
postings_list = self.tasks.get()
try:
with locker_index:
invertedIndex.append(
(postings_list[0], sorted(set([(z, postings_list[1].count(z)) for z in postings_list[1]]))))
finally:
# Mark this task as done, whether an exception happened or not
self.tasks.task_done()
class ReducerPool:
""" Pool of reducers consuming tasks from a queue (of lists of postings) """
def __init__(self, num_threads):
self.tasks = Queue(num_threads)
for _ in range(num_threads):
Reducer(self.tasks)
def add_task(self, postings):
""" Add a task to the queue """
self.tasks.put(postings)
def wait_completion(self):
""" Wait for completion of all the tasks in the queue """
self.tasks.join()
reducer_pool = ReducerPool(nbThreadReduce)
for postings in list_before_reducer:
reducer_pool.add_task(postings)
reducer_pool.wait_completion()
# Creating inverted index
invertedIndex.sort()
self.invertedIndex = invertedIndex
print("Index for block " + str(blockID) + " generated.")
# Release memory
del documentsNames
del list_after_mapper
del list_before_reducer
def saveBlockIndex(self, blockID):
""" Save the current partial inverted index for one block """
if self.indexLocation is not None:
# Save invertedIndex in variable byte code
with open(self.indexLocation + "/" + str(blockID), mode="wb") as file:
self._indexToBinary(file)
else:
print("No location specified to save inverted index for block " + str(blockID) + ".")
# Release memory
self.invertedIndex = []
def mergeBlockIndex(self, blockIndexFiles):
""" Merge all partial inverted index previously saved to get the whole inverted index """
# Reading the first element from each index
currentTermId = {}
currentPostings = {}
for blockID in blockIndexFiles.keys():
nbPostings = VBCodeToFirstInt(blockIndexFiles[blockID])
if nbPostings is not None:
currentTermId[blockID] = VBCodeToFirstInt(blockIndexFiles[blockID])
currentPostings[blockID] = []
previousPostingId = 0
for i in range(nbPostings):
postingId = VBCodeToFirstInt(blockIndexFiles[blockID]) + previousPostingId
postingCount = VBCodeToFirstInt(blockIndexFiles[blockID])
currentPostings[blockID].append((postingId, postingCount))
previousPostingId = postingId
else:
# Closing the block if not any term id is found
blockIndexFiles[blockID].close()
print("Block " + str(blockID) + " has been closed.")
del blockIndexFiles[blockID]
del currentTermId[blockID]
del currentPostings[blockID]
# Merging all elements with the smallest term id and reading the next element of those blocks
# (or close the file if it was the last element)
termId = 0
while termId < self.termLen:
blocksToMerge = [blockID for blockID in currentTermId.keys() if currentTermId[blockID] == termId]
self.invertedIndex.append([termId, []])
for blockID in blocksToMerge:
# Add postings from this block for this term id to the list
self.invertedIndex[termId][1].extend(currentPostings[blockID])
# Reading the next line or closing this block.
nbPostings = VBCodeToFirstInt(blockIndexFiles[blockID])
if nbPostings is not None:
currentTermId[blockID] = VBCodeToFirstInt(blockIndexFiles[blockID])
currentPostings[blockID] = []
previousPostingId = 0
for i in range(nbPostings):
postingId = VBCodeToFirstInt(blockIndexFiles[blockID]) + previousPostingId
postingCount = VBCodeToFirstInt(blockIndexFiles[blockID])
currentPostings[blockID].append((postingId, postingCount))
previousPostingId = postingId
else:
# Closing the block
blockIndexFiles[blockID].close()
print("Block " + str(blockID) + " has been closed.")
del blockIndexFiles[blockID]
del currentTermId[blockID]
del currentPostings[blockID]
termId += 1
def constructIndex(self):
""" Construct the inverted index block by block """
# Write inverted index for each block
for blockID in range(10):
self.parseBlock(blockID)
# Write index in Hard Drive
self.saveBlockIndex(blockID)
print("Index for block " + str(blockID) + " saved.")
# Open files to merge all inverted index
blockIndexFiles = {}
for blockID in range(10):
blockIndexFiles[blockID] = open(self.indexLocation + "/" + str(blockID), mode="rb")
# Merging
print("Merging index from all blocks...")
self.mergeBlockIndex(blockIndexFiles)
print("Index merged.")
if __name__ == "__main__":
# Collection choice
collection_name = ""
while collection_name not in ['CACM', 'CS276']:
collection_name = input("Choose a collection among 'CACM' and 'CS276'\n> ").upper()
if collection_name == 'CS276':
collection = CS276Collection()
else:
collection = CACMCollection()
answer_questions = ""
while answer_questions not in ['Y', 'YES', 'N', 'NO']:
answer_questions = input("Do you want to answer the questions about collections ? (YES or NO)\n> ").upper()
if answer_questions in ['Y', 'YES']:
collection.answerQuestion()
elif os.path.isfile('index' + collection_name + '/docId') and os.path.isfile('index' + collection_name + '/termId') \
and os.path.isfile('index' + collection_name + '/invertedIndex'):
print("Start loading...")
collection.loadIndex()
print("Index loaded.")
else:
start_time = datetime.datetime.now()
collection.constructIndex()
print(f"Index constructed. Time elapsed: {(datetime.datetime.now()-start_time).seconds}s")
collection.saveIndex()
print(f"Index saved. Time elapsed: {(datetime.datetime.now()-start_time).seconds}s")
collection.loadIndex()
print(f"Index loaded. Time elapsed: {(datetime.datetime.now()-start_time).seconds}s")