1+ import sqlite3
2+ import json
3+
4+ class AutoComplete :
5+ """
6+ It works by building a `WordMap` that stores words to word-follower-count
7+ ----------------------------
8+ e.g. To train the following statement:
9+
10+ It is not enough to just know how tools work and what they worth,
11+ we have got to learn how to use them and to use them well.
12+ And with all these new weapons in your arsenal, we would better
13+ get those profits fired up
14+
15+ we create the following:
16+ { It: {is:1}
17+ is: {not:1}
18+ not: {enough:1}
19+ enough: {to:1}
20+ to: {just:1, learn:1, use:2}
21+ just: {know:1}
22+ .
23+ .
24+ profits: {fired:1}
25+ fired: {up:1}
26+ }
27+ so the word completion for "to" will be "use".
28+ For optimization, we use another store `WordPrediction` to save the
29+ predictions for each word
30+ """
31+
32+ def __init__ (self ):
33+ """
34+ Returns - None
35+ Input - None
36+ ----------
37+ - Initialize database. we use sqlite3
38+ - Check if the tables exist, if not create them
39+ - maintain a class level access to the database
40+ connection object
41+ """
42+ self .conn = sqlite3 .connect ("autocompleteDB.sqlite3" , autocommit = True )
43+ cur = self .conn .cursor ()
44+ res = cur .execute ("SELECT name FROM sqlite_master WHERE name='WordMap'" )
45+ tables_exist = res .fetchone ()
46+
47+ if not tables_exist :
48+ self .conn .execute ("CREATE TABLE WordMap(name TEXT, value TEXT)" )
49+ self .conn .execute ('CREATE TABLE WordPrediction (name TEXT, value TEXT)' )
50+ cur .execute ("INSERT INTO WordMap VALUES (?, ?)" , ("wordsmap" , "{}" ,))
51+ cur .execute ("INSERT INTO WordPrediction VALUES (?, ?)" , ("predictions" , "{}" ,))
52+
53+ def train (self , sentence ):
54+ """
55+ Returns - string
56+ Input - str: a string of words called sentence
57+ ----------
58+ Trains the sentence. It does this by creating a map of
59+ current words to next words and their counts for each
60+ time the next word appears after the current word
61+ - takes in the sentence and splits it into a list of words
62+ - retrieves the word map and predictions map
63+ - creates the word map and predictions map together
64+ - saves word map and predictions map to the database
65+ """
66+ cur = self .conn .cursor ()
67+ words_list = sentence .split (" " )
68+
69+ words_map = cur .execute ("SELECT value FROM WordMap WHERE name='wordsmap'" ).fetchone ()[0 ]
70+ words_map = json .loads (words_map )
71+
72+ predictions = cur .execute ("SELECT value FROM WordPrediction WHERE name='predictions'" ).fetchone ()[0 ]
73+ predictions = json .loads (predictions )
74+
75+ for idx in range (len (words_list )- 1 ):
76+ curr_word , next_word = words_list [idx ], words_list [idx + 1 ]
77+ if curr_word not in words_map :
78+ words_map [curr_word ] = {}
79+ if next_word not in words_map [curr_word ]:
80+ words_map [curr_word ][next_word ] = 1
81+ else :
82+ words_map [curr_word ][next_word ] += 1
83+
84+ # checking the completion word against the next word
85+ if curr_word not in predictions :
86+ predictions [curr_word ] = {
87+ 'completion_word' : next_word ,
88+ 'completion_count' : 1
89+ }
90+ else :
91+ if words_map [curr_word ][next_word ] > predictions [curr_word ]['completion_count' ]:
92+ predictions [curr_word ]['completion_word' ] = next_word
93+ predictions [curr_word ]['completion_count' ] = words_map [curr_word ][next_word ]
94+
95+ words_map = json .dumps (words_map )
96+ predictions = json .dumps (predictions )
97+
98+ cur .execute ("UPDATE WordMap SET value = (?) WHERE name='wordsmap'" , (words_map ,))
99+ cur .execute ("UPDATE WordPrediction SET value = (?) WHERE name='predictions'" , (predictions ,))
100+ return ("training complete" )
101+
102+ def predict (self , word ):
103+ """
104+ Returns - string
105+ Input - string
106+ ----------
107+ Returns the completion word of the input word
108+ - takes in a word
109+ - retrieves the predictions map
110+ - returns the completion word of the input word
111+ """
112+ cur = self .conn .cursor ()
113+ predictions = cur .execute ("SELECT value FROM WordPrediction WHERE name='predictions'" ).fetchone ()[0 ]
114+ predictions = json .loads (predictions )
115+ completion_word = predictions [word .lower ()]['completion_word' ]
116+ return completion_word
117+
118+
119+
120+ if __name__ == "__main__" :
121+ input_ = "It is not enough to just know how tools work and what they worth,\
122+ we have got to learn how to use them and to use them well. And with\
123+ all these new weapons in your arsenal, we would better get those profits fired up"
124+ ac = AutoComplete ()
125+ ac .train (input_ )
126+ print (ac .predict ("to" ))
0 commit comments