Skip to content

Commit

Permalink
Differentiate between name match with underscores and without them. (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Feb 28, 2025
1 parent 1627b92 commit 6bc0917
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
28 changes: 18 additions & 10 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -541,15 +541,13 @@ class PackageNameIndex {

PackageNameIndex(this._packageNames) {
_data = _packageNames.map((package) {
final collapsed = _collapseName(package);
return _PkgNameData(collapsed, trigrams(collapsed).toSet());
final lowercased = package.toLowerCase();
final collapsed = _removeUnderscores(lowercased);
return _PkgNameData(lowercased, collapsed, trigrams(collapsed).toSet());
}).toList();
}

/// Maps package name to a reduced form of the name:
/// the same character parts, but without `-`.
String _collapseName(String package) =>
package.replaceAll('_', '').toLowerCase();
String _removeUnderscores(String text) => text.replaceAll('_', '');

/// Search [text] and return the matching packages with scores.
@visibleForTesting
Expand Down Expand Up @@ -581,7 +579,8 @@ class PackageNameIndex {
final singularWord = word.length <= 3 || !word.endsWith('s')
? word
: word.substring(0, word.length - 1);
final collapsedWord = _collapseName(singularWord);
final lowercasedWord = singularWord.toLowerCase();
final collapsedWord = _removeUnderscores(lowercasedWord);
final parts =
collapsedWord.length <= 3 ? [collapsedWord] : trigrams(collapsedWord);
for (var i = 0; i < _data.length; i++) {
Expand All @@ -590,8 +589,16 @@ class PackageNameIndex {
}

final entry = _data[i];
if (entry.collapsed.contains(collapsedWord)) {
score.setValue(i, 1.0);
if (entry.collapsed.length >= collapsedWord.length &&
entry.collapsed.contains(collapsedWord)) {
// also check for non-collapsed match
if (entry.lowercased.length >= lowercasedWord.length &&
entry.lowercased.contains(lowercasedWord)) {
score.setValue(i, 1.0);
continue;
}

score.setValue(i, 0.99);
continue;
}
var matched = 0;
Expand Down Expand Up @@ -622,10 +629,11 @@ class PackageNameIndex {
}

class _PkgNameData {
final String lowercased;
final String collapsed;
final Set<String> trigrams;

_PkgNameData(this.collapsed, this.trigrams);
_PkgNameData(this.lowercased, this.collapsed, this.trigrams);
}

extension on List<IndexedPackageHit> {
Expand Down
7 changes: 7 additions & 0 deletions app/test/search/package_name_index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,12 @@ void main() {
{},
);
});

test('substring: entu', () {
expect(index.search('entu'), {
'fluent': 0.5,
'fluent_ui': 0.99, // not 1.0
});
});
});
}
2 changes: 1 addition & 1 deletion app/test/search/stack_trace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void main() {
'packageHits': [
{
'package': 'stack_trace',
'score': 1.0,
'score': 0.99,
},
],
});
Expand Down

0 comments on commit 6bc0917

Please sign in to comment.