-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
70 lines (64 loc) · 1.71 KB
/
utils.py
File metadata and controls
70 lines (64 loc) · 1.71 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
import pickle
from indexer import Indexer
import os
def save_obj(obj, name):
"""
This function save an object as a pickle.
:param obj: object to save
:param name: name of the pickle file.
:return: -
"""
with open(name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name):
"""
This function will load a pickle file
:param name: name of the pickle file
:return: loaded pickle file
"""
with open(name + '.pkl', 'rb') as f:
return pickle.load(f)
def load_inverted_index():
return None
def load_inverted_index(path):
file = open(path + "\\inverted_index_dic.txt", "r")
inverted_index = {}
line = file.readline()
while line:
splited_line = line.split(":")
term = splited_line[0]
inverted_index[term] = {}
values = splited_line[1].split(" ")
inverted_index[term]['tf'] = values[0]
line = file.readline()
file.close()
return inverted_index
def create_folders(path):
if not os.path.exists(path):
os.makedirs(path)
return path
else:
return path
def read_text_queries(path):
path = path
file = open(path, encoding="utf8")
list=[]
line = file.readline()
while line:
list.append(line)
line = file.readline()
return list
# file_list = []
# file_in = open(path, encoding="utf8")
#
# while True:
# try:
# line = file_in.readline()
# if line != '\n':
# line = line.split(".", 1)[1]
# line = line.split("\n", 1)[0]
# file_list.append(line)
# except:
# break
# file_in.close()
# return file_list