-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
LuceneSentenceIndex.java
376 lines (321 loc) · 11.7 KB
/
LuceneSentenceIndex.java
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package edu.stanford.nlp.patterns;
import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.patterns.surface.Token;
import edu.stanford.nlp.pipeline.CoreNLPProtos;
import edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer;
import edu.stanford.nlp.util.CollectionValuedMap;
import edu.stanford.nlp.util.LuceneFieldType;
import edu.stanford.nlp.util.ArgumentParser;
import edu.stanford.nlp.util.ArgumentParser.Option;
import edu.stanford.nlp.util.logging.Redwood;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import java.io.*;
import java.util.*;
import java.util.function.Function;
/**
* To create a lucene inverted index from tokens to sentence ids.
* (Right now it is not storing all core tokens although some functions might suggest that.)
*
* @author Sonal Gupta on 10/14/14.
*/
public class LuceneSentenceIndex<E extends Pattern> extends SentenceIndex<E> {
@Option(name="saveTokens")
boolean saveTokens = false;
IndexWriter indexWriter;
File indexDir = null;
Directory dir;
Analyzer analyzer = new KeywordAnalyzer();
// Analyzer analyzer = new Analyzer() {
// @Override
// protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
// Tokenizer source = new KeywordTokenizer(reader);
// TokenStream result = new StopWordsFilter(source);
// return new TokenStreamComponents(source, result);
// }
// };
// public final class StopWordsFilter extends FilteringTokenFilter {
// /**
// * Build a filter that removes words that are too long or too
// * short from the text.
// */
// public StopWordsFilter(TokenStream in) {
// super(true, in);
// }
//
// @Override
// public boolean accept() throws IOException {
// return !stopWords.contains(input.toString().toLowerCase());
// }
// }
//StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_42);
IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
DirectoryReader reader = null;
IndexSearcher searcher;
ProtobufAnnotationSerializer p = new ProtobufAnnotationSerializer();
//The fields in index are: tokens, sentence id, List<CoreLabel> annotation of the sentence (optional; not used when sentences are in memory)
public LuceneSentenceIndex(Properties props, Set<String> stopWords, String indexDirStr, Function<CoreLabel, Map<String, String>> transformer) {
super(stopWords, transformer);
ArgumentParser.fillOptions(this, props);
indexDir = new File(indexDirStr);
}
void setIndexReaderSearcher() throws IOException {
FSDirectory index = FSDirectory.open(indexDir.toPath());
if(reader == null){
reader = DirectoryReader.open(index);
searcher = new IndexSearcher(reader);
}else{
DirectoryReader newreader = DirectoryReader.openIfChanged(reader);
if(newreader != null) {
reader.close();
reader = newreader;
searcher = new IndexSearcher(reader);
}
}
}
// SentenceIndex.SentenceIteratorWithWords queryIndex(SurfacePattern pat){
//
//
// String[] n = pat.getSimplerTokensNext();
// String[] pr = pat.getSimplerTokensPrev();
// boolean rest = false;
// if(n!=null){
// for(String e: n){
// if(!specialWords.contains(e)){
// rest = true;
// break;
// }
// }
// }
// if(rest == false && pr!=null){
// for(String e: pr){
// if(!specialWords.contains(e) && !stopWords.contains(e)){
// rest = true;
// break;
// }
// }
// }
//
// }
/**
* give all sentences that have these words
* **/
Set<String> queryIndexGetSentences(CollectionValuedMap<String, String> words) throws IOException, ParseException {
setIndexReaderSearcher();
BooleanQuery.Builder builder = new BooleanQuery.Builder();
String pkey = Token.getKeyForClass(PatternsAnnotations.ProcessedTextAnnotation.class);
for(Map.Entry<String, Collection<String>> en: words.entrySet()){
boolean processedKey = en.getKey().equals(pkey);
for(String en2: en.getValue()){
if(!processedKey || !stopWords.contains(en2.toLowerCase()))
builder.add(new BooleanClause(new TermQuery(new Term(en.getKey(), en2)), BooleanClause.Occur.MUST));
}
}
BooleanQuery query = builder.build();
//query.add(new BooleanClause(new TermQuery(new Term("textannotation","sonal")), BooleanClause.Occur.MUST));
// String queryStr = "";
// for(Map.Entry<String, Collection<String>> en: words.entrySet()){
// for(String en2: en.getValue()){
// queryStr+= " " + en.getKey() + ":"+en2;
// }
// }
// QueryParser queryParser = new QueryParser(Version.LUCENE_42, "sentence", analyzer);
//
// queryParser.setDefaultOperator(QueryParser.Operator.AND);
//
// Query query = queryParser.parse(queryStr);
//Map<String, List<CoreLabel>> sents = null;
TopDocs tp = searcher.search(query, Integer.MAX_VALUE);
Set<String> sentids = new HashSet<>();
if (tp.totalHits > 0) {
for (ScoreDoc s : tp.scoreDocs) {
int docId = s.doc;
Document d = searcher.doc(docId);
// byte[] sent = d.getBinaryValue("tokens").bytes;
// if(saveTokens) {
// sents = new HashMap<String, List<CoreLabel>>();
// List<CoreLabel> tokens = readProtoBufAnnotation(sent);
// sents.put(d.get("sentid"), tokens);
// } else{
sentids.add(d.get("sentid"));
//}
}
} else {
throw new RuntimeException("how come no documents for " + words + ". Query formed is " + query);
//System.out.println("number of sentences for tokens " + words + " are " + sentids);
// if(!saveTokens){
// sents = getSentences(sentids);
// }
}
return sentids;
}
@Override
public void add(Map<String, DataInstance> sentences, boolean addProcessedText) {
try {
this.setIndexWriter();
for(Map.Entry<String, DataInstance> en: sentences.entrySet()){
//String sentence = StringUtils.joinWords(en.getValue(), " ");
add(en.getValue().getTokens(), en.getKey(), addProcessedText);
}
indexWriter.commit();
closeIndexWriter();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public Map<E, Set<String>> queryIndex(Collection<E> patterns) {
try{
Map<E, Set<String>> sents = new HashMap<>();
for(E p : patterns){
Set<String> sentids = queryIndexGetSentences(p.getRelevantWords());
sents.put(p, sentids);
}
return sents;
}
catch (ParseException | IOException e) {
throw new RuntimeException(e);
}
}
public void listAllDocuments() throws IOException {
setIndexReaderSearcher();
for(int i = 0; i < reader.numDocs(); i++){
Document d = searcher.doc(i);
// byte[] sent = d.getBinaryValue("tokens").bytes;
// List<CoreLabel> tokens = readProtoBufAnnotation(sent);
System.out.println(d.get("sentid"));
}
}
private List<CoreLabel> readProtoBufAnnotation(byte[] sent) throws IOException {
ProtobufAnnotationSerializer p = new ProtobufAnnotationSerializer();
List<CoreLabel> toks = new ArrayList<>();
ByteArrayInputStream is = new ByteArrayInputStream(sent);
CoreNLPProtos.Token d;
do{
d = CoreNLPProtos.Token.parseDelimitedFrom(is);
if(d != null)
toks.add(p.fromProto(d));
} while(d != null);
return toks;
}
byte[] getProtoBufAnnotation(List<CoreLabel> tokens) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
for(CoreLabel token: tokens){
CoreNLPProtos.Token ptoken = p.toProto(token);
ptoken.writeDelimitedTo(os);
}
os.flush();
return os.toByteArray();
}
@Override
protected void add(List<CoreLabel> tokens, String sentid, boolean addProcessedText){
try{
setIndexWriter();
Document doc = new Document();
for(CoreLabel l : tokens) {
for (Map.Entry<String, String> en: transformCoreLabeltoString.apply(l).entrySet()) {
doc.add(new StringField(en.getKey(), en.getValue(), Field.Store.YES));//, ANALYZED));
}
if(addProcessedText){
String ptxt = l.get(PatternsAnnotations.ProcessedTextAnnotation.class);
if(!stopWords.contains(ptxt.toLowerCase()))
doc.add(new StringField(Token.getKeyForClass(PatternsAnnotations.ProcessedTextAnnotation.class), ptxt, Field.Store.YES));//, ANALYZED));
}
}
doc.add(new StringField("sentid", sentid, Field.Store.YES));
if(tokens!= null && saveTokens)
doc.add(new Field("tokens", getProtoBufAnnotation(tokens), LuceneFieldType.NOT_INDEXED));
indexWriter.addDocument(doc);
}catch(IOException e){
throw new RuntimeException(e);
}
}
@Override
public void finishUpdating() {
if(indexWriter != null){
try {
indexWriter.commit();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
closeIndexWriter();
}
@Override
public void update(List<CoreLabel> tokens, String sentid) {
try {
setIndexWriter();
indexWriter.deleteDocuments(new TermQuery(new Term("sentid",sentid)));
add(tokens, sentid, true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
void setIndexWriter() {try{
if(indexWriter == null){
dir = FSDirectory.open(indexDir.toPath());
Redwood.log(Redwood.DBG, "Updating lucene index at " + indexDir);
indexWriter = new IndexWriter(dir, iwc);
}}catch(IOException e){
throw new RuntimeException(e);
}
}
void closeIndexWriter(){
try {
if(indexWriter != null)
indexWriter.close();
indexWriter = null;
if(dir != null)
dir.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void saveIndex(String dir){
if(!indexDir.toString().equals(dir)){
try {
IOUtils.cp(indexDir, new File(dir), true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static LuceneSentenceIndex createIndex(Map<String, List<CoreLabel>> sentences, Properties props, Set<String> stopWords, String indexDiskDir, Function<CoreLabel, Map<String, String>> transformer) {
try{
LuceneSentenceIndex sentindex = new LuceneSentenceIndex(props, stopWords, indexDiskDir, transformer);
System.out.println("Creating lucene index at " + indexDiskDir);
IOUtils.deleteDirRecursively(sentindex.indexDir);
if(sentences!= null){
sentindex.setIndexWriter();
sentindex.add(sentences, true);
sentindex.closeIndexWriter();
sentindex.setIndexReaderSearcher();
System.out.println("Number of documents added are " + sentindex.reader.numDocs());
sentindex.numAllSentences += sentindex.reader.numDocs();
}
return sentindex;
}catch(IOException e){
throw new RuntimeException(e);
}
}
public static LuceneSentenceIndex loadIndex(Properties props, Set<String> stopwords, String dir, Function<CoreLabel, Map<String, String>> transformSentenceToString) {
try {
LuceneSentenceIndex sentindex = new LuceneSentenceIndex(props, stopwords, dir, transformSentenceToString);
sentindex.setIndexReaderSearcher();
System.out.println("Number of documents read from the index " + dir + " are " + sentindex.reader.numDocs());
sentindex.numAllSentences += sentindex.reader.numDocs();
return sentindex;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}