This Python script provides a simple implementation of an autocompletion feature using the Natural Language Toolkit (NLTK) corpus of English words. When provided with a query string, it searches through the corpus to find and return words that could potentially complete the given query.
The script relies on the NLTK library. Before running the script, ensure you have installed NLTK and downloaded the 'words' corpus by executing the following commands:
import nltk
nltk.download('words')
Checks if all characters in the query exist in the given word. Returns True
if the condition is met, False
otherwise.
Checks if the characters in the query appear in the exact sequence in the given word. Returns True
if the condition is met, False
otherwise.
Checks if the characters in the query appear sequentially (though not necessarily in a strict sequence) in the given word. Returns a list of two boolean values: the first indicating if the query is sequential, and the second indicating if the query is strict.
Checks if the given word starts with the characters in the query. Returns True
if the condition is met, False
otherwise.
The main function that utilizes the above helper functions to find and return a list of words from the NLTK corpus that could potentially complete the given query.
To use the script, simply provide a query string to the autoComplete
function and it will return a list of potential completions along with the total count of such words:
QUERY = str(input("Enter Text to autocomplete: "))
results = autoComplete(QUERY)
for i in results:
print(i)
print(f"There are {len(results)} word(s) with {QUERY} in them")
Input: aut
Output:
auto
autobahn
autobiographical
...
There are 3 word(s) with aut in them
This script provides a simple way to implement an autocompletion feature using Python and the NLTK library.