Skip to content

Commit 1bd2370

Browse files
committed
added train method
1 parent ab16fbc commit 1bd2370

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

AutoComplete_App/backend.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import sqlite3
2+
# import test_data
3+
# import ast
4+
# import json
5+
6+
class AutoComplete:
7+
"""
8+
It works by building a `WordMap` that stores words to word-follower-count
9+
----------------------------
10+
e.g. To train the following statement:
11+
12+
It is not enough to just know how tools work and what they worth,
13+
we have got to learn how to use them and to use them well.
14+
And with all these new weapons in your arsenal, we would better
15+
get those profits fired up
16+
17+
we create the following:
18+
{ It: {is:1}
19+
is: {not:1}
20+
not: {enough:1}
21+
enough: {to:1}
22+
to: {just:1, learn:1, use:2}
23+
just: {know:1}
24+
.
25+
.
26+
profits: {fired:1}
27+
fired: {up:1}
28+
}
29+
so the word completion for "to" will be "use".
30+
For optimization, we use another store `WordPrediction` to save the
31+
predictions for each word
32+
"""
33+
34+
def __init__(self):
35+
"""
36+
Returns - None
37+
Input - None
38+
----------
39+
- Initialize database. we use sqlite3
40+
- Check if the tables exist, if not create them
41+
- maintain a class level access to the database
42+
connection object
43+
"""
44+
self.conn = sqlite3.connect("autocompleteDB.sqlite3", autocommit=True)
45+
cur = self.conn.cursor()
46+
res = cur.execute("SELECT name FROM sqlite_master WHERE name='WordMap'")
47+
tables_exist = res.fetchone()
48+
print(tables_exist)
49+
50+
if not tables_exist:
51+
self.conn.execute("CREATE TABLE WordMap(name TEXT, value TEXT)")
52+
self.conn.execute('CREATE TABLE WordPrediction (name TEXT, value TEXT)')
53+
cur.execute("INSERT INTO WordMap VALUES (?, ?)", ("wordsmap", "{}",))
54+
cur.execute("INSERT INTO WordPrediction VALUES (?, ?)", ("predictions", "{}",))
55+
56+
def train(self, sentence):
57+
words_list = sentence.split(" ")
58+
words_map = {}
59+
for idx in range(len(words_list)-1):
60+
curr_word, next_word = words_list[idx], words_list[idx+1]
61+
if curr_word not in words_map:
62+
words_map[curr_word] = {}
63+
if next_word not in words_map[curr_word]:
64+
words_map[curr_word][next_word] = 1
65+
else:
66+
words_map[curr_word][next_word] += 1
67+
68+
print(words_map)
69+
70+
71+
if __name__ == "__main__":
72+
input_ = "It is not enough to just know how tools work and what they worth,\
73+
we have got to learn how to use them and to use them well. And with\
74+
all these new weapons in your arsenal, we would better get those profits fired up"
75+
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"))

0 commit comments

Comments
 (0)