Code Monkey home page Code Monkey logo

autotrie's Introduction

AutoTrie

A versatile library which solves autocompletion in Dart/Flutter. It is based around a space-efficient implementation of Trie which uses variable-length lists. With this, serving auto-suggestions is both fast and no-hassle. Suggestions are also sorted by how often they've been entered and subsorted by recency of entry, for search-engine-like results.

Read more about Trie here.

A Brief Note

It takes time, effort, and mental power to keep this package updated, useful, and improving. If you used or are using the package, I'd appreciate it if you could spare a few dollars to help me continue development.

PayPal

Usage

A usage example is provided below. Check the API Reference for detailed docs:

import 'package:autotrie/autotrie.dart';

void main() {
  // You are allowed to initialize with a starting databank by passing a `bank` parameter.
  var engine = AutoComplete(engine: SortEngine.configMulti(Duration(seconds: 1), 15, 0.5, 0.5));

  engine.enter('more'); // Enter more thrice.
  engine.enter('more');
  engine.enter('more');

  engine.enter('moody'); // Enter moody twice.
  engine.enter('moody');

  engine.enter('morose'); // Enter scattered words (with mo).
  engine.enter('morty');
  engine.enter('moment');
  engine.enter('momentum');

  engine.enter('sorose'); // Enter scattered words (without mo).
  engine.enter('sorty');

  engine.delete('morose'); // Delete morose.

  // Check if morose is deleted.
  print('Morose deletion check: ${!engine.contains('morose')}');

  // Check if engine is empty.
  print('Engine emptiness check: ${engine.isEmpty}');
 
  // They've been ranked by frequency and recency. Since they're all so similar
  // in recency, frequency takes priority.
  print("'mo' suggestions: ${engine.suggest('mo')}");
  // Result: [more, moody, momentum, moment, morty]

  // Get all entries.
  // They've *not* been sorted.
  // Use `engine.suggest('')` to get all entries sorted` 
  print('All entries: ${engine.allEntries}');
  // Result: [more, moody, sorty, sorose, momentum, moment, morty]
}

Sorting

The AutoComplete constructor takes a SortEngine, which it uses to sort the result of the autocompletion operation. There are a few different modes it can operate in:

  • SortEngine.entriesOnly() -> AutoComplete results are only sorted by number of entries in the engine (High to Low)
  • SortEngine.msOnly() -> AutoComplete results are only sorted by how much time has passed since their last entry (Low to High)
  • SortEngine.simpleMulti() ->
    • Sorted using two logistic curves, one for ms and one for entries
      • The ms curve is set to use 3 years (a LOT) as the upper end of how far back entries could have been entered
      • The entries curve is set to use 30 entries as the max amount of entries
      • These values are highly arbitrary and not likely to fit your project; this mode is not recommended unless you are just playing around.
    • Takes two weights (one for recency and one for entries) which can be used to balance how heavily each factor should affect the final sorting.
  • SortEngine.configMulti() ->
    • Sorted using two logistic curves, one for ms and one for entries
      • The ms curve is balanced using a parameter (a Duration) for the max time since entry in this engine.
      • The entries curve is balanced using a parameter (an int) for the max amount of entries in this engine.
      • If you know approximately how old and how big this AutoComplete engine is, it is highly recommended that you use this mode.
    • Takes two weights (one for recency and one for entries) which can be used to balance how heavily each factor should affect the final sorting.

Basic File Persistence

AutoComplete is natively capable of writing itself to and reading itself from a file. To do this, persist to a file using the persist method (it takes a File object): await engine.persist(myFile);

Then you can rebuild using AutoComplete.fromFile (it takes a File along with the mandatory SortEngine): var engine = AutoComplete.fromFile(file: myFile, engine: SortEngine.entriesOnly());

This persistence will preserve all the metadata (last insert, number of entries) in the table as well as the core data (the Strings themselves).

Hive Integration

  • Hive is a speedy, local, and key-value database for Dart/Flutter. Go check it out if you haven't already!
  • Hive integration is now available with autotrie:
    • Our way of integration uses extension methods.
    • Import Hive and AutoTrie, and create a normal Hive box using Hive.openBox('nameHere').
    • You can then call searchKeys(String prefix) and searchValues(String prefix) on that box to get auto-suggestions.
    • There is no sorting options: only entry-level sorting is available.

Features and bugs

Please file feature requests and bugs at the issue tracker.


This library and its contents are subject to the terms of the Mozilla Public License, v. 2.0.
© 2020 Aditya Kishore

autotrie's People

Contributors

akushwarrior avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

autotrie's Issues

Hook into hive

Autotrie looks really useful for indexing text in a document. I assuem this is correct.

I was wondering if you planing to add any persistence ? like using it with tailor.

I ask because we are building various flutter components and one if a Rich Text Document writer, and Searching through many of the docs to find text is one of our needs.

We use HIVE and will persist the documents in HIVE.


The other way is to just search the documents everytime and not maintain an index. Will get slower as more docs are added.

thanks for the amazing high quality libs btw !!!

Case-insensitive indexing

I need case-insensitive indexing, so that I can do case-insensitive search while getting results in their original case (all cases, if there are multiple). Any plans for this?

Issue when installing

Because autotrie >=0.3.1 depends on collection ^1.14.12 and every version of flutter_localizations from sdk depends on collection 1.14.11, autotrie >=0.3.1 is incompatible with flutter_localizations from sdk.

So, because . depends on both flutter_localizations any from sdk and autotrie ^0.3.1, version solving failed.
pub get failed (1; So, because . depends on both flutter_localizations any from sdk and autotrie ^0.3.1, version solving failed.)

Is autotrie 1.0.0+1 on dev-channel?

Hi,

I'm facing an issue when using the 1.0.0+1 version of autotrie, could it be that the package expects the dev-channel of flutter?

It works perfectly when removing the version 1.0.0+1, but then it installs version 0.4.1.

[testautotrie] flutter pub get
Running "flutter pub get" in testautotrie...                    
Because every version of flutter_test from sdk depends on meta 1.1.8 and testautotrie depends on meta ^1.2.2, flutter_test from sdk is forbidden.

So, because testautotrie depends on flutter_test any from sdk, version solving failed.
pub get failed (1; So, because testautotrie depends on flutter_test any from sdk, version solving failed.)
exit code 1

I'm using the latest version Flutter 1.20.2

Steps to recreate this:

mkdir testautotrie
cd testautotrie
flutter create .

Add autotrie 1.0.0+1 to the pubspec.yaml file

Best regards

Does persisting support CJK language characters?

I want to use this library to optimise searching through hundreds of thousands of dictionary entries for my language learning application and I think using a trie will allow me to perform this more efficiently.

I tested building a trie of 200K imported words and it took quite a while but it worked. I can't seem to persist the trie, however. When I import it back with fromFile, I get something similar to Invalid radix-10 number or so. My suspicion is that the encoding or the format of the file used for persistence doesn't quite like that I am including Japanese characters in my trie.

It would be great if I could use this library as it would allow me to support a broad range of custom dictionaries for my application. Cheers!

Decouple from Hive

Hive integration should be extracted to a separate package like autotrie_hive so as to not bring the Hive dependency to user packages that do not need it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.