-
Notifications
You must be signed in to change notification settings - Fork 2
/
lucene_index_surrogate.py
153 lines (114 loc) · 4.95 KB
/
lucene_index_surrogate.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
import lucene
import time
import numpy as np
from java.nio.file import Paths
from org.apache.lucene.analysis.core import WhitespaceAnalyzer
from org.apache.lucene.document import Document, Field, StringField, TextField, FieldType
from org.apache.lucene.index import IndexWriter, IndexWriterConfig, IndexOptions, DirectoryReader
from org.apache.lucene.search import IndexSearcher, BooleanQuery
from org.apache.lucene.store import SimpleFSDirectory
from org.apache.lucene.queryparser.classic import QueryParser
def surrogate_text(feature, k=196):
'''
Creates the surrogate text representation of a single feature.
Args:
- feature: a single feature with shape (N,)
- k: length of the truncated permutation; use -1 for the full lenght
'''
feature = feature.reshape(-1)
k = feature.shape[0] if k < 0 else min(k, feature.shape[0])
# find inverse permutation
inv_perm = np.argsort(np.argsort(feature)[::-1]) + 1
# truncate and complement the permutation
# trunc_inv_perm = (k+1) - np.minimum(inv_perm, (k+1)), or better:
trunc_inv_perm = np.maximum((k+1) - inv_perm, 0)
# now each dimension indicates how many times you have to repeat its term in
# the surrogate text representation
surrogate = []
for term, freq in enumerate(trunc_inv_perm):
if freq:
surrogate.append('{}^{}'.format(str(term), freq))
return ' '.join(surrogate)
class LuceneIndex (object):
def __init__(self, lucene_vm, index_dir):
lucene_vm.attachCurrentThread()
BooleanQuery.setMaxClauseCount(2**16) # to avoid 'too many boolean clauses'
self.index_dir = index_dir
#self.fields = dict()
#self.fields['doc_id'] = FieldType(StringField)
#self.fields['doc_id'].setStored(True)
#self.fields['doc_id'].setIndexOptions(IndexOptions.DOCS)
#self.fields['content'] = FieldType(TextField)
# self.fields['content'].setStored(True)
#self.fields['content'].setIndexOptions(IndexOptions.DOCS_AND_FREQS)
# self.fields['content'].setStoreTermVectors(True)
self.directory = SimpleFSDirectory(Paths.get(self.index_dir))
self.analyzer = WhitespaceAnalyzer()
self.parser = QueryParser('content', self.analyzer)
# self.parser.setDefaultOperator(QueryParser.Operator.AND)
self.writer = None
self.reader = None
self.searcher = None
def _init_writer(self):
# Needed?
# self.analyzer = LimitTokenCountAnalyzer(self.analyzer, 10000)
config = IndexWriterConfig(self.analyzer)
self.writer = IndexWriter(self.directory, config)
def _init_searcher(self):
self.reader = DirectoryReader.open(self.directory)
self.searcher = IndexSearcher(self.reader)
def add(self, doc_id, surrogate):
if self.writer is None:
self._init_writer()
# surrogate = surrogate_text(feature)
doc = Document()
#doc.add(Field('doc_id', doc_id, self.fields['doc_id']))
#doc.add(Field('content', surrogate, self.fields['content']))
doc.add(Field('doc_id', doc_id, StringField.TYPE_STORED))
doc.add(Field('content', surrogate, TextField.TYPE_NOT_STORED))
self.writer.addDocument(doc)
# self.writer.commit()
def query(self, surrogate, limit=1000):
if self.searcher is None:
self._init_searcher()
# surrogate = surrogate_text(feature)
query = self.parser.parse(surrogate)
# with open('/tmp/query.txt', 'wb') as f:
# f.write(surrogate)
# s = time.time()
scoreDocs = self.searcher.search(query, limit).scoreDocs
# print('Search Time: {}s'.format(time.time() - s))
return ((self.searcher.doc(result.doc)['doc_id'], result.score) for result in scoreDocs)
def count(self):
if self.searcher is None:
try:
self._init_searcher()
except:
return -1
return self.reader.numDocs()
def close(self):
if self.writer is not None:
self.writer.commit()
self.writer.close()
self.writer = None
if self.searcher is not None:
self.searcher = None
self.reader.close()
self.reader = None
'''
Magic methods to manage this object in a 'with' context.
This assures that close() is called.
'''
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
if __name__ == '__main__':
lucene_vm = lucene.initVM(vmargs=['-Djava.awt.headless=true'])
with LuceneIndex(lucene_vm, 'debug_index') as idx:
a = np.arange(5)
b = np.random.rand(5)
c = a * 10
#idx.add("1", a)
#idx.add("2", b)
idx.query(c)