lafzi_dart 0.1.2 copy "lafzi_dart: ^0.1.2" to clipboard
lafzi_dart: ^0.1.2 copied to clipboard

A phonetic search engine for Quranic text, ported from lafzi.js.

Lafzi Dart #

Lafzi Dart is a Dart library for searching Quranic verses using fuzzy matching and highlighting. It supports searching with various modes and provides verse details including translation and highlight positions.

This project is a Dart port of the original JavaScript project: lafzi.js.

Credit to skipness/lzstring-dart for the Dart implementation of the lz-string compression algorithm used in this project.

Uncompressed data can be found in the original project's repository: lafzi.js/.uncompressed_data.

Features #

  • Fuzzy search for Quranic verses
  • Highlight matched text
  • Returns verse details: surah name, ayat number, text, translation, score, and highlight positions
  • No Flutter dependency; uses Dart IO for file access

Installation #

Add to your pubspec.yaml dependencies:

dependencies:
  lafzi_dart: <version>

Usage #

Example usage in a Dart console app:

import 'package:lafzi_dart/lafzi_dart.dart';
import 'package:lafzi_dart/src/models.dart';

void main() async {
  final lafziSearch = LafziSearch();

  try {
    final List<QuranVerse> result = await lafziSearch.searchLafzi(
      mode: "v",
      threshold: 0.95,
      isHilight: true,
      query: "sibgo",
      multipleHighlightPos: false,
    );

    if (result.isNotEmpty) {
      print("Search Results for 'sibgo':");
      for (final verse in result) {
        print('Surah: ${verse.name}, Ayat: ${verse.ayat}');
        print('Text: ${verse.text}');
        print('Translation: ${verse.trans}');
        if (verse.textHilight != null) {
          print('Highlighted Text: ${verse.textHilight}');
        }
        print('Score: ${verse.score?.toStringAsFixed(4)}');
        print('Highlight Positions: ${verse.highlightPos}');
      }
    } else {
      print("No results found for 'sibgo'.");
    }
  } catch (e) {
    print("Error during search: $e");
  }
}

Custom File Loading #

The LafziSearch class requires an implementation of LafziFileLoader to load the necessary data files. This allows flexibility for different environments (e.g., pure Dart applications or Flutter applications).

  • DartFileLoader: For pure Dart applications (like console apps), you can use DartFileLoader which utilizes dart:io to read files from the file system.

    import 'dart:io';
    import 'dart:typed_data';
    import 'package:lafzi_dart/src/loaddata.dart';
    
    class DartFileLoader implements LafziFileLoader {
      @override
      Future<Uint8List> load(String path) {
        final file = File(path);
        return file.readAsBytes();
      }
    }
    
    // Initialize LafziSearch with DartFileLoader
    final lafziSearch = LafziSearch(lafziLoader: DartFileLoader());
    
  • FlutterFileLoader: For Flutter applications, you would typically load assets using rootBundle. An example FlutterFileLoader implementation would look like this (you'll need to uncomment and adapt this in your Flutter project):

import 'package:flutter/services.dart' show rootBundle; Add this import
import 'dart:typed_data';
import 'package:lafzi_dart/src/loaddata.dart';

class FlutterFileLoader implements LafziFileLoader {
  @override
  Future<Uint8List> load(String path) async {
    /* Make sure to add your data files to your pubspec.yaml under assets:
    assets:
      - assets/data/ 
    */
    final ByteData data = await rootBundle.load('assets/data/$path');
    return data.buffer.asUint8List();
  }
}

Initialize LafziSearch with FlutterFileLoader
final lafziSearch = LafziSearch(lafziLoader: FlutterFileLoader());

Remember to copy all data files (from the data/ directory) to your Flutter project's assets/data/ folder and declare them in your pubspec.yaml under the assets section.

Data Files #

Make sure the required data files are present in the data/ directory:

  • muqathaat.lz
  • index_v.lz
  • index_nv.lz
  • posmap_v.lz
  • posmap_nv.lz
  • quran_teks.lz
  • quran_trans_indonesian.lz

How It Works #

  • The library loads compressed data files using Dart IO.
  • It decompresses and parses the data for fast search and retrieval.
  • The searchLafzi method performs fuzzy matching and returns a list of matching verses.

License #

MIT License

0
likes
0
points
14
downloads

Publisher

verified publisheragusibrah.im

Weekly Downloads

A phonetic search engine for Quranic text, ported from lafzi.js.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on lafzi_dart