SphericalKat / dart-fuzzywuzzy

A dart port of the popular fuzzywuzzy package
https://pub.dev/packages/fuzzywuzzy
GNU General Public License v2.0
39 stars 7 forks source link

Anoying exception raised in debug mode: extractTop generate bad state exception #7

Closed wer-mathurin closed 2 years ago

wer-mathurin commented 2 years ago

@SphericalKat This is due to this method: The removeFirst can throw an exception because it can be empty. So this can be annoying when debugging with the catch all exceptions enabled!

List<ExtractedResult<T>> _findTopKHeap<T>(
      List<ExtractedResult<T>> arr, int k) {
    var pq = PriorityQueue<ExtractedResult<T>>();

    for (var x in arr) {
      if (pq.length < k) {
        pq.add(x);
      } else if (x.compareTo(pq.first) > 0) {
        pq.removeFirst();
        pq.add(x);
      }
    }
    var res = List<ExtractedResult<T>>.empty(growable: true);
    for (var i = k; i > 0; i--) {
      try {
        **var polled = pq.removeFirst();**
        res.add(polled);
      } catch (e) {
        continue;
      }
    }
    return res;
  }

What about doing a little check before, instead of relying on the exception throw? Seems cleaner to me. What do you think?

 List<ExtractedResult<T>> extractTop<T>(
      String query, List<T> choices, Applicable func, int limit,
      [String Function(T obj)? getter]) {
    var best = extractWithoutOrder(query, choices, func, getter);
    var results = _findTopKHeap(best, limit);
    return results.reversed.toList();
  }

  List<ExtractedResult<T>> _findTopKHeap<T>(
      List<ExtractedResult<T>> arr, int k) {
    var pq = PriorityQueue<ExtractedResult<T>>();

    for (var x in arr) {
      if (pq.length < k) {
        pq.add(x);
      } else if (x.compareTo(pq.first) > 0) {
        pq.removeFirst();
        pq.add(x);
      }
    }
    var res = List<ExtractedResult<T>>.empty(growable: true);
    for (var i = k; i > 0; i--) {
      if (pq.isNotEmpty) {
        res.add(pq.removeFirst());
      }
    }
    return res;
  }
wer-mathurin commented 2 years ago

@SphericalKat I did a pull request(https://github.com/SphericalKat/dart-fuzzywuzzy/pull/8) to fix that. If you can merge it this will be awesome.

SphericalKat commented 2 years ago

Hello, I apologize for being unresponsive; I've been busy with some situations.

Thank you for the PR! I'll merge it as soon as the checks pass.

wer-mathurin commented 2 years ago

Will you be able to generate another version of the package? Or let me know what I need to do to help you!

Louis-Michel Mathurin

T : (514) 705-0228

On Tue, 16 Nov 2021 at 17:42, Amogh Milind Lele @.***> wrote:

Closed #7 https://github.com/SphericalKat/dart-fuzzywuzzy/issues/7.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/SphericalKat/dart-fuzzywuzzy/issues/7#event-5628216168, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJL6EZYSZSDCEZC4S2VACFTUMLM3RANCNFSM5IB7B4TQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.