| 1 | import sys |
|---|
| 2 | |
|---|
| 3 | try: |
|---|
| 4 | import sqlite3 |
|---|
| 5 | except: |
|---|
| 6 | print "Unable to load sqlite3 library." |
|---|
| 7 | print |
|---|
| 8 | print "The dungeon collapses on your dreams. Your bot remains dumb\nand predictable." |
|---|
| 9 | sys.exit(1) |
|---|
| 10 | |
|---|
| 11 | def main(): |
|---|
| 12 | print "Opening database...", |
|---|
| 13 | try: |
|---|
| 14 | conn = sqlite3.connect('bmotion_thes.db'); |
|---|
| 15 | conn.isolation_level = "IMMEDIATE" |
|---|
| 16 | except: |
|---|
| 17 | print "failed." |
|---|
| 18 | sys.exit(1) |
|---|
| 19 | |
|---|
| 20 | print "done." |
|---|
| 21 | |
|---|
| 22 | print "Preparing table...", |
|---|
| 23 | |
|---|
| 24 | try: |
|---|
| 25 | conn.execute("""CREATE TABLE IF NOT EXISTS thes ( |
|---|
| 26 | word varchar(30) unique, |
|---|
| 27 | syn text);""") |
|---|
| 28 | except Exception, e: |
|---|
| 29 | print "Unable to create table." |
|---|
| 30 | print e |
|---|
| 31 | sys.exit(1) |
|---|
| 32 | |
|---|
| 33 | try: |
|---|
| 34 | conn.execute("""DROP INDEX IF EXISTS word_idx;""") |
|---|
| 35 | except: |
|---|
| 36 | print "Unable to drop index." |
|---|
| 37 | sys.exit(1) |
|---|
| 38 | |
|---|
| 39 | print "OK" |
|---|
| 40 | |
|---|
| 41 | print "Opening thesaurus source file...", |
|---|
| 42 | |
|---|
| 43 | try: |
|---|
| 44 | fh = open("mthesaur.txt", "r") |
|---|
| 45 | except: |
|---|
| 46 | print "failed." |
|---|
| 47 | sys.exit(2) |
|---|
| 48 | |
|---|
| 49 | print "OK" |
|---|
| 50 | |
|---|
| 51 | print "Importing", |
|---|
| 52 | |
|---|
| 53 | count = 0 |
|---|
| 54 | errors = 0 |
|---|
| 55 | lines = 0 |
|---|
| 56 | |
|---|
| 57 | for line in fh: |
|---|
| 58 | line = line.strip() |
|---|
| 59 | if line == "": |
|---|
| 60 | continue |
|---|
| 61 | lines += 1 |
|---|
| 62 | try: |
|---|
| 63 | key = line.split(",")[0] |
|---|
| 64 | syns = line[line.find(",")+1:] |
|---|
| 65 | |
|---|
| 66 | # rebuild syns list to make sure it's OK |
|---|
| 67 | syn_list = syns.split(",") |
|---|
| 68 | syns = ",".join([x.strip() for x in syn_list]) |
|---|
| 69 | conn.execute("INSERT INTO thes values (?, ?);", (key, syns)) |
|---|
| 70 | count += 1 |
|---|
| 71 | except Exception, e: |
|---|
| 72 | errors += 1 |
|---|
| 73 | if lines % 10 == 0: |
|---|
| 74 | if lines % 100 == 0: |
|---|
| 75 | sys.stdout.write(str(lines)) |
|---|
| 76 | else: |
|---|
| 77 | sys.stdout.write(".") |
|---|
| 78 | |
|---|
| 79 | print "\n%d entries imported, %d entries skipped." % (count, errors) |
|---|
| 80 | if errors > 0: |
|---|
| 81 | print "(Skipped entries are duplicates or are badly formatted.)" |
|---|
| 82 | |
|---|
| 83 | print "Preparing index...", |
|---|
| 84 | |
|---|
| 85 | try: |
|---|
| 86 | conn.execute("CREATE INDEX word_idx ON thes (word);") |
|---|
| 87 | except: |
|---|
| 88 | print "failed." |
|---|
| 89 | sys.exit(1) |
|---|
| 90 | |
|---|
| 91 | print "OK" |
|---|
| 92 | print |
|---|
| 93 | |
|---|
| 94 | print "Finished." |
|---|
| 95 | |
|---|
| 96 | if __name__ == "__main__": |
|---|
| 97 | print "bMotion Thesaurus Import Tool" |
|---|
| 98 | print |
|---|
| 99 | main() |
|---|
| 100 | |
|---|