/
NodeScore.java
250 lines (205 loc) · 8.33 KB
/
NodeScore.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
/******************************************************************************
* Copyright (c) 2004-2008 Whirlwind Match Limited. All rights reserved.
*
* This is open source software; you can use, redistribute and/or modify
* it under the terms of the Open Software Licence v 3.0 as published by the
* Open Source Initiative.
*
* You should have received a copy of the Open Software Licence along with this
* application. if not, contact the Open Source Initiative (www.opensource.org)
*****************************************************************************/
package com.wwm.attrs.internal;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.util.Assert;
import com.wwm.attrs.Score;
import com.wwm.attrs.Scorer;
import com.wwm.db.core.LogFactory;
/**
* Clonable node score tally.
* This class keeps track of which scorers generated which components of the score.
* This allows nodes in the tree to only list the split attributes, and we will calculate
* a correct score providing this object has been passed all the way down the tree.
* If the same scorer fires several times in the same direction, that is only counted as one score and the final value is used.
*
* TODO: NodeScore (misnomer) is used as it retains details of each individual score
*/
public class NodeScore extends Score implements Serializable {
private static final long serialVersionUID = 5484270146327213313L;
// NOTE: FindBugs will warn on transients below. If we can guarantee that scorer names are unique (which we
// currently don't, as ScoreConfiguration stores scorers in a list, not a map
protected transient HashMap<Scorer, Float> forwardsScores;
protected transient HashMap<Scorer, Float> reverseScores;
// these are populated on transmission to the client
protected HashMap<String, Float> labelledForwardsScores;
protected HashMap<String, Float> labelledReverseScores;
/**Create a new score, with the initial value 1.
*
*/
public NodeScore() {
super();
forwardsScores = new HashMap<Scorer, Float>();
reverseScores = new HashMap<Scorer, Float>();
}
/**Copy Constructor. This score will be a deep copy of the parameter.
* @param rhs
*/
@Deprecated // and low perf!
public NodeScore(NodeScore rhs) {
super(rhs);
// This performs a shallow map copy, which satisfies the deep-copy contract,
// as both Scorer and Float are immutable or used immutably.
forwardsScores = new HashMap<Scorer, Float>(rhs.forwardsScores);
reverseScores = new HashMap<Scorer, Float>(rhs.reverseScores);
}
/* (non-Javadoc)
* @see likemynds.db.indextree.Score#add(likemynds.db.indextree.Scorer, float, likemynds.db.indextree.Score.Direction)
*/
@Override
public void add(Scorer s, float score, Direction d) {
switch (d) {
case forwards:
//assert(forwardsScores.get(s) == null || forwardsScores.get(s) >= score); // Score should get smaller down the tree, not bigger
forwardsScores.put(s, score);
break;
case reverse:
//assert(reverseScores.get(s) == null || reverseScores.get(s) >= score); // Score should get smaller down the tree, not bigger
reverseScores.put(s, score);
break;
}
invalidate();
}
/* (non-Javadoc)
* @see likemynds.db.indextree.Score#update()
*/
@Override
protected void update() {
assert(forwardsScores != null);
assert(reverseScores != null);
float forwardsProduct = 1.0f;
float forwardsCount = 0;
float reverseProduct = 1.0f;
float reverseCount = 0;
float product = 1.0f;
float count = 0;
for (Entry<Scorer, Float> entry : forwardsScores.entrySet()) {
Scorer s = entry.getKey();
float score = entry.getValue();
float weight = s.getWeight();
float calcScore = 1 - weight + (weight * score);
forwardsProduct *= calcScore;
product *= calcScore;
// Add weight to counts if not a filter
if ( !s.isFilter() ) {
forwardsCount += weight;
count += weight;
}
}
for (Entry<Scorer, Float> entry : reverseScores.entrySet()) {
Scorer s = entry.getKey();
float score = entry.getValue();
float weight = s.getWeight();
float calcScore = 1 - weight + (weight * score);
reverseProduct *= calcScore;
product *= calcScore;
// Add weight to counts if not a filter
if ( !s.isFilter() ) {
reverseCount += weight;
count += weight;
}
}
linear = linearise(product, count);
forwardsLinear = linearise(forwardsProduct, forwardsCount);
reverseLinear = linearise(reverseProduct, reverseCount);
if (linear == 0) {
nonMatches++;
}
super.update();
}
/**
* Return the number of scores that have been recorded (forwards + reverse)
*/
public float getCount() {
return forwardsScores.size() + reverseScores.size();
}
/**
* Compares this to otherScore, and returns true if all individual scores are l
* the scores within otherScore
* This is useful for testing that the score for a each item in a leaf is always less than or equal to the parent node.
* @param otherScore
* @return
*/
public boolean allScoresLowerThan(NodeScore otherScores) {
return oneWayAllScoresLower(forwardsScores, otherScores.forwardsScores)
&& oneWayAllScoresLower(reverseScores, otherScores.reverseScores);
}
private boolean oneWayAllScoresLower(HashMap<Scorer, Float> scores, HashMap<Scorer, Float> otherScores) {
for( Entry<Scorer, Float> entry : scores.entrySet() ){
Scorer s = entry.getKey();
Float otherScore = otherScores.get(s);
if (otherScore == null){
continue;
}
if (entry.getValue().floatValue() > otherScore ){
LogFactory.getLogger(NodeScore.class).error("Attr " + entry.getKey().getScorerAttrId() + " validation failure. "
+ entry.getValue().floatValue() + " should not be greater than " + otherScore + ". Scorer = " + entry.getKey().getClass().getName() );
return false; // should be <=
}
}
return true;
}
private void writeObject(ObjectOutputStream out) throws IOException {
getLabelledForwardScores();
populate(labelledForwardsScores, forwardsScores);
labelledReverseScores = new HashMap<String, Float>();
populate(labelledReverseScores, reverseScores);
out.defaultWriteObject();
}
private Map<String,Float> getLabelledForwardScores() {
if (labelledForwardsScores == null){
labelledForwardsScores = new HashMap<String, Float>();
}
return labelledForwardsScores;
}
private Map<String,Float> getLabelledReverseScores() {
if (labelledReverseScores == null){
labelledReverseScores = new HashMap<String, Float>();
}
return labelledReverseScores;
}
private void populate(HashMap<String, Float> dest, HashMap<Scorer, Float> src) {
for (Entry<Scorer, Float> entry : src.entrySet()) {
String name = entry.getKey().getName();
if (name == null){
name = String.valueOf(entry.getKey().getScorerAttrId());
}
dest.put(name, entry.getValue());
}
}
public Collection<String> getScorerAttrNames() {
Assert.state(labelledForwardsScores != null, "Cannot call getScorerAttrNames() on server");
HashSet<String> result = new HashSet<String>(labelledForwardsScores.keySet());
result.addAll(labelledReverseScores.keySet());
return result;
}
/**
* This is supported server side by creating a dummy scorer instance
*/
@Override
public void setScorerAttribute(Direction d, String name, float value) {
Map<String,Float> map = d == Direction.forwards ? getLabelledForwardScores() : getLabelledReverseScores();
map.put(name, value);
}
public Float getForwardsScore(String name) {
return getLabelledForwardScores().get(name);
}
public Float getReverseScore(String name) {
return getLabelledReverseScores().get(name);
}
}