diff options
Diffstat (limited to 'dict.py')
-rw-r--r-- | dict.py | 235 |
1 files changed, 117 insertions, 118 deletions
@@ -1,5 +1,6 @@ #!/bin/env python import sys +import random # Provides the API to control the dictionary. global __debug @@ -10,125 +11,123 @@ global word_list __debug = True DBname = "dict.db" word_list = [] -#strings which are tag in XML - -class dict: - - def __init__(self): - - 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.length = 0 - - def getnumwords(self, length = 0): - if self.numwords == -1: - if length == 0: - self.cur.execute("SELECT COUNT(wordid) from las_word") - else: - self.cur.execute("SELECT COUNT(wordid) from las_word where length = ?", (length, )) - self.numwords = self.cur.fetchone() - return self.numwords - - - def getrandomwordid(self, length=0, numwords = 1): - if self.wordid_list == [] or self.length != length: - if length == 0: - self.cur.execute("SELECT wordid from las_word") - else: + +class Dict: + + def __init__(self): + + 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.num_words = -1 + self.wordid_list = [] + self.length = 0 + + def get_num_words(self, length = 0): + if self.num_words == -1: + if length == 0: + self.cur.execute("SELECT COUNT(wordid) from las_word") + else: + self.cur.execute("SELECT COUNT(wordid) from las_word where length = ?", (length, )) + self.num_words = self.cur.fetchone() + return self.num_words + + + def get_random_wordid(self, length=0, numwords = 1): + if self.wordid_list == [] or self.length != length: + if length == 0: + self.cur.execute("SELECT wordid from las_word") + else: + self.length = length + self.cur.execute("SELECT wordid from las_word where length = ?", (length, )) + self.wordid_list = self.cur.fetchall() + #count = self.wordid_list.count + #count = len(self.wordid_list) + + randids = random.sample(self.wordid_list , numwords) + return randids + +class Word: + + def __init__(self, identifier=None, value= None): + 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,)) + elif identifier == None or value == None: + self.las_word_id = None + self.wordid = None + self.word = None + self.length = None + self.synsetid_list = [] + return + 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 - self.cur.execute("SELECT wordid from las_word where length = ?", (length, )) - self.wordid_list = self.cur.fetchall() - #count = self.wordid_list.count - #count = len(self.wordid_list) - import random - randids = random.sample(self.wordid_list , numwords) - return randids - -class word: - - def __init__(self, identifier=None, value= None): - 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 - print "Word is initiated" - 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,)) - elif identifier == None or value == None: - self.las_word_id = None - self.wordid = None - self.word = None - self.length = None - self.synsetid_list = [] - return - 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 - self.synsetid_list = [] - - def getword(self): - return self.word - - def getwordid(self): - return self.wordid - - def getsynsetid(self): - 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, definition) in self.cur: - self.def_list.append( (pos, definition)) - 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( (sample)) - return self.usage_list + self.synsetid_list = [] + + def get_word(self): + return self.word + + def get_wordid(self): + return self.wordid + + def get_synsetid(self): + 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 get_def(self): + self.def_list = [] + if self.synsetid_list == []: + self.get_synsetid() + for synsetid in self.synsetid_list: + self.cur.execute("SELECT * from las_synset where synsetid = ?", (synsetid,) ) + for (synsetid, pos, definition) in self.cur: + self.def_list.append( (pos, definition)) + return self.def_list + + def get_usage(self): + if self.synsetid_list == []: + self.get_synsetid() + 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( (sample)) + return self.usage_list if __name__ == "__main__": - k = dict() - num_words = k.getnumwords() - print num_words - - id = k.getrandomwordid(length = 5, numwords = 3) #will return word of length 15 - for (wordid,) in id: - print wordid - l = word("wordid", wordid ) - - print l.getword() - print l.getdef() - print l.getusage()
\ No newline at end of file + k = Dict() + num_words = k.get_num_words() + print num_words + + id = k.get_random_wordid(length = 5, numwords = 3) #will return word of length 15 + for (wordid,) in id: + print wordid + l = Word("wordid", wordid ) + + print l.get_word() + print l.get_def() + print l.get_usage()
\ No newline at end of file |