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 6211559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:21:38+00:00 2026-05-24T06:21:38+00:00

Is there a generic library that would allow me to have autocompletion according to

  • 0

Is there a generic library that would allow me to have autocompletion according to a custom grammar and item list?

Here’s an example of what I am looking for.

Grammar:

  • You can munch apples and mangoes
  • You can drink milk and water
  • You can move everything
  • Sentence structure: verb [+ adjective] + object

Items:

  • 1 green apple
  • 1 microscopic apple
  • 1 green mango
  • 1 yellow mango
  • 1 mango [no color given]
  • 1 milk
  • 1 water

Expected behaviour (user’s input on the first line, suggestions on the second)

m
move, munch

mo
move

move g
move green apple, move green mango

move y
move yellow mango

move m
move milk, move mango, move microscopic apple
  • 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-05-24T06:21:39+00:00Added an answer on May 24, 2026 at 6:21 am

    I finally found an acceptable solution by using a combination of SPARK (for grammar parsing / syntactic analysis) and my own code for autocompletion.

    About SPARK

    SPARK stands for the Scanning, Parsing, and Rewriting Kit. It formerly
    had no name, and was referred to as the “little language framework.”
    The first version (circa 1998) was described in the paper Compiling
    Little Languages in Python
    at the 7th International Python Conference.

    SPARK is written in 100% pure Python, and is made available as open
    source.

    The autocompletion code

    In the following code:

    • category is the kind of word we are autocompleting. This is obtained by parsing the current command line. For example: if the user is typing “drink m”, the parser will know to expect a word in the category “liquids” defined in the grammar.
    • The user input is stored in a list (self.chars)
    • _get_list_of_existing() returns a list of existing words in a given category
    • _get_common_beginning() return – if available – the longest initial supersequence for multiple matches. For example if the user input is writing “ma” and possible autocompletion words are [magnolia, magnifying glass] the _get_common_beginning() will return “magn”.

    Here’s the relevant code snippets:

    def autocomplete(self, category):
        '''
        If possible, autocomplete a word according to its category.
        '''
        root = ''.join(self.chars).split()[-1]  #The bit after the last space
        pool = self._get_list_of_existing(category)
        matches = [i for i in pool if i.find(root) == 0]
        if len(matches) == 1:
            match = matches[0]+' '
        elif len(matches) > 1:
            match = self._get_common_beginning(matches)
        else:
            return
        self.chars.extend(list(match[len(root):]))
    
    def _get_common_beginning(self, strings):
        '''
        Return the strings that is common to the beginning of each string in
        the strings list.
        '''
        result = []
        limit = min([len(s) for s in strings])
        for i in range(limit):
            chs = set([s[i] for s in strings])
            if len(chs) == 1:
                result.append(chs.pop())
            else:
                break
        return ''.join(result)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a library of generic collection algorithms for .NET? I would like to
Is there a library that implements lock-free algorithms(queue, linked list and others) written in
Is there any feasible way of using generics to create a Math library that
Is there a generic way to clone objects in VBA? So that i could
Is there a generic TimeZoneInfo for Central Europe that takes into consideration both CET
Are there any more generic tools that can compile or basically merge multiple PHP
I want to write a generic (C/C++) library that I will use to develop
Background: I have written a generic logging library in .NET 3.5. Basically the developer
Given a generic List I would need some kind of index (in the database
I have a control library that consists of only UserControl derived controls. I am

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.