Is there a Python library for custom autocompletion?











up vote
2
down vote

favorite












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









share|improve this question
























  • possible duplicate of How to make a python, command-line program autocomplete arbitrary things NOT interpreter
    – S.Lott
    Jul 27 '11 at 12:46






  • 1




    @S.Lott - The question you are flagging as "exact duplicate" - while related - it's not a duplicate. That question only involves completing strings based on a list of other strings. My question is about defining grammar rules and then doing autocompletion based on that. :)
    – mac
    Jul 28 '11 at 0:26










  • Please update your question to make it obvious how your question is different from the others which seem identical. You might want to search for all the other Python autocompleter questions and identify specific differences from all the prior questions.
    – S.Lott
    Jul 28 '11 at 1:32















up vote
2
down vote

favorite












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









share|improve this question
























  • possible duplicate of How to make a python, command-line program autocomplete arbitrary things NOT interpreter
    – S.Lott
    Jul 27 '11 at 12:46






  • 1




    @S.Lott - The question you are flagging as "exact duplicate" - while related - it's not a duplicate. That question only involves completing strings based on a list of other strings. My question is about defining grammar rules and then doing autocompletion based on that. :)
    – mac
    Jul 28 '11 at 0:26










  • Please update your question to make it obvious how your question is different from the others which seem identical. You might want to search for all the other Python autocompleter questions and identify specific differences from all the prior questions.
    – S.Lott
    Jul 28 '11 at 1:32













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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









share|improve this question















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






python parsing user-interface autocomplete grammar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 20:05









Peter Mortensen

13.3k1983111




13.3k1983111










asked Jul 27 '11 at 12:25









mac

28.4k16101120




28.4k16101120












  • possible duplicate of How to make a python, command-line program autocomplete arbitrary things NOT interpreter
    – S.Lott
    Jul 27 '11 at 12:46






  • 1




    @S.Lott - The question you are flagging as "exact duplicate" - while related - it's not a duplicate. That question only involves completing strings based on a list of other strings. My question is about defining grammar rules and then doing autocompletion based on that. :)
    – mac
    Jul 28 '11 at 0:26










  • Please update your question to make it obvious how your question is different from the others which seem identical. You might want to search for all the other Python autocompleter questions and identify specific differences from all the prior questions.
    – S.Lott
    Jul 28 '11 at 1:32


















  • possible duplicate of How to make a python, command-line program autocomplete arbitrary things NOT interpreter
    – S.Lott
    Jul 27 '11 at 12:46






  • 1




    @S.Lott - The question you are flagging as "exact duplicate" - while related - it's not a duplicate. That question only involves completing strings based on a list of other strings. My question is about defining grammar rules and then doing autocompletion based on that. :)
    – mac
    Jul 28 '11 at 0:26










  • Please update your question to make it obvious how your question is different from the others which seem identical. You might want to search for all the other Python autocompleter questions and identify specific differences from all the prior questions.
    – S.Lott
    Jul 28 '11 at 1:32
















possible duplicate of How to make a python, command-line program autocomplete arbitrary things NOT interpreter
– S.Lott
Jul 27 '11 at 12:46




possible duplicate of How to make a python, command-line program autocomplete arbitrary things NOT interpreter
– S.Lott
Jul 27 '11 at 12:46




1




1




@S.Lott - The question you are flagging as "exact duplicate" - while related - it's not a duplicate. That question only involves completing strings based on a list of other strings. My question is about defining grammar rules and then doing autocompletion based on that. :)
– mac
Jul 28 '11 at 0:26




@S.Lott - The question you are flagging as "exact duplicate" - while related - it's not a duplicate. That question only involves completing strings based on a list of other strings. My question is about defining grammar rules and then doing autocompletion based on that. :)
– mac
Jul 28 '11 at 0:26












Please update your question to make it obvious how your question is different from the others which seem identical. You might want to search for all the other Python autocompleter questions and identify specific differences from all the prior questions.
– S.Lott
Jul 28 '11 at 1:32




Please update your question to make it obvious how your question is different from the others which seem identical. You might want to search for all the other Python autocompleter questions and identify specific differences from all the prior questions.
– S.Lott
Jul 28 '11 at 1:32












2 Answers
2






active

oldest

votes

















up vote
2
down vote













One module for auto-completion I'm aware of is Qt's QCompleter, which you can use in Python via PyQt or PySide. I don't think it understands grammar in the way you mean, but it's generic enough to allow you writing code that does.






share|improve this answer




























    up vote
    2
    down vote



    accepted










    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)





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6844243%2fis-there-a-python-library-for-custom-autocompletion%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      One module for auto-completion I'm aware of is Qt's QCompleter, which you can use in Python via PyQt or PySide. I don't think it understands grammar in the way you mean, but it's generic enough to allow you writing code that does.






      share|improve this answer

























        up vote
        2
        down vote













        One module for auto-completion I'm aware of is Qt's QCompleter, which you can use in Python via PyQt or PySide. I don't think it understands grammar in the way you mean, but it's generic enough to allow you writing code that does.






        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          One module for auto-completion I'm aware of is Qt's QCompleter, which you can use in Python via PyQt or PySide. I don't think it understands grammar in the way you mean, but it's generic enough to allow you writing code that does.






          share|improve this answer












          One module for auto-completion I'm aware of is Qt's QCompleter, which you can use in Python via PyQt or PySide. I don't think it understands grammar in the way you mean, but it's generic enough to allow you writing code that does.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 27 '11 at 12:48









          Eli Bendersky

          162k66293366




          162k66293366
























              up vote
              2
              down vote



              accepted










              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)





              share|improve this answer



























                up vote
                2
                down vote



                accepted










                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)





                share|improve this answer

























                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  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)





                  share|improve this answer














                  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)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 5 '11 at 10:23

























                  answered Aug 5 '11 at 10:14









                  mac

                  28.4k16101120




                  28.4k16101120






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6844243%2fis-there-a-python-library-for-custom-autocompletion%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python