import string
import word

_chartable = {}
for x in range(ord("0"), ord("9")+1):
    _chartable[chr(x)] = x - ord("0")
for x in range(ord("a"), ord("z")+1):
    _chartable[chr(x)] = x - ord("a") + 10
for x in range(ord("A"), ord("Z")+1):
    _chartable[chr(x)] = x - ord("A") + 36

def _chars2code(x):
    return _chartable[x[0]]*62*62 + _chartable[x[1]]*62 + _chartable[x[2]]


class Text:

    def __init__(self):

        self.__info = {}
        self.__word_table = {}
        self.__word_order = []

    def get_property(self, key):
        return self.__info.get(key)

    def length(self):
        return len(self.__word_order)

    def get_token(self, i):
        return self.__word_table[self.__word_order[i]]

    def load(self, filename):

        fd = file(filename)

        mode = 0
        word_count = 0

        for line in fd.xreadlines():
            line = line.strip()

            if mode == 0: # reading header info
                i = line.find(":")
                if i >= 0:
                    key = line[:i].strip()
                    val = line[i+1:].strip()
                    self.__info[key] = val
                    if key == "Tokens":
                        word_count = string.atoi(val)
                else:
                    mode = 1

            elif mode == 1: # reading word list
                tok = line.split(" ")
                num = string.atoi(tok[0], 16)
                word_str = tok[1].replace("_", " ")
                self.__word_table[num] = word.get_word(word_str)
                if len(self.__word_table) == word_count:
                    mode = 2

            else: # mod == 2, reading the actual text
                i = 0
                while i < len(line):
                    if line[i] == "[":
                        self.__word_order.append(0)
                        i += 1
                    elif line[i] == "]":
                        self.__word_order.append(1)
                        i += 1
                    else:
                        num = _chars2code(line[i:i+3])
                        self.__word_order.append(num)
                        i += 3

            
