Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/elephant.py
blob: fc22814fe3522f94591619d98830e5283e7e5118 (plain)
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
124
125
126
127
128
129
130
131
import ConfigParser
import random

# Global definition
CONF_FILE='elephant.conf'
CAT_SECTION='Categories'
WORDS_SECTION='Words'
MAIN_SECTION='Main'

# Class word_list
class word_list:
    
    def __init__(self):
        # Number of words
        self.number_of_words = len(self.get_word_list())

    # Get all categories from config file
    def get_categories(self):    
        parser = ConfigParser.SafeConfigParser()
        parser.read(CONF_FILE)

        full_list = parser.items(CAT_SECTION)
        category_list = [row[1] for row in full_list]

        return category_list

    # Get all words from a specifir category
    #def get_words_in_category(self, category):
    #    parser = ConfigParser.SafeConfigParser()
    #    parser.read(CONF_FILE)
    #
    #    word_list = parser.options(category)
    #
    #    return word_list

    # Get all words form config file
    def get_word_list(self):
        parser = ConfigParser.SafeConfigParser()
        parser.read(CONF_FILE)
        
        full_list = parser.items(WORDS_SECTION)
        word_list = [row[0] for row in full_list]

        return word_list

    # Get path of choosen image
    def get_word_image_path(self, word):
        parser = ConfigParser.SafeConfigParser()
        parser.read(CONF_FILE)

        image_path = parser.get(WORDS_SECTION, word)

        return image_path

# Class letters
class letters:
    
    def __init__(self, word):
        self.letter_index, self.letter = self._get_letter_index(word)

    # Return a random letter for a given word and index
    def _get_letter_index(self, word):

        word_len = len(word)

        # Generate a random index of a letter in the word
        letter_index = random.randint(0, word_len - 1)

        # Get the letter at position letter_index
        letter = word[letter_index]
        
        return letter_index+1, letter

    # Return list of indexes for every ocurrence ot the letter in the word.
    def get_all_indexes(self, word, letter):

        # 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):
            if word[i] == letter:
                indexes.append(i+1)
            i+=1
               
        return indexes

    # 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 [1, 2]:
            false_choice = random.randrange(word_len)
            if (false_choice in true_indexes) == True:
                pass
            else:
                false_choices.append(false_choice)
                i += 1

        return false_choices

    # Return the relative place of a letter in case there are multiples
    # ocurrences of the letter. False in case there are only one.
    def get_relative_place(self, letter_index, true_indexes):

        if len(true_indexes) == 1:
            return False

        relative_place = true_indexes.index(letter_index)

        return relative_place+1


    # Translate relative place to words
    def translate_relative_place(self, relative_place):

        if relative_place == False:
            return False

        relative_place -= 1
        places = ['Primera', 'Segunda', 'Tercera', 'Cuarta', 'Quinta', 'Sexta']

        return places[relative_place]