Skip to content

Commit cb30bed

Browse files
committed
Use percentile^2 normalization for like and download scores.
1 parent 3ac305e commit cb30bed

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

app/lib/search/models.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:math';
6+
57
import 'package:clock/clock.dart';
68
import 'package:collection/collection.dart';
79
import 'package:json_annotation/json_annotation.dart';
@@ -68,7 +70,7 @@ extension UpdateLikesExt on Iterable<PackageDocument> {
6870
if (i > 0 && list[i - 1].downloadCount == list[i].downloadCount) {
6971
list[i].downloadScore = list[i - 1].downloadScore;
7072
} else {
71-
list[i].downloadScore = (i + 1) / list.length;
73+
list[i].downloadScore = pow((i + 1) / list.length, 2).toDouble();
7274
}
7375
}
7476
}
@@ -77,13 +79,12 @@ extension UpdateLikesExt on Iterable<PackageDocument> {
7779
/// The score is normalized into the range of [0.0 - 1.0] using the
7880
/// ordered list of packages by like counts (same like count gets the same score).
7981
void updateLikeScores() {
80-
final sortedByLikes = sorted((a, b) => a.likeCount.compareTo(b.likeCount));
81-
for (var i = 0; i < sortedByLikes.length; i++) {
82-
if (i > 0 &&
83-
sortedByLikes[i - 1].likeCount == sortedByLikes[i].likeCount) {
84-
sortedByLikes[i].likeScore = sortedByLikes[i - 1].likeScore;
82+
final list = sorted((a, b) => a.likeCount.compareTo(b.likeCount));
83+
for (var i = 0; i < list.length; i++) {
84+
if (i > 0 && list[i - 1].likeCount == list[i].likeCount) {
85+
list[i].likeScore = list[i - 1].likeScore;
8586
} else {
86-
sortedByLikes[i].likeScore = (i + 1) / sortedByLikes.length;
87+
list[i].likeScore = pow((i + 1) / list.length, 2).toDouble();
8788
}
8889
}
8990
}

app/test/search/mem_index_test.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
106106
'totalCount': 1,
107107
'sdkLibraryHits': [],
108108
'packageHits': [
109-
{'package': 'async', 'score': closeTo(0.71, 0.01)},
109+
{'package': 'async', 'score': closeTo(0.68, 0.01)},
110110
],
111111
});
112112
});
@@ -134,7 +134,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
134134
'totalCount': 1,
135135
'sdkLibraryHits': [],
136136
'packageHits': [
137-
{'package': 'chrome_net', 'score': closeTo(0.33, 0.01)},
137+
{'package': 'chrome_net', 'score': closeTo(0.30, 0.01)},
138138
],
139139
});
140140
});
@@ -148,8 +148,8 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
148148
'totalCount': 2,
149149
'sdkLibraryHits': [],
150150
'packageHits': [
151-
{'package': 'async', 'score': closeTo(0.71, 0.01)},
152151
{'package': 'http', 'score': closeTo(0.69, 0.01)},
152+
{'package': 'async', 'score': closeTo(0.67, 0.01)},
153153
],
154154
});
155155
});
@@ -163,7 +163,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
163163
'totalCount': 1,
164164
'sdkLibraryHits': [],
165165
'packageHits': [
166-
{'package': 'async', 'score': closeTo(0.37, 0.01)},
166+
{'package': 'async', 'score': closeTo(0.35, 0.01)},
167167
],
168168
});
169169
});
@@ -348,7 +348,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
348348
'sdkLibraryHits': [],
349349
'packageHits': [
350350
{'package': 'http', 'score': closeTo(0.92, 0.01)},
351-
{'package': 'async', 'score': closeTo(0.52, 0.01)},
351+
{'package': 'async', 'score': closeTo(0.46, 0.01)},
352352
],
353353
});
354354

@@ -368,7 +368,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
368368
'totalCount': 1,
369369
'sdkLibraryHits': [],
370370
'packageHits': [
371-
{'package': 'chrome_net', 'score': closeTo(0.08, 0.01)},
371+
{'package': 'chrome_net', 'score': closeTo(0.03, 0.01)},
372372
],
373373
});
374374
});
@@ -383,7 +383,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
383383
'sdkLibraryHits': [],
384384
'packageHits': [
385385
{'package': 'http', 'score': closeTo(0.92, 0.01)},
386-
{'package': 'chrome_net', 'score': closeTo(0.08, 0.01)},
386+
{'package': 'chrome_net', 'score': closeTo(0.03, 0.01)},
387387
],
388388
});
389389

@@ -443,7 +443,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
443443
'totalCount': 1,
444444
'sdkLibraryHits': [],
445445
'packageHits': [
446-
{'package': 'async', 'score': closeTo(0.52, 0.01)},
446+
{'package': 'async', 'score': closeTo(0.46, 0.01)},
447447
],
448448
});
449449

@@ -677,7 +677,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
677677
'totalCount': 2,
678678
'sdkLibraryHits': [],
679679
'packageHits': [
680-
{'package': 'def', 'score': closeTo(0.85, 0.01)},
680+
{'package': 'def', 'score': closeTo(0.78, 0.01)},
681681
{'package': 'abc', 'score': closeTo(0.70, 0.01)},
682682
],
683683
},
@@ -693,7 +693,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
693693
'packageHits': [
694694
// `abc` is at the first position, score is kept
695695
{'package': 'abc', 'score': closeTo(0.70, 0.01)},
696-
{'package': 'def', 'score': closeTo(0.85, 0.01)},
696+
{'package': 'def', 'score': closeTo(0.78, 0.01)},
697697
],
698698
},
699699
);
@@ -705,7 +705,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
705705
'totalCount': 2,
706706
'sdkLibraryHits': [],
707707
'packageHits': [
708-
{'package': 'def', 'score': closeTo(0.85, 0.01)},
708+
{'package': 'def', 'score': closeTo(0.78, 0.01)},
709709
{'package': 'abc', 'score': closeTo(0.70, 0.01)},
710710
],
711711
},
@@ -721,7 +721,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
721721
'sdkLibraryHits': [],
722722
'packageHits': [
723723
// `abc` is at the original position
724-
{'package': 'def', 'score': closeTo(0.85, 0.01)},
724+
{'package': 'def', 'score': closeTo(0.78, 0.01)},
725725
{'package': 'abc', 'score': closeTo(0.70, 0.01)},
726726
],
727727
},
@@ -737,7 +737,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
737737
'sdkLibraryHits': [],
738738
'packageHits': [
739739
// `abc` is absent
740-
{'package': 'def', 'score': closeTo(0.85, 0.01)},
740+
{'package': 'def', 'score': closeTo(0.78, 0.01)},
741741
],
742742
},
743743
);
@@ -775,7 +775,7 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
775775
'nameMatches': ['abc_def'],
776776
'sdkLibraryHits': [],
777777
'packageHits': [
778-
{'package': 'abc_def', 'score': closeTo(0.55, 0.01)},
778+
{'package': 'abc_def', 'score': closeTo(0.48, 0.01)},
779779
{'package': 'abc_def_xyz', 'score': closeTo(1.0, 0.01)},
780780
],
781781
});

app/test/service/entrypoint/search_index_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void main() {
4949
'totalCount': 1,
5050
'sdkLibraryHits': [],
5151
'packageHits': [
52-
{'package': 'json_annotation', 'score': greaterThan(0.5)},
52+
{'package': 'json_annotation', 'score': greaterThan(0.47)},
5353
],
5454
});
5555

0 commit comments

Comments
 (0)