-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuceneInverted.java
249 lines (233 loc) · 6.62 KB
/
LuceneInverted.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
package com.ir.pubmed;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Arrays;
import java.util.Collections;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.index.*;
import org.apache.lucene.search.DocIdSetIterator;
public class LuceneInverted {
static String dir = ""; // Enter the respective path
static String index = "index";
static int comparisons_counter=0;
public static LinkedList<Integer> intersectTaatAnd(LinkedList<Integer> a, LinkedList<Integer> b) {
LinkedList<Integer> answer = new LinkedList<>();
int x=0,y=0;
while(x != a.size() && y != b.size()) {
int docA=a.get(x);
int docB=b.get(y);
if(docA == docB) {
comparisons_counter++;
answer.add(docA);
x++;
y++;
}
else if(a.get(x) < b.get(y)) {
comparisons_counter++;
x++;
}
else {
comparisons_counter++;
y++;
}
}
return answer;
}
public static LinkedList<Integer> intersectTaatOr(LinkedList<Integer> a, LinkedList<Integer> b) {
LinkedList<Integer> answer = new LinkedList<>();
int x=0,y=0;
while(x != a.size() && y != b.size()) {
int docA=a.get(x);
int docB=b.get(y);
if(docA == docB) {
comparisons_counter++;
if(!answer.contains(docA))
answer.add(docA);
x++;
y++;
}
else if(docA < docB) {
comparisons_counter++;
if(!answer.contains(docA))
answer.add(docA);
x++;
}
else {
comparisons_counter++;
if(!answer.contains(docB))
answer.add(docB);
y++;
}
}
if(x!=a.size() || y!=b.size()) {
if(x!=a.size()) {
while(x!=a.size()) {
int docA = a.get(x);
if(!answer.contains(docA)) {
comparisons_counter++;
answer.add(docA);
}
x++;
}
}
else if(y!=b.size()) {
while(y!=b.size()) {
int docB = b.get(y);
if(!answer.contains(docB)) {
comparisons_counter++;
answer.add(docB);
}
y++;
}
}
}
return answer;
}
public static void main(String args[]) throws IOException {
IndexReader indexReader = DirectoryReader.open(FSDirectory.open(FileSystems.getDefault().getPath(dir, index)));
Fields fields = MultiFields.getFields(indexReader);
System.out.println(indexReader.numDocs());
Map<String, LinkedList<Integer>> dictionary = new HashMap<>();
Iterator<String> fieldIterator = fields.iterator();
String field= fieldIterator.next();
while(fieldIterator.hasNext()) {
field = fieldIterator.next();
Terms terms = fields.terms(field);
TermsEnum termsIterator = terms.iterator();
PostingsEnum posting = null;
while(termsIterator.next() != null){
BytesRef termBytes = termsIterator.term();
String termString = termBytes.utf8ToString();
LinkedList<Integer> termPosting = new LinkedList<>();
posting = termsIterator.postings(posting);
int docId;
do{
docId = posting.nextDoc();
if(docId == 2147483647)
continue;
termPosting.add(docId);
} while(docId != DocIdSetIterator.NO_MORE_DOCS);
Collections.sort(termPosting);
dictionary.put(termString, termPosting);
}
}
TreeMap<String, LinkedList<Integer>> sortedDictionary = new TreeMap<>();
sortedDictionary.putAll(dictionary);
System.out.println(dictionary.size());
FileWriter sample = new FileWriter(new File("index.txt"));
BufferedWriter bw = new BufferedWriter(sample);
for(String name: sortedDictionary.keySet()) {
String key = name;
String value = sortedDictionary.get(name).toString();
bw.write(key+ " " +value+"\n");
}
bw.close();
// Implementation of Daat and Taat
BufferedReader ip = null;
FileWriter fw = null;
BufferedWriter op = null;
try {
ip = new BufferedReader(new FileReader(new File("input.txt")));
fw = new FileWriter(new File("output.txt"));
op = new BufferedWriter(fw);
System.out.println("Success");
for(String x = ip.readLine(); x!=null; x=ip.readLine()) {
byte[] bytesArray = x.getBytes();
String y = new String(bytesArray,"UTF-8");
String[] printTerms = y.split(" ");
String[] termIp = y.split(" ");
//GetPostings
for(String term: termIp) {
op.write("GetPostings\n");
op.write(term+"\nPostings List: ");
LinkedList<Integer> getPosting = sortedDictionary.get(term);
for(int i=0;i<getPosting.size();i++)
op.write(getPosting.get(i)+" ");
op.write("\n");
}
//Sort by increasing term frequency
for(int i=0;i<termIp.length-1;i++) {
String temp;
for(int j=0;j<termIp.length-i-1;j++) {
LinkedList<Integer> getPostingI = sortedDictionary.get(termIp[j]);
LinkedList<Integer> getPostingJ = sortedDictionary.get(termIp[j+1]);
if(getPostingI.size() < getPostingJ.size()) {
temp = termIp[j];
termIp[j] = termIp[i];
termIp[i] = temp;
}
}
}
//TaatAnd
op.write("TaatAnd\n");
for(String term: printTerms) {
op.write(term+" ");
}
op.write("\n");
LinkedList<Integer> a = sortedDictionary.get(termIp[0]);
for(int i = 1; i<termIp.length;i++) {
LinkedList<Integer> b = sortedDictionary.get(termIp[i]);
a = intersectTaatAnd(a,b);
}
op.write("Results: ");
if(a.size()==0)
op.write("empty\n");
else {
for(int j=0;j<a.size();j++) {
op.write(a.get(j)+" ");
}
op.write("\n");
}
op.write("Number of documents in results: "+a.size()+"\n");
op.write("Number of comparisons: "+comparisons_counter+"\n");
comparisons_counter=0;
//TaatOr
a.clear();
op.write("TaatOr\n");
for(String term: printTerms) {
op.write(term+" ");
}
op.write("\n");
LinkedList<Integer> answer = new LinkedList<>();
a = sortedDictionary.get(termIp[0]);
for(int i = 1; i<termIp.length;i++) {
LinkedList<Integer> b = sortedDictionary.get(termIp[i]);
answer.addAll(intersectTaatOr(a,b));
}
op.write("Results: ");
if(answer.size()==0)
op.write("empty\n");
else {
for(int j=0;j<answer.size();j++) {
op.write(answer.get(j)+" ");
}
op.write("\n");
}
op.write("Number of documents in results: "+answer.size()+"\n");
op.write("Number of comparisons: "+comparisons_counter+"\n");
comparisons_counter=0;
//DaatAnd
a.clear();
}
op.flush();
}catch (IOException e){
System.out.println("Incorrect input file");
}finally {
try {
ip.close();
fw.close();
op.close();
}catch(IOException e) {
System.out.println("Failure.");
}
}
}
}