fuzzy_bolt 1.1.8 copy "fuzzy_bolt: ^1.1.8" to clipboard
fuzzy_bolt: ^1.1.8 copied to clipboard

A high-performance fuzzy search algorithm in Dart, designed for intelligent auto-suggestions, typo tolerance, and fast string matching.

example/fuzzy_bolt_example.dart

import 'dart:async';

import 'package:fuzzy_bolt/fuzzy_bolt.dart';

void main() async {
  List<String> dataset = [
    "apple",
    "banana",
    "grape",
    "apricot",
    "mango",
    "Pineapple",
    "blueberry",
    "Strawberry",
    "watermelon",
    "cantaloupe",
    "raspberry",
    "blackberry",
    "dragonfruit",
    "kiwi",
    "mangosteen",
    "grapefruit",
    "pomegranate",
    "lemon",
    "lime",
    "orange",
    "tangerine",
  ];

  List<Map<String, dynamic>> testCases = [
    {"query": "aple", "strict": 0.85, "typo": 0.7}, // Typo Handling
    {"query": "mengo", "strict": 0.85, "typo": 0.6}, // Close Phonetic Match
    {"query": "berry", "strict": 0.6, "typo": 0.5}, // Partial Match
    {"query": "PINEAPPLE", "strict": 0.9, "typo": 0.8}, // Case Insensitivity
    {"query": "pomgranate", "strict": 0.8, "typo": 0.6}, // Complex Typo
  ];

  // āœ… Basic Search Test
  print("šŸš€ Running Basic Search Test...");

  for (var test in testCases) {
    print("šŸ” Searching for: '${test["query"]}'");
    var basicSearchTextResults = await FuzzyBolt().search(
      dataset: dataset,
      query: test["query"] as String,
      strictThreshold: test["strict"] as double,
      typoThreshold: test["typo"] as double,
    );

    print("šŸ“Œ Top Results:");
    basicSearchTextResults.map((e) => print('$e \n')).toList();
  }

  // āœ… Standard Search With Ranked Test
  for (var test in testCases) {
    print("šŸ” Searching for: '${test["query"]}'");
    var searchRankedResults = await FuzzyBolt().searchWithRanks(
      dataset: dataset,
      query: test["query"] as String,
      strictThreshold: test["strict"] as double,
      typoThreshold: test["typo"] as double,
    );

    print("šŸ“Œ Top Results:");
    for (var res in searchRankedResults) {
      print(
        "   šŸ”¹ ${res['value']} (Score: ${res['rank'].toStringAsFixed(3)}) \n",
      );
    }
  }

  // āœ… Web Support Test
  print("šŸš€ Web Support Test...");
  for (var test in testCases) {
    try {
      print("šŸ” Searching for: '${test["query"]}'");
      var webSupportResults = await FuzzyBolt().searchWithRanks(
        dataset: dataset,
        query: test["query"] as String,
        strictThreshold: test["strict"] as double,
        typoThreshold: test["typo"] as double,
        kIsWeb: true,
      );

      print("šŸ“Œ Top Results:");
      for (var res in webSupportResults) {
        print(
          "   šŸ”¹ ${res['value']} (Score: ${res['rank'].toStringAsFixed(3)}) \n",
        );
      }
    } catch (e) {
      print(e);
    }
  }

  // āœ… Stream-Based Search Test
  print("šŸš€ Running Stream-Based Search...");

  final StreamController<String> queryStreamControllers =
      StreamController<String>();

  final Stream<List<String>> searchResults = FuzzyBolt().streamSearch(
    dataset: dataset,
    query: queryStreamControllers.stream,
    strictThreshold: 0.6,
    typoThreshold: 0.5,
  );

  // Listen for search results
  final StreamSubscription<List<String>> subscriptions = searchResults.listen(
    (results) {
      print("šŸ”„ Stream Update:");
      for (var res in results) {
        print("   šŸ”¹ $res");
      }
    },
    onError: (error) => print("āš ļø Stream Error: $error"),
    onDone: () => print("āœ… Stream Completed"),
  );
  // Simulate typing "a" -> "p" -> "l" -> "e" with a delay
  List<String> querySequences = ["b", "be", "ber", "berr", "berry"];

  for (var query in querySequences) {
    await Future.delayed(Duration(milliseconds: 300)); // Simulate typing delay
    print("\nāŒØļø Typing: '$query'");

    queryStreamControllers.add(query);
  }

  // Close the query stream after execution
  await Future.delayed(Duration(seconds: 1));
  await queryStreamControllers.close();
  await subscriptions.cancel();

  print("šŸ Stream-based search completed.");

  // āœ… Stream-Based Search With Ranked Test
  print("šŸš€ Running Stream-Based Search With Ranked...");

  final StreamController<String> queryStreamController =
      StreamController<String>();

  final Stream<List<Map<String, dynamic>>> searchResultsWithRank = FuzzyBolt()
      .streamSearchWithRanks(
        dataset: dataset,
        query: queryStreamController.stream,
        strictThreshold: 0.6,
        typoThreshold: 0.5,
      );

  // Listen for search results
  final StreamSubscription<List<Map<String, dynamic>>>
  subscription = searchResultsWithRank.listen(
    (results) {
      print("šŸ”„ Stream Update:");
      for (var res in results) {
        print(
          "   šŸ”¹ ${res['value']} (Score: ${res['rank'].toStringAsFixed(3)})",
        );
      }
    },
    onError: (error) => print("āš ļø Stream Error: $error"),
    onDone: () => print("āœ… Stream Completed"),
  );
  // Simulate typing "a" -> "p" -> "l" -> "e" with a delay
  List<String> querySequence = ["b", "be", "ber", "berr", "berry"];

  for (var query in querySequence) {
    await Future.delayed(Duration(milliseconds: 800)); // Simulate typing delay
    print("\nāŒØļø Typing: '$query'");

    queryStreamController.add(query);
  }

  // Close the query stream after execution
  await Future.delayed(Duration(seconds: 1));
  await queryStreamController.close();
  await subscription.cancel();

  print("šŸ Stream-based search completed.");
}
6
likes
0
points
537
downloads

Publisher

verified publishervishwakarthik.in

Weekly Downloads

A high-performance fuzzy search algorithm in Dart, designed for intelligent auto-suggestions, typo tolerance, and fast string matching.

Repository (GitHub)
View/report issues

Topics

#fuzzy-search #search-ranking #text-processing #auto-suggestions #ranking

License

unknown (license)

Dependencies

collection

More

Packages that depend on fuzzy_bolt