Skip to content

Instantly share code, notes, and snippets.

@Rembane
Last active December 26, 2015 06:39
Show Gist options
  • Save Rembane/7109131 to your computer and use it in GitHub Desktop.
Save Rembane/7109131 to your computer and use it in GitHub Desktop.
This is a tree which contains prefixes of names.
# Lets build a dictionary of people, like this:
names = [u'{} {}'.format(f.lower(),e.lower()) for (f,e) in Participant.objects.values_list('fname', 'ename').order_by('fname', 'ename')]
autocompletion_tree = defaultdict(list)
for (i,n) in enumerate(names):
s = ''
for c in n:
s += c
autocompletion_tree[s].append(i)
# Purge it!
# Make it smaller!
for n in names:
for x in range(2, len(n)+1):
if len(autocompletion_tree[n[:x]]) < 2:
del autocompletion_tree[n[:x]]
autocompletion_json = json.dumps(autocompletion_tree)
autocompletion_names = json.dumps(names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment