From 1bd23703b553db2d757457761ac2729d1c27eb6b Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sat, 1 Jun 2024 19:17:33 +0100 Subject: [PATCH 1/4] added train method --- AutoComplete_App/backend.py | 81 +++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 AutoComplete_App/backend.py diff --git a/AutoComplete_App/backend.py b/AutoComplete_App/backend.py new file mode 100644 index 00000000000..13b3fe3f6de --- /dev/null +++ b/AutoComplete_App/backend.py @@ -0,0 +1,81 @@ +import sqlite3 +# import test_data +# import ast +# import json + +class AutoComplete: + """ + It works by building a `WordMap` that stores words to word-follower-count + ---------------------------- + e.g. To train the following statement: + + It is not enough to just know how tools work and what they worth, + we have got to learn how to use them and to use them well. + And with all these new weapons in your arsenal, we would better + get those profits fired up + + we create the following: + { It: {is:1} + is: {not:1} + not: {enough:1} + enough: {to:1} + to: {just:1, learn:1, use:2} + just: {know:1} + . + . + profits: {fired:1} + fired: {up:1} + } + so the word completion for "to" will be "use". + For optimization, we use another store `WordPrediction` to save the + predictions for each word + """ + + def __init__(self): + """ + Returns - None + Input - None + ---------- + - Initialize database. we use sqlite3 + - Check if the tables exist, if not create them + - maintain a class level access to the database + connection object + """ + self.conn = sqlite3.connect("autocompleteDB.sqlite3", autocommit=True) + cur = self.conn.cursor() + res = cur.execute("SELECT name FROM sqlite_master WHERE name='WordMap'") + tables_exist = res.fetchone() + print(tables_exist) + + if not tables_exist: + self.conn.execute("CREATE TABLE WordMap(name TEXT, value TEXT)") + self.conn.execute('CREATE TABLE WordPrediction (name TEXT, value TEXT)') + cur.execute("INSERT INTO WordMap VALUES (?, ?)", ("wordsmap", "{}",)) + cur.execute("INSERT INTO WordPrediction VALUES (?, ?)", ("predictions", "{}",)) + + def train(self, sentence): + words_list = sentence.split(" ") + words_map = {} + for idx in range(len(words_list)-1): + curr_word, next_word = words_list[idx], words_list[idx+1] + if curr_word not in words_map: + words_map[curr_word] = {} + if next_word not in words_map[curr_word]: + words_map[curr_word][next_word] = 1 + else: + words_map[curr_word][next_word] += 1 + + print(words_map) + + +if __name__ == "__main__": + input_ = "It is not enough to just know how tools work and what they worth,\ + we have got to learn how to use them and to use them well. And with\ + all these new weapons in your arsenal, we would better get those profits fired up" + ac = AutoComplete() + print(ac.train(input_)) + # se.index_document("we should all strive to be happy and happy again") + # print(se.index_document("happiness is all you need")) + # se.index_document("no way should we be sad") + # se.index_document("a cheerful heart is a happy one even in Nigeria") + # print(se.find_documents("happy")) \ No newline at end of file From e91a13a5a8e3fcca7639b8096a87c4f1cd686ae9 Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sun, 2 Jun 2024 16:55:36 +0100 Subject: [PATCH 2/4] completed train method with comments --- AutoComplete_App/backend.py | 51 +++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/AutoComplete_App/backend.py b/AutoComplete_App/backend.py index 13b3fe3f6de..66dc9bc79c9 100644 --- a/AutoComplete_App/backend.py +++ b/AutoComplete_App/backend.py @@ -1,7 +1,7 @@ import sqlite3 # import test_data # import ast -# import json +import json class AutoComplete: """ @@ -45,7 +45,6 @@ def __init__(self): cur = self.conn.cursor() res = cur.execute("SELECT name FROM sqlite_master WHERE name='WordMap'") tables_exist = res.fetchone() - print(tables_exist) if not tables_exist: self.conn.execute("CREATE TABLE WordMap(name TEXT, value TEXT)") @@ -54,8 +53,27 @@ def __init__(self): cur.execute("INSERT INTO WordPrediction VALUES (?, ?)", ("predictions", "{}",)) def train(self, sentence): + """ + Returns - string + Input - str: a string of words called sentence + ---------- + Trains the sentence. It does this by creating a map of + current words to next words and their counts for each + time the next word appears after the current word + - takes in the sentence and splits it into a list of words + - retrieves the word map and predictions map + - creates the word map and predictions map together + - saves word map and predictions map to the database + """ + cur = self.conn.cursor() words_list = sentence.split(" ") - words_map = {} + + words_map = cur.execute("SELECT value FROM WordMap WHERE name='wordsmap'").fetchone()[0] + words_map = json.loads(words_map) + + predictions = cur.execute("SELECT value FROM WordPrediction WHERE name='predictions'").fetchone()[0] + predictions = json.loads(predictions) + for idx in range(len(words_list)-1): curr_word, next_word = words_list[idx], words_list[idx+1] if curr_word not in words_map: @@ -65,7 +83,24 @@ def train(self, sentence): else: words_map[curr_word][next_word] += 1 - print(words_map) + # checking the completion word against the next word + if curr_word not in predictions: + predictions[curr_word] = { + 'completion_word': next_word, + 'completion_count': 1 + } + else: + if words_map[curr_word][next_word] > predictions[curr_word]['completion_count']: + predictions[curr_word]['completion_word'] = next_word + predictions[curr_word]['completion_count'] = words_map[curr_word][next_word] + + words_map = json.dumps(words_map) + predictions = json.dumps(predictions) + + cur.execute("UPDATE WordMap SET value = (?) WHERE name='wordsmap'", (words_map,)) + cur.execute("UPDATE WordPrediction SET value = (?) WHERE name='predictions'", (predictions,)) + return("training complete") + if __name__ == "__main__": @@ -73,9 +108,5 @@ def train(self, sentence): we have got to learn how to use them and to use them well. And with\ all these new weapons in your arsenal, we would better get those profits fired up" ac = AutoComplete() - print(ac.train(input_)) - # se.index_document("we should all strive to be happy and happy again") - # print(se.index_document("happiness is all you need")) - # se.index_document("no way should we be sad") - # se.index_document("a cheerful heart is a happy one even in Nigeria") - # print(se.find_documents("happy")) \ No newline at end of file + ac.train(input_) + # print(ac.predict("to")) \ No newline at end of file From c55bc6af5164c1c65c843c565f1b07f299bb0810 Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sun, 2 Jun 2024 16:57:39 +0100 Subject: [PATCH 3/4] added predict method and comments --- AutoComplete_App/backend.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/AutoComplete_App/backend.py b/AutoComplete_App/backend.py index 66dc9bc79c9..47e1c7906d6 100644 --- a/AutoComplete_App/backend.py +++ b/AutoComplete_App/backend.py @@ -1,6 +1,4 @@ import sqlite3 -# import test_data -# import ast import json class AutoComplete: @@ -101,6 +99,22 @@ def train(self, sentence): cur.execute("UPDATE WordPrediction SET value = (?) WHERE name='predictions'", (predictions,)) return("training complete") + def predict(self, word): + """ + Returns - string + Input - string + ---------- + Returns the completion word of the input word + - takes in a word + - retrieves the predictions map + - returns the completion word of the input word + """ + cur = self.conn.cursor() + predictions = cur.execute("SELECT value FROM WordPrediction WHERE name='predictions'").fetchone()[0] + predictions = json.loads(predictions) + completion_word = predictions[word.lower()]['completion_word'] + return completion_word + if __name__ == "__main__": @@ -109,4 +123,4 @@ def train(self, sentence): all these new weapons in your arsenal, we would better get those profits fired up" ac = AutoComplete() ac.train(input_) - # print(ac.predict("to")) \ No newline at end of file + print(ac.predict("to")) \ No newline at end of file From f6b0c094ca727ed5d84c0ee5296484cdc6213ad4 Mon Sep 17 00:00:00 2001 From: Xceptions Date: Sun, 2 Jun 2024 17:06:41 +0100 Subject: [PATCH 4/4] added GUI for autocomplete --- AutoComplete_App/frontend.py | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 AutoComplete_App/frontend.py diff --git a/AutoComplete_App/frontend.py b/AutoComplete_App/frontend.py new file mode 100644 index 00000000000..90e576e849e --- /dev/null +++ b/AutoComplete_App/frontend.py @@ -0,0 +1,37 @@ +from tkinter import * +from tkinter import messagebox +import backend + + +def train(): + sentence = train_entry.get() + ac = backend.AutoComplete() + ac.train(sentence) + +def predict_word(): + word = predict_word_entry.get() + ac = backend.AutoComplete() + print(ac.predict(word)) + +if __name__ == "__main__": + root = Tk() + root.title("Input note") + root.geometry('300x300') + + train_label = Label(root, text="Train") + train_label.pack() + train_entry = Entry(root) + train_entry.pack() + + train_button = Button(root, text="train", command=train) + train_button.pack() + + predict_word_label = Label(root, text="Input term to predict") + predict_word_label.pack() + predict_word_entry = Entry(root) + predict_word_entry.pack() + + predict_button = Button(root, text="predict", command=predict_word) + predict_button.pack() + + root.mainloop() \ No newline at end of file