From 7d681cf0e758f070260f47bbd8073d8aa252fb06 Mon Sep 17 00:00:00 2001 From: Roberto Cristaldo Date: Tue, 29 Jan 2013 01:18:32 +0000 Subject: Backend working. Front end con un problema menor en el lugar relativo --- diff --git a/Ejemplo.py b/Ejemplo.py index 1d01cd2..d112ff5 100755 --- a/Ejemplo.py +++ b/Ejemplo.py @@ -62,6 +62,12 @@ def main(): say(speech_to_say) + options_places = choice_letter.random_places + print 'Opciones' + print 'Opcion 1 %s' %options_places[0] + print 'Opcion 2 %s' %options_places[1] + print 'Opcion 3 %s' %options_places[2] + def say(text): Popen(['espeak', '-v', 'es', text]) diff --git a/activity.py b/activity.py index a8718f4..e147d46 100644 --- a/activity.py +++ b/activity.py @@ -17,10 +17,11 @@ import gtk import logging import elephant +import subprocess -from random import choice - -from gettext import gettext as _ +from time import sleep +from random import choice +from gettext import gettext as _ from sugar.activity import activity from sugar.graphics.toolbarbox import ToolbarBox @@ -76,10 +77,11 @@ class ElephantActivity(activity.Activity): # Instances to Elephants classes words = elephant.word_list() - choice_letter = elephant.letters() - - + # Get a word form word list and path + word, path = self.get_random_word(words) + choice_letter = elephant.letters(word) + # Vertical box conteinter definition vbox_main = gtk.VBox() @@ -93,13 +95,8 @@ class ElephantActivity(activity.Activity): button_repeat.set_label('Repetir') button_option1 = gtk.Button() - button_option1.set_label('Opcion 1:') - button_option2 = gtk.Button() - button_option2.set_label('Opcion 2:') - button_option3 = gtk.Button() - button_option3.set_label('Opcion 3:') #Old activity title #image_word = gtk.Image() @@ -110,10 +107,6 @@ class ElephantActivity(activity.Activity): #label_word = gtk.Label() #label_word.set_text('Elefante') - - #Get a word form word list and path - word, path = self.get_random_word(words) - #Image and label word image_word = self.show_image(path) label_word = self.show_label(word) @@ -134,8 +127,33 @@ class ElephantActivity(activity.Activity): hbox_03.add(button_option2) hbox_03.add(button_option3) - #ShowMeTheMoney!!! + # Connections + button_repeat.connect('clicked', self.__button_clicked_cb, \ + word, \ + choice_letter.letter, \ + choice_letter.relative_place, \ + choice_letter.place_word) + + label_word.connect('focus', self.__label_focus_cb, \ + word, \ + choice_letter.letter, \ + choice_letter.relative_place, \ + choice_letter.place_word) + + options_places = choice_letter.random_places + label = self.set_opton_label('Opcion 1:', options_places[0]) + button_option1.set_label(label) + + label = self.set_opton_label('Opcion 2:', options_places[1]) + button_option2.set_label(label) + + label = self.set_opton_label('Opcion 3:', options_places[2]) + button_option3.set_label(label) + + + # ShowMeTheMoney!!! vbox_main.show_all() + #label_word.is_focus = true # Get random word form word list def get_random_word(self, words): @@ -161,3 +179,56 @@ class ElephantActivity(activity.Activity): return label + # Create proper lables for options buttons + def set_opton_label(self, option, position): + label = option + label += ' Es la ' + label += position + label += ' letra' + + return label + + # Say the word and riddle on button click + def __button_clicked_cb(self, button, \ + word, \ + letter_to_say, \ + relative_place, \ + place_word): + + self.speech_to_say(word, \ + letter_to_say, \ + relative_place, \ + place_word) + + # Say the word and riddle on label focus + def __label_focus_cb(self, label, \ + word, \ + letter_to_say, \ + relative_place, \ + place_word): + + self.speech_to_say(word, \ + letter_to_say, \ + relative_place, \ + place_word) + + # Say the word and riddle + def speech_to_say (self, word, \ + letter_to_say, \ + relative_place, \ + place_word): + + speech_to_say = 'Donde esta la ' + if relative_place == False: + speech_to_say += '' + else: + speech_to_say += place_word + speech_to_say += ' letra ' + speech_to_say += letter_to_say + + self.say(word) + self.say(speech_to_say) + + # Say.. sayyyy.... saaaaayyyy... + def say(self, text): + subprocess.call(['espeak', '-v', 'es', text]) diff --git a/elephant.py b/elephant.py index 5d5383b..28aed27 100755 --- a/elephant.py +++ b/elephant.py @@ -46,16 +46,28 @@ class word_list: class letters: def __init__(self, word): - self.letter_index, self.letter = self._get_letter_index(word) + self._arguments = self._get_letter_index(word) + self.letter_index = self._arguments[0] + self.letter = self._arguments[1] + # Just for debugging + # print self.letter + # print self.letter_index self.all_indexes = self._get_all_indexes(word, self.letter) + # Just for debuggin + # print self.all_indexes self.false_options = self._get_false_options(word, self.all_indexes) + # Just for debugging + # print self.false_options self.relative_place = self._get_relative_place(self.letter_index, \ - self.all_indexes) + self.all_indexes) - self.place_word = self._translate_relative_place(self.relative_place) + self.place_word = self._translate_to_str(self.relative_place) + + self.random_places = self._get_random_places(self.letter_index, \ + self.false_options) # Return a random letter for a given word and index def _get_letter_index(self, word): @@ -68,7 +80,7 @@ class letters: # Get the letter at position letter_index letter = word[letter_index] - return letter_index+1, letter + return letter_index, letter # Return list of indexes for every ocurrence ot the letter in the word. def _get_all_indexes(self, word, letter): @@ -76,12 +88,12 @@ class letters: # Init vars word_len = len(word) indexes = [] - i = 0 - + # Generates a list with indexes of ocurrences of the letter - for i in range(word_len): + i = 0 + while (i < word_len): if word[i] == letter: - indexes.append(i+1) + indexes.append(i) i+=1 return indexes @@ -89,22 +101,15 @@ class letters: # Return false options non-overlaping with true options def _get_false_options(self, word, true_indexes): word_len = len(word) - - #parser = ConfigParser.SafeConfigParser() - #parser.read(CONF_FILE) - - #false_options = parser.get(MAIN_SECTION, 'Falseoptions') - false_choices = [] - - for i in range(0, 2): - false_choice = random.randrange(word_len) - if (false_choice in true_indexes or \ - false_choice in false_choices) == True: - pass - else: - false_choices.append(false_choice) - i += 1 + + i = 0 + while (i < 2): + false_choice = random.randint(0, word_len - 1) + if (false_choice not in true_indexes and \ + false_choice not in false_choices) == True: + false_choices.append(false_choice) + i += 1 return false_choices @@ -117,16 +122,38 @@ class letters: relative_place = true_indexes.index(letter_index) - return relative_place+1 + return relative_place # Translate relative place to words - def _translate_relative_place(self, relative_place): + def _translate_to_str(self, relative_place): if relative_place == False: return False - relative_place -= 1 - places = ['Primera', 'Segunda', 'Tercera', 'Cuarta', 'Quinta', 'Sexta'] + places = ['Primera', \ + 'Segunda', \ + 'Tercera', \ + 'Cuarta', \ + 'Quinta', \ + 'Sexta', \ + 'Septima', \ + 'Octava', \ + 'Novena', \ + 'Decima'] + + return places[relative_place] + + # Generate random places for options + def _get_random_places(self, true_choice, false_choice): + options = [] + + options.append(self._translate_to_str(true_choice)) + options.append(self._translate_to_str(false_choice[0])) + options.append(self._translate_to_str(false_choice[1])) + + random.shuffle(options) + + return options - return places[relative_place] \ No newline at end of file + \ No newline at end of file -- cgit v0.9.1