/
Merge.py
124 lines (97 loc) · 4.49 KB
/
Merge.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
from pathlib import Path
import sys
from os import listdir
import collections
from collections import OrderedDict
import ast
import json
import time
import tracemalloc
def blockdefinition():
ALLBLOCKFILE =[]
BLOCKSTOMERGE =0
spimi_index = 'Merge/invert_index.txt'
BLOCKPATH = 'index_blocks/'
blockentries = Path('index_blocks/')
for blockentry in blockentries.iterdir():
BLOCKSTOMERGE = BLOCKSTOMERGE+1
#print(BLOCKSTOMERGE)
for blockentry in blockentries.iterdir():
ALLBLOCKFILE.append(blockentry.name)
blockMerge(ALLBLOCKFILE,BLOCKSTOMERGE,BLOCKPATH,spimi_index)
#compression()
def blockMerge(ALLBLOCKFILE,BLOCKSTOMERGE,BLOCKPATH,spimi_index):
print("=============== Merging SPIMI blocks into final inverted index... ===============")
tracemalloc.start()
Filewrite = open('Merge/invert_index.txt',"w+")
iterlist = []
term=""
current_term = ""
startmerge = time.process_time()
for BLOCKFILE in ALLBLOCKFILE:
print("File Name:",BLOCKFILE)
print("-- Reading into memory... ",BLOCKFILE.split(".txt",1)[0])
finaldict={}
l = open(BLOCKPATH + BLOCKFILE)
Fileread = open('Merge/invert_index.txt')
Initialfile= Fileread.read()
if(Initialfile.strip()):
lst = Initialfile.strip().split('\n')
for i in range(len(lst)):
val = lst[i].split(" -----------> ")
finaldict[val[0]] = val[1]
else:
finaldict={}
iterlist = (l.read().strip().split('\n'))
for l2 in range (len(iterlist)):
ksplit = iterlist[l2].split(" -----------> ")# ['aaaaaq', '[[2136, 1]]'] OR ['aam', '[[1782, 1], [1786, 1]]']
if(finaldict.get(ksplit[0])!= None):
postlingvalold = json.loads (finaldict.get(ksplit[0])) # [[1,4],[2,5]]
newblock = json.loads(ksplit[1])
for i in range (len(newblock)):
if newblock[i] not in postlingvalold:
#print("THIS IS THE NEWBLOCK MATCHING THE CONDITION : ", newblock[i])
postlingvalold.append(newblock[i])
finaldict[ksplit[0]] = str(postlingvalold)
else:
current_term = ksplit[0]
term=term+current_term.capitalize()
finaldict[ksplit[0]] = ksplit[1]
sorted(finaldict)
Filewrite = open('Merge/invert_index.txt',"w+")
Filewrite1 = open('Merge/invert_actual_index.txt',"w+")
indexwriter1 = open('Merge/index_cp_1.txt',"w+")
indexwriter1.write(term)
for key,value in sorted(finaldict.items()):
Filewrite.write(key+" -----------> "+ value + "\n")
Filewrite1.write(key+" -----------> "+ value + "\n")
print("Finished merging block: ",BLOCKFILE.split(".txt",1)[0]," and writing to disk")
endmerge = time.process_time()
eachmerge = endmerge -startmerge
print("\n Time taken after each Block merge : ",eachmerge, "\n")
Fileread.close()
Filewrite.close()
Filewrite1.close()
indexwriter1.close()
current, peak = tracemalloc.get_traced_memory()
print(f" After merge : Current memory usage is {current / 10**6}MB")
tracemalloc.stop()
def compression():
indexReader = open('Merge/invert_index.txt') #to be moved
compression_word_list = open('Merge/index_cp_1.txt')
compression_word= compression_word_list.read()
inverted_index_file= indexReader.read()
if(inverted_index_file.strip()):
indexwriter_changed = open('Merge/invert_index.txt',"w+")
lst_elements = inverted_index_file.strip().split('\n')
for i in range(len(lst_elements)):
_each_val = lst_elements[i].split(' -----------> ')
_index_places = compression_word.find(_each_val[0].capitalize())
_each_val[0] = _each_val[0].replace(_each_val[0], str(_index_places))
lst_elements[i] = _each_val[0] +" -----------> "+_each_val[1]
for i in range(len(sorted(lst_elements))):
indexwriter_changed.write(lst_elements[i])
indexwriter_changed.write ("\n")
indexReader.close()
compression_word_list.close()
indexwriter_changed.close()