-
Notifications
You must be signed in to change notification settings - Fork 0
/
systems.py
142 lines (115 loc) · 4.35 KB
/
systems.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
import os
from pyserini.index.__main__ import JIndexCollection
from pyserini.search import SimpleSearcher
import jsonlines
from multiprocessing import Pool
import shutil
CHUNKSIZE = 100000000
ARGS = ["-collection", "JsonCollection",
"-generator", "DefaultLuceneDocumentGenerator",
"-threads", "6",
"-input", "./index/convert/",
"-index", "./index/",
"-storePositions",
"-storeDocvectors",
"-storeRaw"]
class Ranker(object):
def __init__(self):
self.idx = None
self.searcher = None
def _make_chuncks(self, dir):
for file in os.listdir(dir):
if file.endswith(".jsonl"):
with open(os.path.join(dir, file), 'r') as f:
cnt = 0
while True:
lines = f.readlines(CHUNKSIZE)
if not lines:
break
with open(''.join(['./index/chunks/', file[:-6], '_', str(cnt), '.jsonl']), 'w') as _chunk_out:
for line in lines:
_chunk_out.write(line)
cnt += 1
def _convert_chunks(self, file):
with jsonlines.open(os.path.join('./index/convert/', file), mode='w') as writer:
with jsonlines.open(os.path.join("./index/chunks/", file)) as reader:
for obj in reader:
title = obj.get('title') or ''
title = title[0] if type(title) is list else title
abstract = obj.get('abstract') or ''
abstract = abstract[0] if type(abstract) is list else abstract
author = obj.get('person') or ''
author = ' '.join(author) if type(author) is list else author
topic = obj.get('topic') or ''
topic = ' '.join(topic) if type(topic) is list else topic
try:
doc = {'id': obj.get('id'),
'contents': ' '.join([title,
abstract,
author,
topic])}
writer.write(doc)
except Exception as e:
print(e)
def _mkdir(self, dir):
try:
os.mkdir(dir)
except OSError as error:
print(error)
def index(self):
self._mkdir('./index/')
self._mkdir('./index/convert/')
self._mkdir('./index/chunks/')
self._make_chuncks("./data/gesis-search/documents/")
for i in os.listdir("./index/chunks/"):
self._convert_chunks(i)
# p = Pool()
# p.map(self._convert_chunks, os.listdir("./index/chunks/"))
# p.close()
shutil.rmtree('./index/chunks')
JIndexCollection.main(ARGS)
self.searcher = SimpleSearcher('./index/')
shutil.rmtree('./index/convert/')
def rank_publications(self, query, page, rpp):
hits = []
itemlist = []
if query is not None:
if self.idx is None:
try:
self.searcher = SimpleSearcher('./index/')
self.searcher.set_rm3(10, 10, 0.5)
except Exception as e:
print('No index available: ', e)
if self.searcher is not None:
hits = self.searcher.search(query, k=100)
itemlist = [hit.docid for hit in hits[page*rpp:(page+1)*rpp]]
return {
'page': page,
'rpp': rpp,
'query': query,
'itemlist': itemlist,
'num_found': len(hits)
}
class Recommender(object):
def __init__(self):
self.idx = None
def index(self):
pass
def recommend_datasets(self, item_id, page, rpp):
itemlist = []
return {
'page': page,
'rpp': rpp,
'item_id': item_id,
'itemlist': itemlist,
'num_found': len(itemlist)
}
def recommend_publications(self, item_id, page, rpp):
itemlist = []
return {
'page': page,
'rpp': rpp,
'item_id': item_id,
'itemlist': itemlist,
'num_found': len(itemlist)
}