-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvertedIndexer.java
353 lines (283 loc) · 12 KB
/
InvertedIndexer.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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class InvertedIndexer {
//main dictionary. will store term(as key) and a value which will is a map whose
//key:document ID and value:list of postings of the term in that documentID
static TreeMap<String, TreeMap<Integer, ArrayList<Integer>>> dictionary = new TreeMap<>();
//list containing all document IDs
static HashMap<Integer,Integer> docList = new HashMap<>();
//average length of a document in the corpus
static double avgDocLength = 0;
static HashMap<String, HashMap<Integer, Integer>> term_freq = new HashMap<>();
//cache variable for implementing nextDoc and prevDoc using galloping search
static HashMap<String,Integer> nextCache = new HashMap<>();
static HashMap<String,Integer> prevCache = new HashMap<>();
public static void main(String args[]) throws Exception {
corpusParser(args[0]);
System.out.println(dictionary);
System.out.println("DOCLIST");
System.out.println(docList);
System.out.println("AVG DOC LENGTH");
System.out.println(avgDocLength);
freq_term_doc();
System.out.println("\n \n Printing Term Frequency");
System.out.println(term_freq);
System.out.println("should->"+term_freq.get("should"));
}
public static void corpusParser(String fileName) throws Exception {
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(file));
//variable to store docID, which will later be used as total number of docs in VSM
int docID=1;
//position of term in current document
int termPos=1;
//read the file line by line
String st;
docList.put(1,0);
while((st = br.readLine()) != null) {
//if line is just '\n' means we have encountered '\n\n'
//so increment docID and reset term position
if(st.isEmpty()) {
docList.put(docID,termPos-1);
docID++;
termPos=1;
}
else {
for(String terms : st.split("\\s+")) {
//list of positions in current doc for current term
ArrayList<Integer> posting = new ArrayList<>();
//map of current docID to posting positions for current term
TreeMap<Integer , ArrayList<Integer>> postingList = new TreeMap<>();
//can also use terms = terms.replaceAll("\\p{Punct}", "").toLowerCase(); incase we want to get rid of punctuations as well
//but that wasn't asked in the question
terms = terms.replaceAll("\\p{Punct}", "").toLowerCase();
//if term is not in dictionary
if(!dictionary.containsKey(terms)) {
posting.add(termPos);
postingList.put(docID, posting);
dictionary.put(terms, postingList);
}
//if term is already in dictionary
else {
postingList = dictionary.get(terms);
//if the term comes first time in current document
if(postingList.containsKey(docID)) {
posting = postingList.get(docID);
posting.add(termPos);
dictionary.put(terms, postingList);
}
//if the term is already encountered in current document
else {
posting.add(termPos);
postingList.put(docID, posting);
dictionary.put(terms, postingList);
}
}
termPos++;
}
}
}
br.close();
docList.put(docID,termPos-1);
double totalDocLength = 0;
for(int docLength : docList.values()){
totalDocLength += docLength;
}
avgDocLength = totalDocLength/docList.size();
}
public static void freq_term_doc() {
for(Map.Entry<String, TreeMap<Integer,ArrayList<Integer>>> entry : dictionary.entrySet()) {
TreeMap<Integer, ArrayList<Integer>> dict = new TreeMap<>(entry.getValue());
HashMap<Integer, Integer> tm = new HashMap<>();
for(Map.Entry<Integer, ArrayList<Integer>> dictEntry : dict.entrySet()) {
tm.put(dictEntry.getKey(), dictEntry.getValue().size()); //doc number, freq
}
term_freq.put(entry.getKey(), tm);
}
}
/** FIRSTDOC:
* Get the document id of the first document that contains the term
*
*/
public static int firstDoc(String term){
//List of documents in which the term occurs
ArrayList<Integer> docList = new ArrayList<>(dictionary.get(term).keySet());
//Term doesn't exist in the dictionary
if(docList.size() == 0)
return -1;
return docList.get(0);
}
/** LASTDOC
* Get the document id of the last document that contains the term
*
*/
public static int lastDoc(String term){
//List of documents in which the term occurs
ArrayList<Integer> docList = new ArrayList<>(dictionary.get(term).keySet());
//Term doesn't exist in the dictionary
if(docList.size() == 0)
return -1;
return docList.get(docList.size()-1);
}
/** GALLOPPING SEARCH: PREVDOC
* Get the document id of the last document
* before current that contains the term
* using galloping search
*
*/
public static int prevDoc(String term, int current)
{
//List of documents in which the term occurs
ArrayList<Integer> docList;
if(dictionary.containsKey(term)) {
docList = new ArrayList<>(dictionary.get(term).keySet());
}else {
return Integer.MIN_VALUE;
}
int listSize = docList.size();
int cachedId, low=0, high=0, jump;
//check for boundary conditions
// current <= first document id in the list
// then return -infinity to signify no previous doc exists
if(listSize == 0 || docList.get(0) >= current){
return Integer.MIN_VALUE;
}
//current > last document id in the list
//then return the highest(last) document id from the list
if(docList.get(listSize - 1) < current) {
prevCache.put(term,listSize-1);
return lastDoc(term);
}
//check if previous cached index for the term exists
if(prevCache.containsKey(term)) {
cachedId = prevCache.get(term);
}else {
cachedId = -1;
}
// previous cached index >= current
// then set high to start after the cached index else set it to end of list
if(cachedId>0 && docList.get(cachedId) >= current) {
high = cachedId+1;
}else {
high = listSize-1 ;
}
//Initialize jump to one at the beginning of galloping search
jump = 1;
low = high - jump;
// increase jumps exponentially till
// either current is lesser than low or the list ends
while(low >= 0 && docList.get(low)>=current) {
high = low;
jump = 2 * jump;
low = high - jump;
}
if(low<0) {
low = 0;
}
//binarySearch for current on the last 2 jumps
cachedId = binarySearch(docList,low,high,current,false);
//cache the previous document id retrieved
prevCache.put(term, cachedId);
return docList.get(cachedId);
}
/** GALLOPPING SEARCH: NEXTDOC
* Get the document id of the first document
* after current that contains the term
* using galloping search
*
*/
public static int nextDoc(String term, int current) {
ArrayList<Integer> docList;
//List of documents that contain the term
if(dictionary.containsKey(term)) {
docList = new ArrayList<>(dictionary.get(term).keySet());
}else {
return Integer.MAX_VALUE;
}
int listSize = docList.size();
int cachedId, low=0, high=0, jump;
//check border conditions
//if current >= last document id in the list
//then return +infinity to signify no next doc exists
if(listSize == 0 || docList.get(listSize-1) <= current){
return Integer.MAX_VALUE;
}
//if current < first document id in the list
//then return the first document id from the list
if(docList.get(0) > current) {
nextCache.put(term,0);
return firstDoc(term);
}
//check if the next cached index exists
if(nextCache.containsKey(term)) {
cachedId = nextCache.get(term);
}else {
cachedId = -1;
}
//next cached index <= current
//set low to start from the previous index else set it to the start of the list
if(cachedId>0 && docList.get(cachedId)<=current) {
low = cachedId-1;
}else {
low = 0;
}
//initialize jump to 1 at the beginning of the galloping search
jump = 1;
high = low + jump;
//search for current with exponential jumps
//till either current value is greater than high or high exceeds list size
while(high<listSize && docList.get(high)<=current) {
low = high;
jump = 2 * jump;
high = low + jump;
}
if(high>listSize) {
high = listSize;
}
//binary search for current in the last 2 jumps
cachedId = binarySearch(docList,low,high,current,true);
//cache the next_document id retrieved
nextCache.put(term, cachedId);
return docList.get(cachedId);
}
/**
* Function to search for an element in a list using binary search
* @param isNext Signifies if search is for previous or next element
*
*/
public static int binarySearch(ArrayList<Integer> docList, int low, int high, int current, boolean isNext) {
int mid = 0;
//Loop till high and low don't cross each other
while(high-low > 1) {
//calculate the mid index of the search window
mid = (low+high)/2;
//searching for the next element
if(isNext) {
if(docList.get(mid) <= current) {
low = mid;
}else {
high = mid;
}
}
//searching for the previous element
else {
if(docList.get(mid) < current) {
low = mid;
}else {
high = mid;
}
}
}
//return low value for previous and high value for next
if(isNext) {
return high;
}else {
return low;
}
}
}