Skip to content

nomemory/jasuggest

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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);

About

A simple autosuggest library based on a Java Trie implementation.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages