From 374c6b082614bfdb26ce6241ab1d7e18dad5acb3 Mon Sep 17 00:00:00 2001 From: Assim Deodia Date: Mon, 30 Jun 2008 13:08:28 +0000 Subject: First version of GUI --- diff --git a/dict.py b/dict.py index d9a2836..47032be 100644 --- a/dict.py +++ b/dict.py @@ -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 diff --git a/las-gui.py b/las-gui.py new file mode 100644 index 0000000..2d2e21b --- /dev/null +++ b/las-gui.py @@ -0,0 +1,364 @@ +#!/usr/bin/env python + +# example helloworld.py + +import pygtk +pygtk.require('2.0') +import gtk +from las import Listenspell + +class LS_gui: + + # This is a callback function. The data arguments are ignored + # in this example. More on callbacks below. + def __init__(self): + DBname = "dict.db" + self.las = Listenspell(DBname) + self.load_activity_interface() + + def destroy(self, widget, data=None): + #print "destroy signal occurred" + gtk.main_quit() + + def load_activity_interface(self): + self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL) + self.main_window.connect("destroy", self.destroy) + + self.main_window.set_border_width(10) + + self.main_window.set_title("Listen and Spell") + + self.Hcontainer = gtk.HBox() + self.Hcontainer.show() + + self.main_window.add(self.Hcontainer) + + self.vcontainer_left = gtk.VBox() + self.vcontainer_left.set_border_width(10) + self.vcontainer_left.show() + + self.vcontainer_right = gtk.VBox() + self.vcontainer_right.set_border_width(10) + self.vcontainer_right.show() + + + self.Hcontainer.pack_start(self.vcontainer_left,True, True,0) + self.Hcontainer.pack_start(self.vcontainer_right,True, True,0) + + #####################Left Pane widgets########################## + + self.label_v_left_a = gtk.Label("Hello this is label 1") + self.label_v_left_b = gtk.Label("label 2") + + self.label_v_left_a.show() + self.label_v_left_b.show() + + self.text_input = gtk.Entry(0) + #self.text_input.set_text("Preset input text") + #self.text_input. + self.text_input.show() + self.text_input.connect("focus-in-event", self.text_input_focus, None) + self.text_input.connect("activate", self.text_input_activate, None) + + self.console_text_view = gtk.TextView() + #self.console_text_view.show() + + self.console_text_sw = gtk.ScrolledWindow() + self.console_text_sw.set_shadow_type(gtk.SHADOW_ETCHED_IN) + self.console_text_sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) + + self.console_text_sw.add(self.console_text_view) + + + self.text_submit_button = gtk.Button(label="Submit") + self.text_submit_button.show() + self.text_submit_button.connect("clicked", self.submit_button_clicked, None) + + self.console_text_buffer = gtk.TextBuffer() + self.console_text_buffer.set_text("This is hint text") + self.console_text_view.set_editable(False) + self.console_text_view.set_buffer(self.console_text_buffer) + + self.console_text_frame = gtk.Frame("Console:") + self.console_text_frame.add(self.console_text_sw) + self.console_text_frame.show_all() + + self.vcontainer_left.add(self.label_v_left_a) + self.vcontainer_left.add(self.label_v_left_b) + self.vcontainer_left.add(self.text_input) + self.vcontainer_left.add(self.text_submit_button) + self.vcontainer_left.add(self.console_text_frame) + + + ################################################################ + + #################Right pane wigets############################## + + self.v_buttonbox = gtk.VButtonBox() + + self.repeat_word_button = gtk.Button(label="Repeat Word") + self.repeat_word_button.connect("clicked", self.repeat_word_button_clicked, None) + + self.get_def_button = gtk.Button(label="Get Def") + self.get_def_button.connect("clicked", self.get_def_button_clicked, None) + + self.get_usage_button = gtk.Button("Get Usage") + self.get_usage_button.connect("clicked", self.get_usage_button_clicked, None) + + self.get_word_length_button = gtk.Button("Get Word Length") + self.get_word_length_button.connect("clicked", self.get_word_length_button_clicked, None) + + self.v_buttonbox.add(self.repeat_word_button) + self.v_buttonbox.add(self.get_def_button) + self.v_buttonbox.add(self.get_usage_button) + self.v_buttonbox.add(self.get_word_length_button) + + self.v_buttonbox.show_all() + + self.button_frame = gtk.Frame("Buttons") + self.button_frame.add(self.v_buttonbox) + self.button_frame.show() + + self.vcontainer_right.add(self.button_frame) + + self.stats_frame = gtk.Frame("Stats") + + self.stats_table = gtk.Table(4,2,True) + + self.score_label = gtk.Label("Score: ") + self.score_value_label = gtk.Label("s") + #self.score_value_label. + + self.skill_level_label = gtk.Label("Skill Level: ") + self.skill_level_value_label = gtk.Label("sl") + + self.words_played_label = gtk.Label("Words played: ") + self.words_played_value_label = gtk.Label("wp") + + self.words_correct_label = gtk.Label("Correct Words: ") + self.words_correct_value_label = gtk.Label("cw") + + self.stats_table.attach(self.score_label, 0, 1, 0, 1) + self.stats_table.attach(self.score_value_label, 1, 2, 0, 1) + + self.stats_table.attach(self.skill_level_label, 0, 1, 1, 2) + self.stats_table.attach(self.skill_level_value_label, 1, 2, 1, 2) + + self.stats_table.attach(self.words_played_label, 0, 1, 2, 3) + self.stats_table.attach(self.words_played_value_label, 1, 2, 2, 3) + + self.stats_table.attach(self.words_correct_label, 0, 1, 3, 4) + self.stats_table.attach(self.words_correct_value_label, 1, 2, 3, 4) + + self.vcontainer_right.add(self.stats_frame) + self.stats_frame.add(self.stats_table) + + self.stats_frame.show_all() + + ################################################################ + + self.main_window.show() + + ##################Call Backs#################################### + + def submit_button_clicked(self, widget, data = None): + answer = self.text_input.get_text() + self.las.say_text("You typed") + self.shout(answer) + if answer == self.elem: + self.las.say_text("Correct") + self.las.ans_correct(self.wordid) + + else: + self.las.ans_incorrect(self.wordid) + self.las.say_text("Incorrect") + self.display_console("Incorrect. The correct spelling is.. ") + self.las.say_text("The correct spelling is..\n") + self.shout(self.elem) + self.update_all() + self.play_game("next word") + return False + + def text_input_focus(self, widget, event, data= None): + #print "text_input_focus_in" + return False + + def text_input_activate(self, widget, data=None): + self.submit_button_clicked(widget, data) + #print "text_input_activate" + return False + + def repeat_word_button_clicked(self, widget, data = None): + self.las.say_text(self.elem) + + def get_def_button_clicked(self, widget, data = None): + definition = self.las.get_word_info(self.wordid, "def") + self.display_console("Definition: ") + for (pos, definition) in definition: + self.display_console(pos + " : " + definition) + + def get_usage_button_clicked(self, widget, data = None): + if self.usage_used == 0: + usage = self.las.get_word_info(self.wordid, "usage") + self.total_num_usage = len(usage) + if self.total_num_usage == 0: + self.display_console("No usage in the database") + elif self.total_num_usage == self.usage_used: + self.display_console("No more usage in the database") + else: + (sample) = usage[self.usage_used] + las.say_text(sample) + self.usage_used = self.usage_used + 1 + + def get_word_length_button_clicked(self, widget, data = None): + self.display_console("Word Length: " + str(len(self.elem))) + + def ask_skill_level(self): + dialog = gtk.Dialog("Enter Skill Level", self.main_window, 0,(gtk.STOCK_OK, gtk.RESPONSE_OK, "Cancel", gtk.RESPONSE_CANCEL)) + self.las.say_text("Skill Level") + hbox = gtk.HBox(False, 8) + hbox.set_border_width(8) + dialog.vbox.pack_start(hbox, False, False, 0) + stock = gtk.image_new_from_stock( + gtk.STOCK_DIALOG_QUESTION, + gtk.ICON_SIZE_DIALOG) + hbox.pack_start(stock, False, False, 0) + + table = gtk.Table(1, 1) + table.set_row_spacings(4) + table.set_col_spacings(4) + hbox.pack_start(table, True, True, 0) + + label = gtk.Label("Skill Level") + label.set_use_underline(True) + table.attach(label, 0, 1, 0, 1) + local_skill_level = gtk.Entry() + local_skill_level.set_text(str(self.las.get_skill_level())) + table.attach(local_skill_level, 1, 2, 0, 1) + label.set_mnemonic_widget(local_skill_level) + + dialog.show_all() + response = dialog.run() + + if response == gtk.RESPONSE_OK: + skill_level = int (local_skill_level.get_text()) + + dialog.destroy() + self.las.set_skill_level(skill_level) + self.update_all() + + def shout(self,string): + self.display_console("") + for char in string: + self.display_console(char, newline = False) + self.las.say_text(char) + + ################################################################ + + #####################Update methods############################# + def update_all(self): + self.update_score() + self.update_skill_level() + self.update_words_played() + self.update_words_correct() + + def update_score(self, score = None): + if score ==None: + score = self.las.get_points() + self.score_value_label.set_text(str(score)) + + def update_skill_level(self, skill_level = None): + if skill_level ==None: + skill_level = self.las.get_skill_level() + self.skill_level_value_label.set_text(str(skill_level)) + + def update_words_played(self, words_played = None): + if words_played ==None: + words_played = self.las.get_words_played() + self.words_played_value_label.set_text(str(words_played)) + + def update_words_correct(self, words_correct = None): + if words_correct ==None: + words_correct = self.las.get_words_correct() + self.words_correct_value_label.set_text(str(words_correct)) + + def play_word(self): + self.usage_used = -1 + self.total_num_usage = -1 + + # Initilazing variable for eacch word + + self.elem = self.las.get_word_info(self.wordid, "word") #get a word from the list + self.pronounelem = self.las.get_word_info(self.wordid, "phnm") #get a pronounciation from the list + + self.display_console("Spell...") # say the explanation if the word is ambiguous. + if self.elem != self.pronounelem: # determine whether to bother pronouncing a description + + self.las.say_text("Spell... ... " + self.elem + "... " + self.pronounelem) + else: + #print "Spell... " + self.las.say_text("Spell... ... " + self.elem) + + def play_game(self,mode = "start"): + if mode == "start": + self.las.play_sound("begin") + self.this_level_size = 7 + self.this_level_max_error = 3 + self.display_label_a("Welcome") + self.las.say_text("Welcome") + self.ask_skill_level() + self.wordid_list = self.las.load_wordid(self.this_level_size + self.this_level_max_error) + self.this_level_num_words = len(self.wordid_list) + self.this_level_words_left = self.this_level_num_words + self.word_index = 1 + self.wordid = self.wordid_list[self.word_index] + self.play_word() + elif mode == "next word": + self.this_level_words_left -= 1 + if self.this_level_num_words == 0: + if self.las.words_correct == self.this_level_size: + self.display_console("You have crossed this level") + self.las.say_text("congratulation") + self.display_console("Please select new level to play the game") + else: + self.display_console("Sorry you are not able to cross this level") + self.las.say_text("Game Over") + self.display_console("Please select lower level to play the game") + self.play_game("start") + self.word_index += 1 + self.wordid = self.wordid_list[self.word_index] + self.play_word() + + ########################Display Methods######################### + + def display_console(self, text, newline = True ): + if newline == True: + text = "\n" + text + self.console_text_buffer.insert_at_cursor(text) + + def display_label_a(self, text): + self.label_v_left_a.set_text(text) + + def display_label_b(self, text): + self.label_v_left_b.set_text(text) + + ################################################################ + + ########################Game Logic############################## + + ################################################################ + + + + def main(self): + # All PyGTK applications must have a gtk.main(). Control ends here + # and waits for an event to occur (like a key press or mouse event). + gtk.main() + +# If the program is run directly or passed as an argument to the python +# interpreter then create a HelloWorld instance and show it +if __name__ == "__main__": + gui = LS_gui() + #gui.__init__() + gui.play_game("start") + gui.main() diff --git a/las.py b/las.py index 9ed08d3..f9bb3be 100644 --- a/las.py +++ b/las.py @@ -5,159 +5,155 @@ import dbus import random import time import csnd -from dict import dict -from dict import word +from dict import Dict +from dict import Word # 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() + """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() + 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 + 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 __init__(self): + import msvcrt - def __call__(self): - import msvcrt - return msvcrt.getch() + def __call__(self): + import msvcrt + return msvcrt.getch() -class listenspell(): +class Listenspell(): - def __init__(self): + 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() + self.skill_level = 0 + self.level_threshold = 5 + self.points = 0 + self.words_played = 0 + self.words_correct = 0 + self.init = False + 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 start_speechd(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 play_sound(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 set_skill_level(self,level): + self.skill_level = level - def getskill_level(self): - return self.skill_level + def get_skill_level(self): + return self.skill_level - def getwords_played(self): - return self.words_played + def get_words_played(self): + return self.words_played - def getwords_correct(self): - return self.words_correct + def get_words_correct(self): + return self.words_correct - def getpoints(self): - return self.points + def get_points(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_correct(self, wordid): + self.play_sound("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. + def ans_incorrect(self, wordid): + self.play_sound("incorrect") + def clear_screen(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__(identifier = "wordid", value= wordid) - print "Hello Again" - 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() + 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.get_random_wordid(length = self.skill_level,numwords = num_words) + for(wordid, ) in temp_list: + self.wordid_list.append(wordid) + return self.wordid_list + + def get_word_info(self,wordid, attribute): + if self.word_obj.get_wordid() != wordid: + self.word_obj.__init__(identifier = "wordid", value= wordid) + print "Hello Again" + self.words_played = self.words_played + 1 + + if attribute == "def": + return self.word_obj.get_def() + elif attribute == "usage": + return self.word_obj.get_usage() + elif attribute == "word": + return self.word_obj.get_word() + elif attribute == "phnm": + return self.word_obj.get_word() # For the time being + + else: return "Invalid attribute" + + def say_text(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 get_key(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 exit_game(self): + self.say_text("goodbye") + sys.exit() \ No newline at end of file diff --git a/ls.py b/ls.py index 6871e61..52b0a34 100644 --- a/ls.py +++ b/ls.py @@ -1,156 +1,153 @@ -from las import listenspell +from las import Listenspell ######################################################################################## -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 +def play_word(wordid): + elem = las.get_word_info(wordid, "word") #get a word from the list + pronounelem = las.get_word_info(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) + if elem != pronounelem: # determine whether to bother pronouncing a description + print "Spell..." # say the explanation if the word is ambiguous. + las.say_text("Spell... ... " + elem + "... " + pronounelem) + else: + print "Spell... " + las.say_text("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 - * to quit - or any other key to answer""" - if reprint == True: - print string - #las.saytext(string) - k = las.getkey() - if k == "1": + print 'Press $ to answer or spacebar to get a clue. Press * to quit' + + k = las.get_key() + #las.saytext(k) + if k == "*": + las.exit_game() + + if k == " ": # if the user wants a clue + cluemode = True 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) - reprint = False - num_sample = num_sample - 1 - if num_sample != 0: - reprint = True - 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)) - elif k =="*": - las.exitgame() - 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 + while(cluemode): + string = """press + 1 for definition + 2 for Sample + 3 to repete the word + 4 for word length + * to quit + or any other key to answer""" + if reprint == True: + print string + #las.saytext(string) + k = las.get_key() + if k == "1": + reprint = True + definition = las.get_word_info(wordid, "def") + for (pos, definition) in definition: + print pos + " : " + definition + elif k == "2": + usage = las.get_word_info(wordid, "usage") + num_sample = len(usage) + if num_sample == 0: + print "No usage in the database" + reprint = True + for (sample) in usage: + las.say_text(sample) + reprint = False + num_sample = num_sample - 1 + if num_sample != 0: + reprint = True + print "To get another sample press 'n' or anyother key to answer" + k = las.get_key() + if k != 'n': break + elif k == "3": + las.say_text(elem) + reprint = False + elif k == "4": + print "Word Length:" + str(len(elem)) + elif k =="*": + las.exit_game() + else: + cluemode = False + + answer = raw_input( "Your answer: " ) + answer = answer.strip() + las.say_text("You typed\n") + shout(answer) + if answer == elem: + las.say_text("Correct") + las.ans_correct(wordid) + return True + else: + las.ans_incorrect(wordid) + las.say_text("Incorrect") + print "Incorrect. The correct spelling is.. " + las.say_text("The correct spelling is..\n") + shout(elem) + return False ######################################################################################## def shout(string): - for character in string: - print character + " " - las.saytext(character) + for character in string: + print character + " " + las.say_text(character) ######################################################################################## -def getskilllevel(): +def get_skill_level(): # 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) - + print 'Enter skill level between 1 and 9, or * to quit' + las.say_text("Skill level") + k = las.get_key() + if k == "*": + las.exit_game() # quit the program + print k + las.say_text(k) + skill_level = int(k) + 3 + las.set_skill_level( skill_level) if __name__=='__main__': - las = listenspell() - las.clearscreen() - las.playsound("begin") - - 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() + las = Listenspell() + las.clear_screen() + las.play_sound("begin") + + gameover = False + while gameover == False: + levelsize = 7 # this stays constant throughout + + get_skill_level() + wordidlist = las.load_wordid(levelsize + 3) # At max Three error possible + num_words = len(wordidlist) + las.clear_screen() + + #### MAIN LOOP + + las.play_sound("begin") + + + for index in range(num_words): + + ## print pronouncedict + ## print pronouncedictstring + ## print pronouncelist + wordid = wordidlist[index] + result = play_word(wordid) + print "Your score is " + str(las.getpoints()) + + if result == True: + if las.get_words_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.get_key() + if k == '%': + break # Restart game with next level + else: + las.exit_game() + + if las.get_words_correct == 7: + continue # Restart game with next level + else: + gameover = True + las.say_text("Game Over") + print "Game Over." + las.exit_game() -- cgit v0.9.1