Skip to content

Commit e91a13a

Browse files
committed
completed train method with comments
1 parent 1bd2370 commit e91a13a

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

AutoComplete_App/backend.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sqlite3
22
# import test_data
33
# import ast
4-
# import json
4+
import json
55

66
class AutoComplete:
77
"""
@@ -45,7 +45,6 @@ def __init__(self):
4545
cur = self.conn.cursor()
4646
res = cur.execute("SELECT name FROM sqlite_master WHERE name='WordMap'")
4747
tables_exist = res.fetchone()
48-
print(tables_exist)
4948

5049
if not tables_exist:
5150
self.conn.execute("CREATE TABLE WordMap(name TEXT, value TEXT)")
@@ -54,8 +53,27 @@ def __init__(self):
5453
cur.execute("INSERT INTO WordPrediction VALUES (?, ?)", ("predictions", "{}",))
5554

5655
def train(self, sentence):
56+
"""
57+
Returns - string
58+
Input - str: a string of words called sentence
59+
----------
60+
Trains the sentence. It does this by creating a map of
61+
current words to next words and their counts for each
62+
time the next word appears after the current word
63+
- takes in the sentence and splits it into a list of words
64+
- retrieves the word map and predictions map
65+
- creates the word map and predictions map together
66+
- saves word map and predictions map to the database
67+
"""
68+
cur = self.conn.cursor()
5769
words_list = sentence.split(" ")
58-
words_map = {}
70+
71+
words_map = cur.execute("SELECT value FROM WordMap WHERE name='wordsmap'").fetchone()[0]
72+
words_map = json.loads(words_map)
73+
74+
predictions = cur.execute("SELECT value FROM WordPrediction WHERE name='predictions'").fetchone()[0]
75+
predictions = json.loads(predictions)
76+
5977
for idx in range(len(words_list)-1):
6078
curr_word, next_word = words_list[idx], words_list[idx+1]
6179
if curr_word not in words_map:
@@ -65,17 +83,30 @@ def train(self, sentence):
6583
else:
6684
words_map[curr_word][next_word] += 1
6785

68-
print(words_map)
86+
# checking the completion word against the next word
87+
if curr_word not in predictions:
88+
predictions[curr_word] = {
89+
'completion_word': next_word,
90+
'completion_count': 1
91+
}
92+
else:
93+
if words_map[curr_word][next_word] > predictions[curr_word]['completion_count']:
94+
predictions[curr_word]['completion_word'] = next_word
95+
predictions[curr_word]['completion_count'] = words_map[curr_word][next_word]
96+
97+
words_map = json.dumps(words_map)
98+
predictions = json.dumps(predictions)
99+
100+
cur.execute("UPDATE WordMap SET value = (?) WHERE name='wordsmap'", (words_map,))
101+
cur.execute("UPDATE WordPrediction SET value = (?) WHERE name='predictions'", (predictions,))
102+
return("training complete")
103+
69104

70105

71106
if __name__ == "__main__":
72107
input_ = "It is not enough to just know how tools work and what they worth,\
73108
we have got to learn how to use them and to use them well. And with\
74109
all these new weapons in your arsenal, we would better get those profits fired up"
75110
ac = AutoComplete()
76-
print(ac.train(input_))
77-
# se.index_document("we should all strive to be happy and happy again")
78-
# print(se.index_document("happiness is all you need"))
79-
# se.index_document("no way should we be sad")
80-
# se.index_document("a cheerful heart is a happy one even in Nigeria")
81-
# print(se.find_documents("happy"))
111+
ac.train(input_)
112+
# print(ac.predict("to"))

0 commit comments

Comments
 (0)