def categories_to_tree(categories, paths):
    """Takes output from assign_number_to_top_categories and creates a tree
    of dictionaries.  The keys are the numeric index and the values are
    tuples of (name, sub-tree)"""
    d = (None, {})
    for cat, fullpath in zip(categories, paths):
        pos = d
        for idx, (n, name) in enumerate(cat):
            if idx == len(cat) - 1:
                v = fullpath
            else:
                v = {}
            entry = pos[1].setdefault(n, (name, v))
            assert(pos[1][n][0] == name)
            pos = entry
    return d[1]