1
1
import sqlite3
2
2
# import test_data
3
3
# import ast
4
- # import json
4
+ import json
5
5
6
6
class AutoComplete :
7
7
"""
@@ -45,7 +45,6 @@ def __init__(self):
45
45
cur = self .conn .cursor ()
46
46
res = cur .execute ("SELECT name FROM sqlite_master WHERE name='WordMap'" )
47
47
tables_exist = res .fetchone ()
48
- print (tables_exist )
49
48
50
49
if not tables_exist :
51
50
self .conn .execute ("CREATE TABLE WordMap(name TEXT, value TEXT)" )
@@ -54,8 +53,27 @@ def __init__(self):
54
53
cur .execute ("INSERT INTO WordPrediction VALUES (?, ?)" , ("predictions" , "{}" ,))
55
54
56
55
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 ()
57
69
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
+
59
77
for idx in range (len (words_list )- 1 ):
60
78
curr_word , next_word = words_list [idx ], words_list [idx + 1 ]
61
79
if curr_word not in words_map :
@@ -65,17 +83,30 @@ def train(self, sentence):
65
83
else :
66
84
words_map [curr_word ][next_word ] += 1
67
85
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
+
69
104
70
105
71
106
if __name__ == "__main__" :
72
107
input_ = "It is not enough to just know how tools work and what they worth,\
73
108
we have got to learn how to use them and to use them well. And with\
74
109
all these new weapons in your arsenal, we would better get those profits fired up"
75
110
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