-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex_generator.py
166 lines (124 loc) · 5.67 KB
/
index_generator.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
import os
import heapq
import json
import math
from dotenv import load_dotenv
from index_generator_helper import *
from token_processing_pipeline import process_csv_file
load_dotenv()
def spimi_invert(token_stream, blocks_dir):
print("DEBUG: Started SPIMI invertion")
block_counter = 0
dictionary = {}
for token in token_stream:
dictionary_checkpoint = dictionary.copy()
token_word = token[0]
token_file_id = token[1]
if token_word in dictionary:
if dictionary[token_word][-1][0] == token_file_id:
dictionary[token_word][-1][1] += 1
else:
dictionary[token_word].append([token_file_id, 1])
else:
dictionary[token_word] = [[token_file_id, 1]]
if get_total_size(dictionary) > int(os.getenv("PAGE_SIZE")):
sort_and_save_block(dictionary_checkpoint, block_counter, blocks_dir)
dictionary = {token_word: [[token_file_id, 1]]}
block_counter += 1
if dictionary:
sort_and_save_block(dictionary, block_counter, blocks_dir)
print("DEBUG: Finished SPIMI invertion")
def create_merged_index(blocks_dir, merge_index_output_path):
print("DEBUG: Started creation of merged index")
block_files = [os.path.join(blocks_dir, f) for f in os.listdir(blocks_dir) if f.endswith('.txt')]
readers = [open(file, 'r', encoding='utf-8') for file in block_files]
pq = []
for i, reader in enumerate(readers):
line = reader.readline()
if line:
word, postings = parse_line(line)
heapq.heappush(pq, ParsedLine(word, postings, i))
current_writer_line = []
with open(merge_index_output_path, 'w', encoding='utf-8') as writer:
while pq:
min_element = heapq.heappop(pq)
current_word = min_element.word
file_idx = min_element.file_index
current_postings = min_element.postings
line = readers[file_idx].readline()
if line:
word, postings = parse_line(line)
heapq.heappush(pq, ParsedLine(word, postings, file_idx))
if len(current_writer_line) > 0 and current_writer_line[0] != current_word:
text_to_write = ""
for elem in current_writer_line:
text_to_write += f"{elem},"
writer.write(text_to_write[:-1] + "\n")
current_writer_line = []
if len(current_writer_line) == 0:
current_writer_line.append(current_word)
for elem in current_postings:
current_writer_line.append(int(elem[0]))
current_writer_line.append(elem[1])
else:
if current_word == current_writer_line[0]:
if int(current_postings[0][0]) == current_writer_line[-2]:
current_writer_line[-1] += current_postings[0][1]
for elem in current_postings[1:]:
current_writer_line.append(int(elem[0]))
current_writer_line.append(elem[1])
else:
for elem in current_postings:
current_writer_line.append(int(elem[0]))
current_writer_line.append(elem[1])
text_to_write = ""
for elem in current_writer_line:
text_to_write += f"{elem},"
writer.write(text_to_write[:-1] + "\n")
for reader in readers:
reader.close()
print("DEBUG: Finished creation of merged index")
def create_merged_weighted_index(merged_index_path, filenames_json_path, output_path):
print("DEBUG: Started creation of merged weighted index")
with open(filenames_json_path, "r") as filenames_json:
filenames_len = len(json.load(filenames_json))
with open(merged_index_path, "r") as merged_index_file, open(output_path, "w") as output_file:
for line in merged_index_file:
parts = line.strip().split(',')
word = parts[0]
doc_freq = len(parts) // 2
idf = math.log(filenames_len / doc_freq)
new_line = [word]
for i in range(1, len(parts), 2):
file_id = parts[i]
tf = int(parts[i + 1])
tfidf = tf * idf
new_line.extend([file_id, str(tfidf)])
output_file.write(','.join(new_line) + '\n')
print("DEBUG: Finished creation of merged weighted index")
def generate_index():
output_dir = "./index_output"
blocks_dir = "./index_output/blocks"
clear_folder(output_dir)
os.makedirs(output_dir, exist_ok=True)
os.makedirs(blocks_dir, exist_ok=True)
token_stream = process_csv_file("./songs/songs_filter_16k.csv", output_dir)
spimi_invert(token_stream, blocks_dir)
create_merged_index(blocks_dir, f"{output_dir}/merged_index.txt")
create_merged_weighted_index(f"{output_dir}/merged_index.txt",
f"{output_dir}/filenames.json",
f"{output_dir}/merged_weighted_index.txt")
if __name__ == "__main__":
# clear_folder("./test-blocks")
# spimi_invert(process_csv_file("./songs_filter_100.csv",
# "./"),
# "./test-blocks")
# output_dir = "./index_output"
# blocks_dir = "./index_output/blocks"
#
# create_merged_weighted_index(f"{output_dir}/merged_index.txt",
# f"{output_dir}/filenames.json",
# f"{output_dir}/merged_weighted_index.txt")
#
# create_merged_index("./index_output/blocks", "./index_output/merged_index.txt")
generate_index()