This repository contains a command-line predictive text engine written in C. It suggests word completions based on a user-entered prefix. The application uses a hash table to efficiently store and retrieve a dictionary of words and their usage frequencies.
- Prefix-Based Suggestions: Provides real-time word suggestions as you type.
- Frequency Tracking: Learns from user input by incrementing the frequency of selected words.
- Persistent Memory: Saves word frequencies to a file, allowing learned behavior to persist across sessions.
- Dynamic Dictionary: Automatically adds new, unrecognized words to its dictionary.
- Efficient Data Structure: Implemented with a hash table using a polynomial rolling hash and separate chaining to handle collisions, ensuring fast lookups.
The program's core logic is built around a hash table data structure:
- Initialization: A hash table is created when the program starts.
- Data Loading:
- It first populates the hash table with an initial set of words from
predictive_text/data/dictionary.txt. - Next, it loads
predictive_text/data/frequency.txtto update the usage counts for words based on previous sessions.
- It first populates the hash table with an initial set of words from
- User Interaction Loop:
- The user is prompted to enter a text prefix.
- The program searches the entire hash table for all words that start with the entered prefix.
- The matching words (suggestions) are displayed along with their current frequency count.
- If the exact word typed by the user is one of the suggestions, its frequency is incremented.
- If no suggestions are found for the prefix, the prefix is treated as a new word, added to the hash table, and its frequency is initialized.
- Saving State: When the user enters
exit, the program writes the updated word frequencies from the hash table back intopredictive_text/data/frequency.txtbefore terminating.
.
└── predictive_text/
├── data/
│ ├── dictionary.txt # Base dictionary of words.
│ └── frequency.txt # Stores words and their usage frequencies.
└── src/
├── main.c # Main application entry point and user interaction loop.
├── hashtable.c # Implementation of the hash table data structure.
├── hashtable.h # Header file for the hash table.
├── io.c # File I/O functions for loading/saving data.
├── io.h # Header file for I/O functions.
├── utils.c # Utility functions (e.g., string manipulation).
└── utils.h # Header file for utility functions.
- A C compiler, such as GCC.
- Clone the repository and navigate to the source directory:
git clone https://github.com/drv4ever/auto_complete-.git cd auto_complete-/predictive_text/src - Compile the source files using GCC:
gcc -o autocomplete main.c hashtable.c io.c utils.c
- From the
srcdirectory, run the executable. The program is configured to find its data files in the parentdatadirectory../autocomplete
- The program will load the dictionary and frequency data, and you can begin typing.
- Enter a prefix and press Enter to see suggestions.
the Dictionary and frequency data loaded enter a prefix (or|exit|to quit)hel Suggestions for 'hel': helicopter (freq:->) 1 hell (freq:->) 1 help (freq:->) 1 hello (freq:->) 4 - To close the application, type
exitand press Enter. This will save the session's word frequencies before quitting.