Skip to content

Commit 0e90686

Browse files
committed
Slightly lower score for depluralized matches.
1 parent ffc5b3b commit 0e90686

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

app/lib/search/mem_index.dart

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,11 +619,20 @@ class PackageNameIndex {
619619
IndexedScore<String>? filterOnNonZeros,
620620
}) {
621621
assert(score.keys.length == _packageNames.length);
622-
final singularWord = word.length <= 3 || !word.endsWith('s')
623-
? word
624-
: word.substring(0, word.length - 1);
625-
final lowercasedWord = singularWord.toLowerCase();
622+
final lowercasedWord = word.toLowerCase();
626623
final collapsedWord = _removeUnderscores(lowercasedWord);
624+
625+
// Note: This is a very simple plurality check, which may not work wor many
626+
// cases, but provided a simple approximation.
627+
// The check should be done on the lowercased input value.
628+
final lowercaseDepluralized =
629+
lowercasedWord.length <= 3 || !lowercasedWord.endsWith('s')
630+
? null
631+
: lowercasedWord.substring(0, lowercasedWord.length - 1);
632+
final collapsedDepluralized = lowercaseDepluralized == null
633+
? null
634+
: _removeUnderscores(lowercaseDepluralized);
635+
627636
final parts =
628637
collapsedWord.length <= 3 ? [collapsedWord] : trigrams(collapsedWord);
629638
for (var i = 0; i < _data.length; i++) {
@@ -632,18 +641,24 @@ class PackageNameIndex {
632641
}
633642

634643
final entry = _data[i];
635-
if (entry.collapsed.length >= collapsedWord.length &&
636-
entry.collapsed.contains(collapsedWord)) {
637-
// also check for non-collapsed match
638-
if (entry.lowercased.length >= lowercasedWord.length &&
639-
entry.lowercased.contains(lowercasedWord)) {
644+
// Check for direct substring match.
645+
// TODO: Consider using trie or other substring index here.
646+
if (entry._containsCollapsed(collapsedDepluralized ?? collapsedWord)) {
647+
// most score for original non-collapsed match
648+
if (entry._containsLowercased(lowercasedWord)) {
640649
score.setValue(i, 1.0);
641650
continue;
642651
}
643652

653+
// otherwise a slightly lower score for:
654+
// - collapsed-only original match
655+
// - non-collapsed depluralized match
656+
// - collapsed depluralized match
644657
score.setValue(i, 0.99);
658+
645659
continue;
646660
}
661+
647662
var matched = 0;
648663
var unmatched = 0;
649664
final acceptThreshold = parts.length ~/ 2;
@@ -682,6 +697,14 @@ class _PkgNameData {
682697
final Set<String> trigrams;
683698

684699
_PkgNameData(this.lowercased, this.collapsed, this.trigrams);
700+
701+
bool _containsLowercased(String value) {
702+
return lowercased.length >= value.length && lowercased.contains(value);
703+
}
704+
705+
bool _containsCollapsed(String value) {
706+
return collapsed.length >= value.length && collapsed.contains(value);
707+
}
685708
}
686709

687710
extension on List<IndexedPackageHit> {

app/test/search/maps_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void main() {
3030
'sdkLibraryHits': [],
3131
'packageHits': [
3232
{'package': 'maps', 'score': 1.0},
33-
{'package': 'map', 'score': 1.0},
33+
{'package': 'map', 'score': 0.99},
3434
],
3535
});
3636
});

app/test/search/mem_index_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ server.dart adds a small, prescriptive server (PicoServer) that can be configure
562562
final match2 = index.search(
563563
ServiceSearchQuery.parse(query: 'apps', order: SearchOrder.text));
564564
expect(match2.packageHits.map((e) => e.toJson()), [
565-
{'package': 'app', 'score': 1.0},
566565
{'package': 'apps', 'score': 1.0},
566+
{'package': 'app', 'score': 0.99},
567567
]);
568568
});
569569

app/test/search/package_name_index_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,20 @@ void main() {
127127
});
128128
});
129129
});
130+
131+
group('redis', () {
132+
final index = PackageNameIndex([
133+
'redis',
134+
'x_redis_client',
135+
'credit_union',
136+
]);
137+
138+
test('redis', () {
139+
expect(index.search('redis'), {
140+
'redis': 1.0,
141+
'x_redis_client': 1.0,
142+
'credit_union': 0.99,
143+
});
144+
});
145+
});
130146
}

0 commit comments

Comments
 (0)