Ejemplo n.º 1
0
def encode(word):
	
	# a dictionnary for all possible symbol
	symbols = {}
	
	# counts the number of occurences each symbol
	for letter in word:
		if letter in symbols:
			symbols[letter] += 1
		else :
			symbols[letter] = 1
	
	
	# here we construct the binary tree with tuples :
	tree = buildTree(symbols)
	
	# we have to return the tree too for decoding later
	# we could encode it better, but its native string representation is ok
	treeString = str(tree)
	treeString = treeString.replace(", ", ",")
	
	# the code we'll return, which first contains the length taken by the tree
	code = BitStream(12)
	code.int = len(treeString)
	
	
	# then we add the tree to the code
	codedTree = BitArray(8 * len(treeString))
	codedTree.bytes = treeString
	code.append(codedTree)
	
	# a recursive traversal of the tree allows us to create a dictionnary of matching symbols and codes for encoding :
	symbols = buildEncodingDict(tree)
	
	# finally we encode the word
	codedWord = encodeWithDict(symbols, word)
	
	code.append(codedWord)
	
	return code