Giter Club home page Giter Club logo

jasuggest's Introduction

jasuggest

A simple autosuggest library based on a Trie implementation.

Available in jcenter().

Maven

<dependency>
  <groupId>net.andreinc.jasuggest</groupId>
  <artifactId>jasuggest</artifactId>
  <version>0.0.1</version>
  <type>pom</type>
</dependency>

Gradle

compile 'net.andreinc.jasuggest:jasuggest:0.0.1'

Simple Example

String[] words = { "us", "usa", "use", "useful", "useless", "user", "usurper" };

// All the searches will be cached in a ConcurrentHashMap with a maximum size of 512 elements.
// Check: https://github.com/jhalterman/expiringmap
JaCacheConfig jaCacheConfig =
                JaCacheConfig.builder()
                             .maxSize(512)
                             .build();

JaSuggest jaSuggest = JaSuggest.builder()
                               .ignoreCase()
                               .withCache(jaCacheConfig)
                               .buildFrom(words);
                               
List<String> result = jaSuggest.findSuggestions("use");

// [useful, useless, user]

The above example creates internally creates a Trie based on the supplied words. In memory the Trie looks like:

Image of the trie

Notes:

  • Each blue node has an additional property that marks the existence of an word. So when the findSuggestions() method is called a tree traversal is performed in order to determine all the possible outcomes. When a "blue node" is visited the result is added to the map and the traversing operation continues;

  • The library supports caching the results.

Simple Example - Increasing performance at the cost of memory consumption

The builder() method prebuiltWords() adds more information to the Trie. Basically each "blue node" that marks the existence of a word also contains the same word as a property:

Image of the trie

So the only change you need to make is when you create the JaSuggest object:

JaSuggest jaSuggest = JaSuggest.builder()
                               .ignoreCase()
                               .prebuiltWords() // !HERE!
                               .withCache(jaCacheConfig)
                               .buildFrom(words);

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.