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 wit
| 3 | |
| 4 | |
| 5 | class AutoComplete: |
| 6 | """ |
| 7 | It works by building a `WordMap` that stores words to word-follower-count |
| 8 | ---------------------------- |
| 9 | e.g. To train the following statement: |
| 10 | |
| 11 | It is not enough to just know how tools work and what they worth, |
| 12 | we have got to learn how to use them and to use them well. |
| 13 | And with all these new weapons in your arsenal, we would better |
| 14 | get those profits fired up |
| 15 | |
| 16 | we create the following: |
| 17 | { It: {is:1} |
| 18 | is: {not:1} |
| 19 | not: {enough:1} |
| 20 | enough: {to:1} |
| 21 | to: {just:1, learn:1, use:2} |
| 22 | just: {know:1} |
| 23 | . |
| 24 | . |
| 25 | profits: {fired:1} |
| 26 | fired: {up:1} |
| 27 | } |
| 28 | so the word completion for "to" will be "use". |
| 29 | For optimization, we use another store `WordPrediction` to save the |
| 30 | predictions for each word |
| 31 | """ |
| 32 | |
| 33 | def __init__(self): |
| 34 | """ |
| 35 | Returns - None |
| 36 | Input - None |
| 37 | ---------- |
| 38 | - Initialize database. we use sqlite3 |
| 39 | - Check if the tables exist, if not create them |
| 40 | - maintain a class level access to the database |
| 41 | connection object |
| 42 | """ |
| 43 | self.conn = sqlite3.connect("autocompleteDB.sqlite3", autocommit=True) |
| 44 | cur = self.conn.cursor() |
| 45 | res = cur.execute("SELECT name FROM sqlite_master WHERE name='WordMap'") |
| 46 | tables_exist = res.fetchone() |
| 47 | |
| 48 | if not tables_exist: |
| 49 | self.conn.execute("CREATE TABLE WordMap(name TEXT, value TEXT)") |
| 50 | self.conn.execute("CREATE TABLE WordPrediction (name TEXT, value TEXT)") |
| 51 | cur.execute( |
| 52 | "INSERT INTO WordMap VALUES (?, ?)", |
| 53 | ( |
| 54 | "wordsmap", |
| 55 | "{}", |
| 56 | ), |
| 57 | ) |
| 58 | cur.execute( |
| 59 | "INSERT INTO WordPrediction VALUES (?, ?)", |
| 60 | ( |
| 61 | "predictions", |
| 62 | "{}", |