forked from yuzhujoe/LuceneSE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQryIopNear.java
271 lines (195 loc) · 6.12 KB
/
QryIopNear.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
/**
* Copyright (c) 2015, Carnegie Mellon University. All Rights Reserved.
*/
import java.io.*;
import java.util.*;
/**
* The SYN operator for all retrieval models.
*/
public class QryIopNear extends QryIop {
/**
* Evaluate the query operator; the result is an internal inverted
* list that may be accessed via the internal iterators.
* @throws IOException Error accessing the Lucene index.
*/
private int distance;
private int numOfMatch;
public QryIopNear(int k){
this.distance = k;
}
protected void evaluate () {
// Create an empty inverted list. If there are no query arguments,
// that's the final result.
this.invertedList = new InvList (this.getField());
if (args.size () == 0) {
return;
}
List<Integer> positions = new ArrayList<Integer>();
// Each pass of the loop adds 1 document to result inverted list
// until all of the argument inverted lists are depleted.
int minDocid = Qry.INVALID_DOCID;
for (Qry q_i: this.args) {
//QryIop null as prapmeter is ok
if (q_i.docIteratorHasMatch (null)) {
int q_iDocid = q_i.docIteratorGetMatch ();
if ((minDocid > q_iDocid) ||
(minDocid == Qry.INVALID_DOCID)) {
minDocid = q_iDocid;
}
}
}
if (minDocid == Qry.INVALID_DOCID)
return; // All docids have been processed. Done.
while (true) {
// Find the minimum next document id. If there is none, we're done.
ArrayList<Integer> l = new ArrayList<Integer>();
int[] locationPtr = new int[this.args.size()];
if(this.docIteratorHasMatchAll()){
minDocid = this.args.get(0).docIteratorGetMatch();
// System.out.println("minDocid: "+minDocid);
}else{
return;
}
//initialize all location ptr
for(int i=0; i < this.args.size(); i++){
Qry q_i = this.args.get(i);
boolean check = ((QryIop) q_i).locIteratorHasMatch();
if(!check)
return;
locationPtr[i] = ((QryIop) q_i).locIteratorGetMatch();
}
int numOfMatch = 0;
int ptr = 0;
Qry q_0 = this.args.get(0);
while(((QryIop) q_0).locIteratorHasMatch()){
for(int i = 1; i < this.args.size(); i++) {
QryIop q_a = (QryIop) this.args.get(i-1);
QryIop q_b = (QryIop) this.args.get(i);
if (q_a.locIteratorHasMatch()) {
int loc_a = q_a.locIteratorGetMatch();
q_b.locIteratorAdvancePast(loc_a);
if (! q_b.locIteratorHasMatch()) {
break;
}
int loc_b = q_b.locIteratorGetMatch();
int dist = loc_b - loc_a;
if (dist > distance) {
break;
} else if (i+1 == this.args.size()) { //last argument
positions.add(loc_b);
for(int w = 1; w < this.args.size();w++){
((QryIop)(this.args.get(w))).locIteratorAdvance();
}
//q_b.locIteratorAdvance(); //one position cannot match twice
//advanceAllPtr();
}
}
}
((QryIop) q_0).locIteratorAdvance();
}
if(!positions.isEmpty())
this.invertedList.appendPosting(minDocid, positions);
positions.clear();
this.args.get(0).docIteratorAdvancePast(minDocid);
}
}
//true if location
private boolean docIdAllNearhasMatch() {
// TODO Auto-generated method stub
for(int i=0;i< this.args.size();i++){
if (!((QryIop)this.args.get(i)).locIteratorHasMatch()){
return false;
}
}
return true;
}
//advance all ptrs
private void advanceAllPtr() {
// TODO Auto-generated method stub
for(int i=0;i< this.args.size();i++){
((QryIop)this.args.get(i)).locIteratorAdvance();
}
}
//advance min pters
private int advanceMinPtr(int[] locationPtr) {
// TODO Auto-generated method stub
int minIdx = 0;
for(int i=0;i< locationPtr.length;i++){
if(locationPtr[i] < locationPtr[minIdx]){
minIdx = i;
}
}
return minIdx;
}
//true if docid at index i has match
private boolean docIdNearhasMatch(int i) {
return ((QryIop)this.args.get(i)).locIteratorHasMatch();
}
private void advancePtr(int ptr) {
((QryIop)this.args.get(ptr)).locIteratorAdvance();
if(ptr == 0){
for (int i=1; i< this.args.size();i++){
((QryIop)this.args.get(i)).locIteratorAdvancePast(0);
}
}
return;
}
private int abs(int a){
return (a > 0)?a:(-a);
}
// to chek the docid
private boolean checkSameDocid(int[] docIdForNear){
int anchor = docIdForNear[0];
for(int id : docIdForNear){
if(id != anchor)
return false;
}
return true;
}
// true if location satisfy condition
private boolean checkValidLocation(int[] locationPtr){
int firstLoc;
int secondLoc;
int diff;
int len = locationPtr.length-1;
for (int i=0;i< len; i++){
firstLoc = locationPtr[i];
secondLoc = locationPtr[i+1];
diff = secondLoc - firstLoc;
if( diff > distance || diff <= 0){
return false;
}
}
return true;
}
private boolean docIteratorHasMatchAll(){
boolean matchFound = false;
// Keep trying until a match is found or no match is possible.
while (! matchFound) {
// Get the docid of the first query argument.
if (! this.args.get (0).docIteratorHasMatch (null)) {
return false;
}
int docid_0 = this.args.get (0).docIteratorGetMatch ();
// Other query arguments must match the docid of the first query
// argument.
matchFound = true;
for (int i=1; i<this.args.size(); i++) {
this.args.get(i).docIteratorAdvanceTo (docid_0);
if (! this.args.get(i).docIteratorHasMatch (null)) { // If any argument is exhausted
return false; // there are no more matches.
}
int docid_i = this.args.get(i).docIteratorGetMatch ();
if (docid_0 != docid_i) { // docid_0 can't match. Try again.
this.args.get(0).docIteratorAdvancePast (docid_0);
matchFound = false;
break;
}
}
if (matchFound) {
this.args.get(0).docIteratorSetMatchCache (docid_0);
}
}
return true;
}
}