Example #1
0
    def spell(self, num):
        """Return the spelling of the given integer

        Arguments:
          num   -- number to spell

        Return value:
          A string with num's spelling.

        """

        if num == 0:
            return self.NUMBERS[0]

        # *** Pass 1. Apply rules to decompose the number ***
        tokens = self._parse_num(num)
        # Renumber orders
        order = 0
        for i in range(len(tokens)-1, -1, -1):
            if isorder(tokens[i]):
                order += 1
                tokens[i] = order
        tokens = squash(isorder, tokens)
        logging.debug("Number decomposition:\n    %s\n", tokens)

        # *** Pass 2. Apply list transformations ***
        processed_tokens = apply_passes(tokens, self.PASSES, self.META)
        for index, token in enumerate(processed_tokens):
            if isnum(token):
                processed_tokens[index] = self.NUMBERS[int(token)]
            elif isorder(token):
                processed_tokens[index] = self.ORDERS[token]
        result = ''.join(processed_tokens).rstrip()

        logging.debug("Final components:\n    %s\n", processed_tokens)

        # Finally, squash any sequence of whitespace into a single space
        return re.sub(r'\s+', ' ', result)
Example #2
0
def apply_passes(tokens, passes, meta):
    """Magic processing with index mangling

    Returns a new list with processed tokens.

    """
    # distill tokens into a list of tuples with no whitespace or words
    processed_tokens = [(index, x) for index, x in enumerate(tokens)
                        if isnum(x) or isorder(x)]
    secondary_list = [x for index, x in processed_tokens]
    parts = tokens[:]
    logging.debug("Processed: %s", processed_tokens)
    logging.debug("Secondary: %s", secondary_list)
    logging.debug("Component: %s", parts)

    pass_no = 1
    for pass_ in passes:
        parser = listparse.Parser(pass_, meta)
        new_list, ranges = parser.sub(secondary_list)
        if not ranges:
            continue

        stored_list = secondary_list[:]
        for r in ranges:
            start, end = r
            start_index = processed_tokens[start][0]
            end_index = processed_tokens[end-1][0]

            processed_tokens[start:end] = [(-1, None)]

            subst = [new_list[start]]
            secondary_list[start:end] = subst
            parts[start_index:end_index+1] = subst

            for i in range(start+1, len(processed_tokens)):
                index, token = processed_tokens[i]
                processed_tokens[i] = (index - (end_index - start_index), token)
        logging.debug("Pass #%s:\n    %s\n    -> %s\n    -> %s\n", pass_no, stored_list, pass_, secondary_list)
        pass_no += 1
    if pass_no > 1:
        logging.debug("After final pass:\n    %s\n", parts)

    return parts