itrie 0.0.2 copy "itrie: ^0.0.2" to clipboard
itrie: ^0.0.2 copied to clipboard

Efficient, immutable and stack safe implementation of a Trie data structure in dart: autocomplete, text search, spell checking, strings and prefixes.

example/itrie_example.dart

import 'dart:convert';
import 'dart:io';

import 'package:itrie/itrie.dart';

void main() async {
  /// Source: https://github.com/dwyl/english-words/blob/master/words.txt
  final file = File('./example/words.txt');
  Stream<String> lines = file.openRead().transform(utf8.decoder).transform(
        LineSplitter(),
      );

  try {
    final sourceList = await lines.toList();
    final selection = sourceList
        .where(
          (key) => key.length >= 2 && RegExp(r'^[A-Za-z]+$').hasMatch(key),
        )
        .map((e) => e.toLowerCase());

    await File('./example/selection.txt')
        .writeAsString(selection.toList().join("\n"));

    final itrie = ITrie<int>.fromIterable(
      selection
          .map(
            (key) => (key.toLowerCase(), key.length),
          )
          .toList(),
    );

    print("Loaded ${itrie.length} words");
    while (true) {
      print("Search a word");
      final line = stdin.readLineSync(encoding: utf8);
      if (line != null) {
        if (line == "q") {
          break;
        }

        print(itrie.get(line));
      }
    }
  } catch (e) {
    print('Error: $e');
  }
}
7
likes
160
points
48
downloads

Publisher

verified publishersandromaglione.com

Weekly Downloads

Efficient, immutable and stack safe implementation of a Trie data structure in dart: autocomplete, text search, spell checking, strings and prefixes.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on itrie