https://github.com/nomemory/jasuggest
A simple autosuggest library based on a Java Trie implementation.
https://github.com/nomemory/jasuggest
Last synced: about 1 year ago
JSON representation
A simple autosuggest library based on a Java Trie implementation.
- Host: GitHub
- URL: https://github.com/nomemory/jasuggest
- Owner: nomemory
- License: other
- Created: 2017-06-30T08:28:02.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-12T13:53:02.000Z (about 9 years ago)
- Last Synced: 2025-06-04T02:45:47.356Z (about 1 year ago)
- Language: Java
- Size: 418 KB
- Stars: 10
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
## jasuggest
A simple autosuggest library based on a [Trie](https://en.wikipedia.org/wiki/Trie) implementation.
Available in [jcenter()](https://bintray.com/nomemory/maven/jasuggest).
**Maven**
```
net.andreinc.jasuggest
jasuggest
0.0.1
pom
```
**Gradle**
```
compile 'net.andreinc.jasuggest:jasuggest:0.0.1'
```
## Simple Example
```java
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 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:

**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:

So the only change you need to make is when you create the `JaSuggest` object:
```java
JaSuggest jaSuggest = JaSuggest.builder()
.ignoreCase()
.prebuiltWords() // !HERE!
.withCache(jaCacheConfig)
.buildFrom(words);
```