-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQrySopIndriWsum.java
98 lines (84 loc) · 3.21 KB
/
QrySopIndriWsum.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
/**
* Copyright (c) 2016, Carnegie Mellon University. All Rights Reserved.
*/
import java.io.*;
import java.util.*;
/**
* The AND operator for all retrieval models.
*/
public class QrySopIndriWsum extends QrySop
implements QryIFWeightable, QryIFIndriable {
private ArrayList<Double> weights;
public QrySopIndriWsum() {
this.weights = new ArrayList<Double>();
}
public void appendWeight(double weight) {
this.weights.add(weight);
}
/**
* Indicates whether the query has a match.
* @param r The retrieval model that determines what is a match
* @return True if the query matches, otherwise false.
*/
public boolean docIteratorHasMatch (RetrievalModel r) {
if (r instanceof RetrievalModelIndri)
return this.docIteratorHasMatchMin(r);
else
throw new IllegalArgumentException
(r.getClass().getName() + " doesn't support the WSUM operator.");
}
/**
* Get a score for the document that docIteratorHasMatch matched.
* @param r The retrieval model that determines how scores are calculated.
* @return The document score.
* @throws IOException Error accessing the Lucene index
*/
public double getScore (RetrievalModel r) throws IOException {
if (r instanceof RetrievalModelIndri) {
return this.getScoreIndri(r);
} else {
throw new IllegalArgumentException
(r.getClass().getName() + " doesn't support the WSUM operator.");
}
}
/**
* getScore for the Indri retrieval model.
* @param r The retrieval model that determines how scores are calculated.
* @return The document score.
* @throws IOException Error accessing the Lucene index
*/
private double getScoreIndri(RetrievalModel r) throws IOException {
if (! this.docIteratorHasMatchCache()) {
// this should never be called...
return 0.0;
} else {
RetrievalModelIndri indri = (RetrievalModelIndri) r;
ArrayList<Double> scores = new ArrayList<Double>();
int docid = this.docIteratorGetMatch();
for (Qry q_i: this.args) {
if (q_i.docIteratorHasMatchCache() &&
q_i.docIteratorGetMatch() == docid) {
scores.add(((QrySop) q_i).getScore(r));
} else {
scores.add(((QryIFIndriable) q_i).getDefaultIndriScore(r, docid));
}
}
return indri.wsumCombiner(scores, this.weights);
}
}
/**
* getDefaultScore for the Indri retrieval model.
* @param r The retrieval model that determines how scores are calculated.
* @return The document score.
* @throws IOException Error accessing the Lucene index
*/
public double getDefaultIndriScore(RetrievalModel r, int docid)
throws IOException {
RetrievalModelIndri indri = (RetrievalModelIndri) r;
ArrayList<Double> scores = new ArrayList<Double>();
for (Qry q_i: this.args) {
scores.add(((QryIFIndriable) q_i).getDefaultIndriScore(r, docid));
}
return indri.wsumCombiner(scores, this.weights);
}
}