Last active
December 26, 2015 06:39
-
-
Save Rembane/7109131 to your computer and use it in GitHub Desktop.
This is a tree which contains prefixes of names.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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