comigor / fuzzy

Fuzzy search in Dart
MIT License
37 stars 20 forks source link

Score not showing properly #8

Open thencke opened 4 years ago

thencke commented 4 years ago

First of all, amazing library. But, I guess the score property of result object is always zero (even when it hasn't found any matches).

Example:

void testFuzzy() {
    final fuse = Fuzzy(
      [
        Person('John', 'Doe', 29),
        Person('Jane', 'Doe', 32),
        Person('Peter', 'Parker', 36),
        Person('Bruce', 'Wayne', 30),
        Person('Clark', 'Kent', 50),
        Person('Tony', 'Stark', 34),
        Person('Clint', 'Barton', 35),
      ],
      options: FuzzyOptions(
        keys: [WeightedKey<Person>(name: 'firstname', getter: (p) => p.firstname, weight: 1)],
        shouldNormalize: true,
        isCaseSensitive: false,
        shouldSort: false,
        findAllMatches: false,
        verbose: true,
      ),
    );

    var result = fuse.search('John');

    if (result.isNotEmpty) {
      result
          .forEach((r) => print('${r.item.firstname} ${r.item.surname} (${r.item.age}) | score: ${r.score}'));
    } else {
      print('no results');
    }
  }

class Person {
  final String firstname;
  final String surname;
  final int age;

  Person(this.firstname, this.surname, this.age);
}

output shows:

flutter: Full text: "John", score: 0.0 flutter: Score average (final): 0.0 flutter: Check Matches: true flutter: Full text: "Jane", score: 0.5 flutter: Score average (final): 0.5 flutter: Check Matches: true flutter: Full text: "Peter", score: 1.0 flutter: Score average (final): 1.0 flutter: Check Matches: true flutter: Full text: "Bruce", score: 1.0 flutter: Score average (final): 1.0 flutter: Check Matches: true flutter: Full text: "Clark", score: 1.0 flutter: Score average (final): 1.0 flutter: Check Matches: true flutter: Full text: "Tony", score: 0.5 flutter: Score average (final): 0.5 flutter: Check Matches: true flutter: Full text: "Clint", score: 1.0 flutter: Score average (final): 1.0 flutter: Check Matches: true flutter: Computing score: flutter: John Doe (29) | score: 0.0 flutter: Jane Doe (32) | score: 0.0 flutter: Tony Stark (34) | score: 0.0

We can see that on verbose output the score is taken correctly, but when printing after search, is aways zero.

I can workaround it doing:

    result.forEach((r) => r.score = r.matches[0].score);
    result.sort((a,b) => a.score.compareTo(b.score));

But I'm not happy with this approach, is there a solution for this? Or perhaps I'm doing something wrong, please help. Thanks in advance

thencke commented 4 years ago

I forgot to mention that results are not sorted by score as well (even with shouldSort: true), due to the same problem, score always equal to zero, so sorting will not take effect.

comigor commented 4 years ago

Hello @blinkn! Sorry for taking a long time to answer this.

This library was a hacky implementation of Fuse.js in Dart and those sort of errors could definitely occur, sorry about that.

I'm not sure that I'll be able to take a look at this soon, but if you could, please update us in the process, and open a PR here so other could benefit from it as well!