Everyone in today's smartphone-saturated world has had their share of interactions with textual "autocomplete." But how would you actually make an autocomplete system?
In this project, CompleteMe, we will create a simple textual autocomplete system.
A common way to solve this problem is using a data structure called a Trie. A Trie is somewhat similar to binary trees, but whereas each node in a binary tree points to up to 2 subtrees, nodes within our retrieval tries will point to N
subtrees, where N
is the size of the alphabet we want to complete within.
Thus for a simple latin-alphabet text trie, each node will potentially have 26 children, one for each character that could potentially follow the text entered thus far.
To complete the project, you will need to build an autocomplete system which provides the following features:
- Insert a single word to the autocomplete dictionary
- Count the number of words in the dictionary
- Populate a newline-separated list of words into the dictionary
- Suggest completions for a substring
- Mark a selection for a substring
- Weight subsequent suggestions based on previous selections
- Clone the repo
$ git clone git@github.com:bethsebian/trie_hard.git autocomplete_me
- Navigate to autocomplete_me directory
$ cd autocomplete_me
. - Fire up a pry session
$ pry
from the autocomplete_me directory - Require the complete_me.rb file
$ require "./lib/complete_me.rb"
- Set a variable pointing to a new instance of the CompleteMe class
$ completion = CompleteMe.new
- Add words to your dictionary:
- Add words word-by-word to the trie with
$ completion.insert("pizza")
OR - Add the entire Mac dictionary with
$ dictionary = File.read("/usr/share/dict/words")
and then$ completion.populate(dictionary)
. - Confirm words have been added with
$ completion.count
- Retrieve autocomplete suggestions with the
suggest
method:$ completion.suggest("piz")
will return["pizza", "pizzeria", "pizzicato"]
- My first complex logic application, completed my third week of Turing/programming (!).
- Usage Frequency Weighting
- The application is able to weigh suggestions based on whether they've been selected by users in the past.
- User can
select
a suggestion with the commandcompletion.select("piz", "pizzeria")
. - The next time
suggest
is called, the suggestions will be reordered to illustrate the weighting of "pizzeria" ahead of "pizza".
- The node.rb file could be refactored and cleaned up a big, maybe extracting some methods to a third class.
- I created an user interface using shoes that needs some work still.
To run Shoes:
- Download Shoes for Ruby from http://shoesrb.com/.
- Follow instructions above for "Interacting with the Trie" and populate the trie with the Mac dictionary.
- Run Shoes.
- Click on "Run an App" in Shoes.
- Navigate to the shoes.rb file in this repo.
- I'd like a cleaner way of presenting results from the user's search.
- The dropdown boxes that show results need to be removed between each search.