1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# Created for the OpenTeacher project
# Released under GPLv3 license
#
# Usage:
#
# to synchronize the local database with the online database
# Synonym.updateDB('en')
#
# to look up a word (in this case the word 'bad') in the local database (returns an array with all found synonyms or None if none were found)
# getWordLocal('bad','en')
#
# to look up a word (in this case the word 'bad') in the online database (returns an array with all found synonyms or None if none were found)
# getWordOnline('bad','en')
try:
import simplejson as json
except ImportError:
import json
import urllib
import sqlite3
# Put your unique application token here (you can use this default one for now)
SYNONYM_DB_TOKEN = '163dc7cb5c492d3cc903e724b6594ea52fc1eb08'
# Synonym database URL. You do not want to touch this.
SYNONYM_DB_URL = 'http://www.milanboers.nl/py-synonyms/synonymlist.php'
# Filename of the database
SYNONYM_DB_NAME = 'synonyms.db'
# Current languages in this database:
# en (English)
class Synonym:
#updates the local table for this language, and creates one if it doesnt already exist
@staticmethod
def updateDB(lang):
db = sqlite3.connect(SYNONYM_DB_NAME)
#first get the current version of the database
currentVersion = Synonym.getDBVersionLocal(lang)
if currentVersion == 0:
#table doesnt exist. Make it
db.execute('CREATE TABLE ' + lang + ' (id int, words text, lastupdate int)')
params = urllib.urlencode({'token' : SYNONYM_DB_TOKEN, 'lang' : lang, 'time' : currentVersion})
page = urllib.urlopen(SYNONYM_DB_URL + '?%s' % params)
feedback = json.loads(page.read())
dbCursor = db.cursor()
#update the table
for row in feedback:
#check if the row exists
try:
dbCursor.execute("UPDATE " + lang + " SET words = '" + json.dumps(row[1]) + "' AND lastupdate = " + row[2] + " WHERE id = " + row[0])
dbCursor.fetchone()[0]
except:
db.execute("INSERT INTO " + lang + " VALUES ('" + row[0] + "','" + json.dumps(row[1]) + "', '" + row[2] + "')")
db.commit()
db.close()
#get a word from the online db
@staticmethod
def getWordOnline(word,lang):
# Get it online
params = urllib.urlencode({'token' : SYNONYM_DB_TOKEN, 'lang' : lang, 'word' : word})
page = urllib.urlopen(SYNONYM_DB_URL + '?%s' % params)
feedback = json.loads(page.read())
return feedback
#get a word from the local db
@staticmethod
def getWordLocal(word,lang):
db = sqlite3.connect(SYNONYM_DB_NAME)
dbCursor = db.cursor()
try:
dbCursor.execute("SELECT words FROM " + lang + " WHERE words LIKE '%" + word + "%' LIMIT 1")
except OperationalError:
#table doesn't exist, return none
return None
if len(dbCursor.fetchall()) == 0:
#no words found, return none
return None
words = json.loads(dbCursor.fetchall()[0])
db.close()
for wordInArray in words:
if wordInArray == word:
words.remove(word)
return words
#gets the local table version, returns 0 if there is no table
@staticmethod
def getDBVersionLocal(lang):
db = sqlite3.connect(SYNONYM_DB_NAME)
dbCursor = db.cursor()
try:
dbCursor.execute("SELECT MAX(lastupdate) FROM " + lang)
except sqlite3.OperationalError:
#table does not exit, return 0 as the time
return 0
feedback = dbCursor.fetchone()[0]
db.close()
return feedback
#gets the online db version
@staticmethod
def getDBVersionOnline(lang):
params = urllib.urlencode({'token' : SYNONYM_DB_TOKEN, 'lang' : lang, 'lastupdate' : 'yes'})
page = urllib.urlopen(SYNONYM_DB_URL + '?%s' % params)
feedback = page.read()
return feedback
|