Skip to content

Commit

Permalink
Remove Score as we no longer need that abstraction. (dart-lang#8289)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Nov 14, 2024
1 parent ca42f46 commit b5584bd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ class PackageNameIndex {

/// Search [text] and return the matching packages with scores.
@visibleForTesting
Score search(String text) {
Map<String, double> search(String text) {
IndexedScore<String>? score;
for (final w in splitForQuery(text)) {
final s = searchWord(w, filterOnNonZeros: score);
Expand All @@ -489,7 +489,7 @@ class PackageNameIndex {
score.multiplyAllFrom(s);
}
}
return score?.toScore() ?? Score.empty;
return score?.toMap() ?? {};
}

/// Search using the parsed [word] and return the matching packages with scores
Expand Down
13 changes: 7 additions & 6 deletions app/lib/search/sdk_mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:math';

import 'package:_pub_shared/utils/http.dart';
// ignore: implementation_imports
import 'package:pana/src/dartdoc/dartdoc_index.dart';
Expand Down Expand Up @@ -135,18 +137,17 @@ class SdkMemIndex {
final isQualifiedQuery = query.contains(library.split(':').last);

final tokens = _tokensPerLibrary[library]!;
final plainResults =
Score(tokens.searchWords(words).top(3, minValue: 0.05));
final plainResults = tokens.searchWords(words).top(3, minValue: 0.05);
if (plainResults.isEmpty) continue;

final libraryWeight = _libraryWeights[library] ?? 1.0;
final weightedResults = isQualifiedQuery
? plainResults
: plainResults.mapValues(
: plainResults.map(
(key, value) {
final dir = p.dirname(key);
final w = (_apiPageDirWeights[dir] ?? 1.0) * libraryWeight;
return w * value;
return MapEntry(key, w * value);
},
);

Expand Down Expand Up @@ -185,9 +186,9 @@ class SdkMemIndex {

class _Hit {
final String library;
final Score top;
final Map<String, double> top;

_Hit(this.library, this.top);

late final score = top.maxValue;
late final score = top.values.fold(0.0, (a, b) => max(a, b));
}
28 changes: 4 additions & 24 deletions app/lib/search/token_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,6 @@ import 'package:meta/meta.dart';

import 'text_utils.dart';

/// Represents an evaluated score as an {id: score} map.
extension type const Score._(Map<String, double> _values)
implements Map<String, double> {
static const Score empty = Score._({});

Score(this._values);

factory Score.fromEntries(Iterable<MapEntry<String, double>> entries) =>
Score(Map.fromEntries(entries));

double get maxValue => _values.values.fold(0.0, math.max);

/// Transfer the score values with [f].
Score mapValues(double Function(String key, double value) f) =>
Score.fromEntries(
_values.entries.map((e) => MapEntry(e.key, f(e.key, e.value))));
}

/// The weighted tokens used for the final search.
class TokenMatch {
final Map<String, double> _tokenWeights = <String, double>{};
Expand Down Expand Up @@ -151,7 +133,7 @@ extension StringTokenIndexExt on TokenIndex<String> {
/// scoring.
@visibleForTesting
Map<String, double> search(String text) {
return searchWords(splitForQuery(text)).toScore();
return searchWords(splitForQuery(text)).toMap();
}
}

Expand Down Expand Up @@ -250,17 +232,15 @@ class IndexedScore<K> {
}
return Map.fromEntries(list.map((i) => MapEntry(_keys[i], _values[i])));
}
}

extension StringIndexedScoreExt on IndexedScore<String> {
Score toScore() {
final map = <String, double>{};
Map<K, double> toMap() {
final map = <K, double>{};
for (var i = 0; i < _values.length; i++) {
final v = _values[i];
if (v > 0.0) {
map[_keys[i]] = v;
}
}
return Score._(map);
return map;
}
}

0 comments on commit b5584bd

Please sign in to comment.