https://github.com/indiesquidge/autocomplete
low-level, primitive autocomplete using trie data structure
https://github.com/indiesquidge/autocomplete
autocomplete javascript trie-structure trie-tree-autocomplete
Last synced: 12 months ago
JSON representation
low-level, primitive autocomplete using trie data structure
- Host: GitHub
- URL: https://github.com/indiesquidge/autocomplete
- Owner: indiesquidge
- License: mit
- Created: 2018-05-28T17:53:16.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-25T04:47:19.000Z (about 8 years ago)
- Last Synced: 2025-04-14T02:32:45.562Z (over 1 year ago)
- Topics: autocomplete, javascript, trie-structure, trie-tree-autocomplete
- Language: JavaScript
- Size: 29.3 KB
- Stars: 5
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Autocomplete
A low-level, primitive autocomplete using trie data structure
## Features
This autocomplete library has the ability to insert words manually, get word
suggestions based on a query, populate the data structure with a dictionary
array of words, and delete undesired words.
_NOTE: The library is not populated with any words by default._
### Insertion
```javascript
const completion = createTrie();
completion.insert("hello");
completion.count(); // => 1
completion.insert("world");
completion.count(); // => 2
```
### Suggestions
```javascript
completion.suggest("he"); // => ['hello']
completion.insert("hellen");
completion.suggest("he"); // => ["hello", "hellen"]
completion.suggest("w"); // => ["world"]
```
### Population
```javascript
const fs = require("fs");
const text = "/usr/share/dict/words";
const dictionary = fs.readFileSync(text).toString().trim().split("\n");
const completion = createTrie();
completion.populate(dictionary);
completion.suggest("world"); // => [ 'world', 'worlded', 'worldful', …]
```
### Deletion
```javascript
completion.suggest("world"); // => ['world', 'worlded', 'worldful', …]
completion.delete("worlded");
completion.suggest("world"); // => ['world', 'worldful', …]
```
## Future Improvements
- [ ] case-insensitive suggestions
- it'd be nice to be able to type in "ap" and have something like "Apache"
come up as a suggestion; since keys are strings in JS and strings are
case-sensitive, this would require a bit of refactoring around how we
discover the `startNode` in `Trie#suggest`
- [ ] substring suggestions
- right now, words are only displayed as suggestions if the word is prefixed
by the query; a better implementation would include suggestions that have
the query as a substring appearing anywhere in the word, not just as a
prefix; e.g., querying in "ist" would return suggestions like "istle" and
"isthmus", but also "listen"
---
_Inspired by the ["Compelete-Me"][1] project from the Turing School of Software &
Design_
[1]: http://frontend.turing.io/projects/complete-me.html