From bff3f1a1c99f3c7c79808972fe5345949d686e6d Mon Sep 17 00:00:00 2001 From: Assim Deodia Date: Sat, 21 Jun 2008 06:47:36 +0000 Subject: Listen-Spell(command line) v1.0 --- diff --git a/begin.wav b/begin.wav new file mode 100644 index 0000000..be6ba53 --- /dev/null +++ b/begin.wav Binary files differ diff --git a/correct.wav b/correct.wav new file mode 100644 index 0000000..cb5d6bb --- /dev/null +++ b/correct.wav Binary files differ diff --git a/dict.py b/dict.py new file mode 100644 index 0000000..ac3e6ad --- /dev/null +++ b/dict.py @@ -0,0 +1,135 @@ +#!/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 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, 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, 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) + 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 + 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 + 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 + + def getword(self): + return self.word + + def getwordid(self): + return self.wordid + + 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 = [] + try: + self.synsetid_list + except AttributeError: + 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): + try: + self.synsetid_list + except AttributeError: + 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 + + +if __name__ == "__main__": + k = dict() + 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() \ No newline at end of file diff --git a/dictionary.py b/dictionary.py deleted file mode 100644 index fd4e4da..0000000 --- a/dictionary.py +++ /dev/null @@ -1,123 +0,0 @@ -#!/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() \ No newline at end of file diff --git a/incorrect.wav b/incorrect.wav new file mode 100644 index 0000000..359f147 --- /dev/null +++ b/incorrect.wav Binary files differ diff --git a/las.py b/las.py new file mode 100644 index 0000000..1a61511 --- /dev/null +++ b/las.py @@ -0,0 +1,173 @@ +#!/bin/env python +import sys +import os +import dbus +import random +import time +import csnd +from dict import dict +from dict import word + +bus = dbus.SessionBus() +APLAY = "/usr/bin/aplay" +espeak_object = bus.get_object('org.laptop.Speech','/org/laptop/Speech') + +dictionary = "" +pronouncedict = "" +skill = "" +answer = "" +clearit = "" # Because I couldn't null a string + +# My class for key detection +class _Getch: + """Gets a single character from standard input. Does not echo to the screen.""" + def __init__(self): + try: + self.impl = _GetchWindows() + except ImportError: + self.impl = _GetchUnix() + + def __call__(self): return self.impl() + +class _GetchUnix: + def __init__(self): + import tty, sys + + def __call__(self): + import sys, tty, termios + fd = sys.stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(sys.stdin.fileno()) + ch = sys.stdin.read(1) + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + return ch + +class _GetchWindows: + def __init__(self): + import msvcrt + + def __call__(self): + import msvcrt + return msvcrt.getch() + +class listenspell(): + + def __init__(self): + + self.skill_level = 0 + self.level_threshold = 5 + self.points = 0 + self.words_played = 0 + self.words_correct = 0 + self.init = False + from dict import dict + from dict import word + self.dict_obj = dict() + self.word_obj = word() + + + def startspeechd(self): # Currently not in use have to manually start sugar-speechd + import os + pid = os.fork() + if pid: + return + else: + # we are the child + os.popen("./sugar-speechd") + + def playsound(self,event): + if event == "begin": + os.popen("aplay --quiet begin.wav") + elif event == "correct": + os.popen("aplay --quiet correct.wav") + elif event == "incorrect": + os.popen("aplay --quiet incorrect.wav") + + def setskill_level(self,level): + self.skill_level = level + + def getskill_level(self): + return self.skill_level + + def getwords_played(self): + return self.words_played + + def getwords_correct(self): + return self.words_correct + + def getpoints(self): + return self.points + + def ans_correct(self, wordid): + self.playsound("correct") + self.points = self.points + self.skill_level + self.words_correct = self.words_correct + 1 + + def ans_incorrect(self, wordid): + self.playsound("incorrect") + + def clearscreen(self,numlines=100): + """Clear the console. + + numlines is an optional argument used only as a fall-back. + """ + import os + if os.name == "posix": + # Unix/Linux/MacOS/BSD/etc + os.system('clear') + elif os.name in ("nt", "dos", "ce"): + # DOS/Windows + os.system('CLS') + else: + # Fallback for other operating systems. + print '\n' * numlines + + def load_wordid(self, num_words): + temp_list = [] + self.wordid_list = [] + + temp_list = self.dict_obj.getrandomwordid(length = self.skill_level,numwords = num_words) + for(wordid, ) in temp_list: + self.wordid_list.append(wordid) + return self.wordid_list + + def getwordinfo(self,wordid, attribute): + if self.word_obj.getwordid() != wordid: + self.word_obj.__init__("wordid", wordid) + self.words_played = self.words_played + 1 + + if attribute == "def": + return self.word_obj.getdef() + elif attribute == "usage": + return self.word_obj.getusage() + elif attribute == "word": + return self.word_obj.getword() + elif attribute == "phnm": + return self.word_obj.getword() # For the time being + + else: return "Invalid attribute" + + + def saytext(self, text): + if self.init == False: + bus = dbus.SessionBus() + self.espeak_object = bus.get_object('org.laptop.Speech','/org/laptop/Speech') + self.init = True + text = str(text) + self.espeak_object.SayText(text) + + def getkey(self): + for longestinput in range(15): + inkey = _Getch() + import sys + for i in xrange(sys.maxint): + k=inkey() + if k<>'':break + elif k == '\r': break + return k + + def exitgame(self): + self.saytext("goodbye") + sys.exit() diff --git a/ls.py b/ls.py new file mode 100644 index 0000000..82a19da --- /dev/null +++ b/ls.py @@ -0,0 +1,148 @@ +from las import listenspell + + + +las = listenspell() +las.clearscreen() +las.playsound("begin") + +######################################################################################## +def playword(wordid): + elem = las.getwordinfo(wordid, "word") #get a word from the list + pronounelem = las.getwordinfo(wordid, "phnm") #get a pronounciation from the list + + if elem != pronounelem: # determine whether to bother pronouncing a description + print "Spell..." # say the explanation if the word is ambiguous. + las.saytext("Spell... ... " + elem + "... " + pronounelem) + else: + print "Spell... " + las.saytext("Spell... ... " + elem) + +## get an answer + print 'Press $ to answer or spacebar to get a clue. Press * to quit' + + k = las.getkey() + #las.saytext(k) + if k == "*": + las.exitgame() + + if k == " ": # if the user wants a clue + cluemode = True + reprint = True + while(cluemode): + string = """press + 1 for definition + 2 for Sample + 3 to repete the word + 4 for word length + or any other key to answer""" + if reprint == True: + print string + #las.saytext(string) + k = las.getkey() + if k == "1": + reprint = True + definition = las.getwordinfo(wordid, "def") + for (pos, definition) in definition: + print pos + " : " + definition + elif k == "2": + usage = las.getwordinfo(wordid, "usage") + num_sample = len(usage) + if num_sample == 0: + print "No usage in the database" + reprint = True + for (sample) in usage: + las.saytext(sample) + num_sample = num_sample - 1 + if num_sample != 0: + print "To get another sample press 'n' or anyother key to answer" + k = las.getkey() + if k != 'n': break + elif k == "3": + las.saytext(elem) + reprint = False + elif k == "4": + print "Word Length:" + str(len(elem)) + else: + cluemode = False + + answer = raw_input( "Your answer: " ) + answer = answer.strip() + las.saytext("You typed\n") + shout(answer) + if answer == elem: + las.saytext("Correct") + las.ans_correct(wordid) + return True + else: + las.ans_incorrect(wordid) + las.saytext("Incorrect") + print "Incorrect. The correct spelling is.. " + las.saytext("The correct spelling is..\n") + shout(elem) + return False + +######################################################################################## +def shout(string): + for character in string: + print character + " " + las.saytext(character) +######################################################################################## +def getskilllevel(): +# get a skill level + print 'Enter skill level between 1 and 9, or * to quit' + las.saytext("Skill level") + k = las.getkey() + if k == "*": + las.exitgame() # quit the program + print k + las.saytext(k) + + skill_level = int(k) + 3 + + las.setskill_level( skill_level) + + + + +gameover = False +while gameover == False: + levelsize = 7 # this stays constant throughout + + getskilllevel() + wordidlist = las.load_wordid(levelsize + 3) # At max Three error possible + num_words = len(wordidlist) + las.clearscreen() + + #### MAIN LOOP + + las.playsound("begin") + + + for index in range(num_words): + + ## print pronouncedict + ## print pronouncedictstring + ## print pronouncelist + wordid = wordidlist[index] + result = playword(wordid) + print "Your score is " + str(las.getpoints()) + + if result == True: + if las.getwords_correct == 7: + print "Very good." + print "If you want to play game again with another level press $ or any other key to quit" + k = las.getkey() + if k == '$': + break # Restart game with next level + else: + las.exitgame() + + if las.getwords_correct == 7: + continue # Restart game with next level + else: + gameover = True + las.saytext("Game Over") + print "Game Over." + las.exitgame() + -- cgit v0.9.1