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
119
120
121
122
123
|
#!/bin/env python
import sys
# Provides the API to control the dictionary.
global __debug
global DBname
global word_list
__debug = True
DBname = "dict.db"
word_list = []
#strings which are tag in XML
class dictionary:
def __init__(self, DBname):
import sqlite3
self.conn = sqlite3.connect(DBname, isolation_level=None)
# Turn on autocommit mode
# Set isolation_level to "IMMEDIATE"
self.conn.isolation_level = "IMMEDIATE"
self.cur = self.conn.cursor()
self.numwords = -1
self.wordid_list = []
self.level = 0
def getnumwords(self, level = 0):
if self.numwords == -1:
if level == 0:
self.cur.execute("SELECT COUNT(wordid) from las_word")
else:
self.cur.execute("SELECT COUNT(wordid) from las_word where length = ?", (level, ))
self.numwords = self.cur.fetchone()
return self.numwords
def getrandomwordid(self, level=0):
if self.wordid_list == [] or self.level != level:
if level == 0:
self.cur.execute("SELECT wordid from las_word")
else:
self.level = level
self.cur.execute("SELECT wordid from las_word where length = ?", (level, ))
self.wordid_list = self.cur.fetchall()
#count = self.wordid_list.count
count = len(self.wordid_list)
import random
randid = random.randint(0,count)
(id,) = self.wordid_list[randid]
return id
class word:
def __init__(self, identifier, value):
import sqlite3
self.conn = sqlite3.connect(DBname, isolation_level=None)
# Turn on autocommit mode
# Set isolation_level to "IMMEDIATE"
self.conn.isolation_level = "IMMEDIATE"
self.cur = self.conn.cursor()
if identifier == "las_word_id":
self.las_word_id = value
self.cur.execute("SELECT * from las_word where laswid = ?", (value,))
elif identifier == "wordid":
self.wordid = value
self.cur.execute("SELECT * from las_word where wordid = ?", (value,))
elif identifier == "word":
self.word = value
self.cur.execute("SELECT * from las_word where lemma = ?", (value,))
else:
return "Invalid Usage"
(laswid, wordid, lemma, length) = self.cur.fetchone()
self.las_word_id = laswid
self.wordid = wordid
self.word = lemma
self.length = length
def getword(self):
return self.word
def getsynsetid(self):
self.synsetid_list = []
self.cur.execute("SELECT * from las_sense where wordid = ?", (self.wordid,))
for (wordid, synsetid, rank) in self.cur:
self.synsetid_list.append(synsetid)
return self.synsetid_list
def getdef(self):
self.def_list = []
if self.synsetid_list == []:
self.getsynsetid()
for synsetid in self.synsetid_list:
self.cur.execute("SELECT * from las_synset where synsetid = ?", (synsetid,) )
for (synsetid, pos, defination) in self.cur:
self.def_list.append( (synsetid, pos, defination))
return self.def_list
def getusage(self):
if self.synsetid_list == []:
self.getsynsetid()
self.usage_list = []
for synsetid in self.synsetid_list:
self.cur.execute("SELECT * from las_sample where synsetid = ?", (synsetid,))
for (synsetid, sampleid, sample) in self.cur:
self.usage_list.append( (synsetid, sampleid, sample))
return self.usage_list
if __name__ == "__main__":
k = dictionary("dict.db")
num_words = k.getnumwords()
print num_words
wordid = k.getrandomwordid(15) #will return word of length 15
l = word("wordid", wordid )
print l.getword()
l.getsynsetid()
print l.getdef()
print l.getusage()
|