GithubHelp home page GithubHelp logo

subpath / rust-autocomplete-poc Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 881 KB

Fastest autocomplete in rust with O(1) complexity

License: Creative Commons Zero v1.0 Universal

Rust 100.00%
rust rust-lang autocomplete autocomplete-suggestions

rust-autocomplete-poc's Introduction

rust-autocomplete-poc

Fastest autocomplete in rust with O(1) complexity

this is a simple cli implementation to showcase autocomplete usage.

Backstory

On my daily job I'm working with autocomplete and I'm using WFST provided by Lucene.

WFSTs are awesome! They are fast and provide really impressive compression! But one day I was thinking how can I make autocomplete even faster?

And I've had this dumb idea: What is the fastest thing in the universe?

Correct! it's a .get method from the HashMap! and it also has O(1) complexity so it should scale really well!

- But how to implement prefix-lookup in the HashMap?

you might ask.

So here is the dumb part, you don't need to do it, you can prepare keys that will already contain all possible prefixes, duh.

Let's follow my example here To start with autocomplete you usually have some weighted string, weight is determine how relevant the query is.

My weighted strings:

pizza   4
pie     2

So in my autocomplete vocabulary there are just 2 words, pizza and pie and my weighted strings are already sorted.

My HashMap:

{
    "p": ["pizza", "pie"],
    "pi": ["pizza", "pie"],
    "piz": ["pizza"],
    "pizz": ["pizza"],
    "pizza": ["pizza"],
    "pie": ["pie"]
}

You see that it's not quite your normal HashMap since it contains multiple values for a given key. In python you can modify Python's dict like this:

class Dictlist(dict):
    def __setitem__(self, key, value):
        try:
            self[key]
        except KeyError:
            super(Dictlist, self).__setitem__(key, [])
        self[key].append(value)

In Rust I used multimap crate.

Running this code:

  1. clone it
  2. run it with cargo run or build it and run from binary gif

Autocomplete API:

You can find my quick api implementation of the same autocomplete here.

rust-autocomplete-poc's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.