Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utility.py
blob: baf42c509c9e5ac0e57d69f967be24927a914dd2 (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
#!/usr/bin/env python

'''Utility functions module.
'''
import constants
import re

def get_unicode(data):
	if not isinstance(data, unicode):
		return unicode(data, "utf-8")
	return data

def create_board(size):
	board = []
	# To do hacer que sea dinamico
	if size == constants.DEFAULT_BOARD_SIZE:
		for i in range(0, size):
			board.append(['','','','','','','','','','','','','','',''])
		
	elif size == 9:
		for i in range(0, size):
			board.append(['','','','','','','','',''])		
	
	return board

def get_letters_from_file(lenguage = constants.DEFAULT_LENGUAGE):
	letters = {}
	# Todo utilizar modulo os.join.path para formar el path al archivo de letras....
	letters_path = constants.LENGUAGES_PATH + "/" + lenguage + "/" + "letters.txt"
	
	file = open(letters_path, "r")
	list = file.readlines()
	# format "Letter": (count, score)
	pattern = re.compile(r".(?P<letter>[\w\s]+).: .(?P<count>\d+),(?P<score>\d+).")
	
	for i in list:
		m = pattern.match(i)
		if m:
			info = (m.group('count') , m.group('score'))
			letters[m.group('letter')] = (int(info[0]) , int(info[1]))	
	
	return letters
	
	
def get_dictionary(lenguage = constants.DEFAULT_LENGUAGE):
	''' Get the dictionary used in the Game
	
		@param lenguage: the lenguage.
		@return: a list containing the dictionary.
	'''
	dict = []
	dict_path = constants.DICTIONARIES_PATH + "/dict-" + lenguage + ".txt"
	file = open(dict_path, "r")
	dict = f.readlines()

def search_word(lenguage = constants.DEFAULT_LENGUAGE, word, dict):
	''' Look for the specified word in the dictionary 
	
		@param lenguage: the lenguage.
		@param word: word to look for
		@param dict: the dictionary.
		@return: True or False
	'''
	l2 = filter(lambda n: re.match(word, n), dict)
	if not l2:
		return False
	return True