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
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
The autocompletion code
In the following code:
categoryis 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.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: