Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8022289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:13:31+00:00 2026-06-04T22:13:31+00:00

Making a specific example: You have a list of every first name in the

  • 0

Making a specific example:

  • You have a list of every first name in the USA.
  • You want to autosuggest completions in a GUI.

The obvious thing is to do is use a radix tree to get a list of names for the given prefix. However, this doesn’t take into account the frequency information. So, instead of just having the top 5 results be the first lexical results I would like the most common 5 names:

e.g. For the prefix dan

 (5913, 'Daniel')
 (889, 'Danny')
 (820, 'Dana')
 (272, 'Dan')
 (60, 'Dane')

Is there a trie tree algorithm that I’ve missed? Of course the ideal implementation (if one exists) is in python in my mind.

UPDATE: Generally happy with what Paddy3113 has proposed, though I will say that it blows up completely when I feed it the 2.6GB file which is one of the files I’m reducing. Looking into the details the output gives some insight:

samz;Samzetta|Samzara|Samzie
samza;Samzara
samzar;Samzara
samzara;Samzara
samze;Samzetta
samzet;Samzetta
samzett;Samzetta
samzetta;Samzetta
samzi;Samzie
samzie;Samzie

# Format - PREFIX;"|".join(CHOICES).

We’ve got a few more days on the bounty side of things, so I’m still looking for the killer solution. Since it’s not just about the reduction but also about the lookup side of things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:13:33+00:00Added an answer on June 4, 2026 at 10:13 pm

    Yes, we can use a trie. The most frequent names for a trie node are either (1) the name at that trie node or (2) a most frequent name for a child of the trie node. Here’s some Python code to play with.

    from collections import defaultdict
    
    
    class trie:
        __slots__ = ('children', 'freq', 'name', 'top5')
    
        def __init__(self):
            self.children = defaultdict(trie)
            self.freq = 0
            self.name = None
            self.top5 = []
    
        def __getitem__(self, suffix):
            node = self
            for letter in suffix:
                node = node.children[letter]
            return node
    
        def computetop5(self):
            candidates = []
            for letter, child in self.children.items():
                child.computetop5()
                candidates.extend(child.top5)
            if self.name is not None:
                candidates.append((self.freq, self.name))
            candidates.sort(reverse=True)
            self.top5 = candidates[:5]
    
        def insert(self, freq, name):
            node = self[name]
            node.freq += freq
            node.name = name
    
    
    root = trie()
    with open('letter_s.txt') as f:
        for line in f:
            freq, name = line.split(None, 1)
            root.insert(int(freq.strip()), name.strip())
    root.computetop5()
    print(root['St'].top5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a very specific control panel sort of thing to organize information,
I'm making an Android soundboard. I currently have 12 pages for this specific category.
I have a specific problem with my calendar I'm making. Its difficult to explain
I'm making a website with specific posts that have tags created by the user.
First, I assume each structure-specific sequences would have different ways to remove an item:
I am trying to confine a background to a specific shape. Example: I have
I'm reviving this question, and making it more specific: Is there a .NET framework
I'm planning on making a self-moderated site where people ca share specific information on
im using some js to insert safari specific styles on a website im making.
I'm trying to make a specific control smaller, without making its content smaller.. If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.