-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreList.java
More file actions
119 lines (104 loc) · 2.81 KB
/
Copy pathScoreList.java
File metadata and controls
119 lines (104 loc) · 2.81 KB
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
/**
* Copyright (c) 2017, Carnegie Mellon University. All Rights Reserved.
*/
import java.io.*;
import java.util.*;
/**
* This class implements the document score list data structure
* and provides methods for accessing and manipulating them.
*/
public class ScoreList {
// A utility class to create a <internalDocid, externalDocid, score>
// object.
private class ScoreListEntry {
private int docid;
private String externalId;
private double score;
private ScoreListEntry(int internalDocid, double score) {
this.docid = internalDocid;
this.score = score;
try {
this.externalId = Idx.getExternalDocid (this.docid);
}
catch (IOException ex){
ex.printStackTrace();
}
}
}
/**
* A list of document ids and scores.
*/
private List<ScoreListEntry> scores = new ArrayList<ScoreListEntry>();
/**
* Append a document score to a score list.
* @param docid An internal document id.
* @param score The document's score.
*/
public void add(int docid, double score) {
scores.add(new ScoreListEntry(docid, score));
}
/**
* Get the internal docid of the n'th entry.
* @param n The index of the requested document.
* @return The internal document id.
*/
public int getDocid(int n) {
return this.scores.get(n).docid;
}
/**
* Get the score of the n'th entry.
* @param n The index of the requested document score.
* @return The document's score.
*/
public double getDocidScore(int n) {
return this.scores.get(n).score;
}
/**
* Set the score of the n'th entry.
* @param n The index of the score to change.
* @param score The new score.
*/
public void setDocidScore(int n, double score) {
this.scores.get(n).score = score;
}
/**
* Get the size of the score list.
* @return The size of the posting list.
*/
public int size() {
return this.scores.size();
}
/*
* Compare two ScoreListEntry objects. Sort by score, then
* internal docid.
*/
public class ScoreListComparator implements Comparator<ScoreListEntry> {
@Override
public int compare(ScoreListEntry s1, ScoreListEntry s2) {
if (s1.score > s2.score)
return -1;
else
if (s1.score < s2.score)
return 1;
else
return (s1.externalId.compareTo(s2.externalId));
}
}
/**
* Sort the list by score and external document id.
*/
public void sort () {
Collections.sort(this.scores, new ScoreListComparator());
}
/**
* Reduce the score list to the first num results to save on RAM.
*
* @param num Number of results to keep.
*/
public void truncate(int num) {
List<ScoreListEntry> truncated = new ArrayList<ScoreListEntry>(this.scores.subList(0,
Math.min(num, scores.size())));
this.scores.clear();
this.scores = truncated;
}
}