import re


def importGWT(filename,initialId=1):
    """Get the a neighborhood structure from a GWT file.
    
    :param filename: name of the file (String)
    :type filename: string
    :param initialId: First id of the areas.
    :type initialId: integer 

    :rtype: contiguity dictionary.

    **Example 1** Storing a GWT neighborhood structure into a layer
    object::

        import clusterpy
        china = clusterpy.importArcData("clusterpy/data_examples/china")
        china.customW = clusterpy.importGWT("clusterpy/data_examples/china_gwt_658.193052.gwt")

    """
    finp = open(filename)
    finp.readline()
    w = {}
    reg = re.compile(r"(\d+)\s(\d+)\s+(\d+.\d*)")
    for line in finp:
        items = reg.match(line.strip())
        if items:
            id = int(items.group(1))
            neigh = int(items.group(2))
            if id - initialId in w:
                w[id - initialId].append(neigh - initialId)
            else:
                w[id - initialId] = [neigh - initialId]
        else:
            raise NameError("File structure is not from a GWT file")
    return w