SphericalKat / dart-fuzzywuzzy

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

📳RangeError (index): Invalid value: Not in inclusive range 0..26: 30 👀 #6

Closed Lakhankumawat closed 1 year ago

Lakhankumawat commented 2 years ago

Error 🙄

======== Exception caught by widgets library =======================================================
The following RangeError was thrown building _SearchPage<String>(dirty, dependencies: [_LocalizationsScope-[GlobalKey#e1ca9], _InheritedTheme], state: _SearchPageState<String>#62f97):
RangeError (index): Invalid value: Not in inclusive range 0..26: 30

The relevant error-causing widget was: 
  MaterialApp MaterialApp:file:///F:/Marketing-App/lib/main.dart:8:10
When the exception was thrown, this was the stack: 
#0      List.[] (dart:core-patch/growable_array.dart:254:60)
#1      DiffUtils.levEditDistance (package:fuzzywuzzy/diffutils/diff_utils.dart:600:37)
#2      DiffUtils.getRatio (package:fuzzywuzzy/diffutils/diff_utils.dart:732:24)
#3      SimpleRatio.apply (package:fuzzywuzzy/ratios/simple_ratio.dart:7:29)
#4      ratio (package:fuzzywuzzy/fuzzywuzzy.dart:14:24)
#5      WeightedRatio.apply (package:fuzzywuzzy/algorithms/weighted_ratio.dart:26:16)
#6      Extractor.extractWithoutOrder (package:fuzzywuzzy/extractor.dart:27:24)
#7      extractAll (package:fuzzywuzzy/fuzzywuzzy.dart:94:20)

My FuzzyWuzzy Implementation

List<String> testing =
          extractAll(query: query, choices: suggestions, cutoff: 10)
              .map((e) => e.choice.toString())
              .toList();

I am currently using fuzzy search inside Search Delegate flutter , but i don't think issue is through that as you'll see;

Suggestions (choice list)

Sugesstions are my choices and its a large list :

[Test Title, YO, HELLO TITLE, Test title, Test title, Test title isha, Test title, Top 10 Highest Paying Jobs in India 2021, NEET Preparation Tips, Syllabus & Best Books for 2021, Best Books for JEE Preparation 2021, 2022, 2023 [Main + Advanced], Best Indian Romance Novels | A List of 10 Romantic Books, All the Harry Potter Books in Order: Your J.K. Rowling Reading List, Top 5 Sci Fi Books Ever Written Of All Time | Must Read, NEET PG 2021: Admit Card to be released on April 12. Check details, NTA Extends JEE Main 2021 April Application Correction Date - Check Details Here, TOP REASONS TO BUY SECOND HAND BOOKS ONLINE, KNOW HOW THE HABIT OF READING BOOKS CAN CHANGE YOUR LIFE!, Why to buy pre-loved books, This comic collector has one of the largest collections of vintage India-published comic books, As book publishing shrinks during the pandemic, how are India’s printing presses coping, Vikram Seth’s A Suitable Boy will soon stream on Netflix; here’s what the novel is all about, 10 Most Controversial Nov

Remarks

suggestions list have string with greater length - I tried by using sub string of each string in suggestions , issue remained same Also suggestion is of large length

Additional context

flutter doctor


F:\flutter_ecommerce_app>flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
Flutter (Channel stable, 2.5.0, on Microsoft Windows [Version 10.0.19043.1151], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Android Studio (version 2020.3)
[√] VS Code (version 1.60.1)
[√] Connected device (3 available)

• No issues found!

> flutter doctor verbose

F:\Fluttter Apps\customer>flutter doctor -v [√] Flutter (Channel stable, 2.5.0, on Microsoft Windows [Version 10.0.19043.1151], locale en-US) • Flutter version 2.5.0 at D:\src\flutter\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 4cc385b4b8 (13 days ago), 2021-09-07 23:01:49 -0700 • Engine revision f0826da7ef • Dart version 2.14.0

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at C:\Users\LAKHN KUMAWAT\AppData\Local\Android\sdk • Platform android-31, build-tools 31.0.0 • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted.

[√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 2020.3) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.60.1) • VS Code at C:\Users\LAKHN KUMAWAT\AppData\Local\Programs\Microsoft VS Code • Flutter extension can be installed from: https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (3 available) • RMX1851 (mobile) • 4e5981a2 • android-arm64 • Android 11 (API 30) • Chrome (web) • chrome • web-javascript • Google Chrome 93.0.4577.82 • Edge (web) • edge • web-javascript • Microsoft Edge 93.0.961.47

• No issues found!

> flutter version

F:\flutter_ecommerce_app>flutter --version Flutter 2.5.0 • channel stable • https://github.com/flutter/flutter.git Framework • revision 4cc385b4b8 (9 days ago) • 2021-09-07 23:01:49 -0700 Engine • revision f0826da7ef Tools • Dart 2.14.0



@SphericalKat  Please help me if you get my issue 🥺.
Lakhankumawat commented 2 years ago

No Updates ?

Lakhankumawat commented 2 years ago

:eyes:

vjamrich commented 2 years ago

Same error here. I have around 300 000 choices. The issue goes away with a shorter choices list.

SphericalKat commented 2 years ago

Hello @Lakhankumawat @vjamrich, sorry for the inactivity. Could either of you send me a sample list that can reproduce the problem? Thanks.

sgehrman commented 2 years ago

I'm seeing this:

Error thrown by handler. RangeError (index): Invalid value: Not in inclusive range 0..214: 216 dart:core List.[] DiffUtils.levEditDistance package:fuzzywuzzy/diffutils/diff_utils.dart:600 DiffUtils.getRatio package:fuzzywuzzy/diffutils/diff_utils.dart:732 SimpleRatio.apply package:fuzzywuzzy/ratios/simple_ratio.dart:7 ratio package:fuzzywuzzy/fuzzywuzzy.dart:14 WeightedRatio.apply package:fuzzywuzzy/algorithms/weighted_ratio.dart:26 Extractor.extractWithoutOrder package:fuzzywuzzy/extractor.dart:27 Extractor.extractSorted package:fuzzywuzzy/extractor.dart:53 extractAllSorted

VuillaumeGautier commented 2 years ago

Hi ! I have the same error being thrown when I give a string with an emoji in it, could it be related ?

vjamrich commented 2 years ago

Hi ! I have the same error being thrown when I give a string with an emoji in it, could it be related ?

To expand on this, my dataset included chinese characters (UTF-8 encoding). Although taking a random subset of the dataset did not trigger the error - therefore I thought this might have been unrelated.

The dataset used consisted of the following dictionary - https://www.mdbg.net/chinese/dictionary?page=cedict

SphericalKat commented 2 years ago

Hi ! I have the same error being thrown when I give a string with an emoji in it, could it be related ?

Could you post your dataset if that's possible? If not; could you try removing any non-ascii characters from the dataset and see if it doesn't crash?

SphericalKat commented 2 years ago

Hi ! I have the same error being thrown when I give a string with an emoji in it, could it be related ?

To expand on this, my dataset included chinese characters (UTF-8 encoding). Although taking a random subset of the dataset did not trigger the error - therefore I thought this might have been unrelated.

The dataset used consisted of the following dictionary - https://www.mdbg.net/chinese/dictionary?page=cedict

Thank you, I'll take a look with this.

VuillaumeGautier commented 2 years ago

I don't have a dataset to give, as it's straight up test from a textfield : any emoji can make a crash, just removing it fix it. I haven't tested chinese character, but japanese (and their chinese characters) seems to be fine on my side (it is just considered as a character, fuzziness still works fine).

vimaxwell commented 1 year ago

I encounter same issue with this code ratio("🌙 love song", "love")

woutervanwijk commented 1 year ago

Same here. It's a unicode problem maybe?

seceba commented 1 year ago

Hello, I fixed the code.

Go to diff_utils.dart find levEditDistance() function.

Add int maxRange = 1114111; at the top of the function.

find these codes

var c1 = s1.runes.toList();
var c2 = s2.runes.toList(); 

replace it with this

var c1 = s1.codeUnits.toList();
var c2 = s2.codeUnits.toList();

This will work.

Inakitajes commented 1 year ago

When does @SphericalKat will merge this fix?

As some you already stated, the issue is related with the special characters handling (emojis in my case).

As an easy temporary fix, you can use a regex to delete all non ASCII chars within the choice getter:

extractAllSorted<SearchResultItem>(
    query: query,
    choices: options,
    cutoff: 65,
    getter: (x) => x.name.replaceAll(RegExp(r'[^\x00-\x7F]'), ''),
);
Kal-Elx commented 1 year ago

I can confirm that this is still a problem. @SphericalKat can you merge @seceba's suggestion?

Kal-Elx commented 1 year ago

I created a PR with the changes suggested by @seceba and it works. Until it's merged you can use it by making this change in your pubspec.yaml.

fuzzywuzzy:
    # Use a fork where non ascii characters are handled.
    git:
      url: https://github.com/Kal-Elx/dart-fuzzywuzzy.git
      ref: fc70dd42f1d526ec3634af81598991173e272e09
guplem commented 1 year ago

I faced the same issue and can confirm the PR fixes it. Thank you very much @Kal-Elx

vjamrich commented 1 year ago

I can also confirm, the PR also fixed the issue on my side!

SphericalKat commented 1 year ago

Thanks for the fix @Kal-Elx This has been released to pub.dev!