Parse NAME structure assuming ALTREE dialect.

    In ALTREE dialect maiden name (if present) is saved as SURN sub-record
    and is also appended to family name in parens. Given name is saved in
    GIVN sub-record. Few examples:

    No maiden name:

        1 NAME John /Smith/
        2 GIVN John

    With maiden name:

        1 NAME Jane /Smith (Ivanova)/
        2 GIVN Jane
        2 SURN Ivanova

    No maiden name

        1 NAME Mers /Daimler (-Benz)/
        2 GIVN Mers

    Because family name can also contain parens it's not enough to parse
    family name and guess maiden name from it, we also have to check for
    SURN record.

    ALTREE also replaces empty names with question mark, we undo that too.

    :param record: NAME record
    :return: tuple with 3 or 4 elements, first three elements of tuple are
        the same as returned from :py:meth:`split_name` method, fourth element
        (if present) denotes maiden name.

def parse_name_altree(record):
    """Parse NAME structure assuming ALTREE dialect.

    In ALTREE dialect maiden name (if present) is saved as SURN sub-record
    and is also appended to family name in parens. Given name is saved in
    GIVN sub-record. Few examples:

    No maiden name:

        1 NAME John /Smith/
        2 GIVN John

    With maiden name:

        1 NAME Jane /Smith (Ivanova)/
        2 GIVN Jane
        2 SURN Ivanova

    No maiden name

        1 NAME Mers /Daimler (-Benz)/
        2 GIVN Mers

    Because family name can also contain parens it's not enough to parse
    family name and guess maiden name from it, we also have to check for
    SURN record.

    ALTREE also replaces empty names with question mark, we undo that too.

    :param record: NAME record
    :return: tuple with 3 or 4 elements, first three elements of tuple are
        the same as returned from :py:meth:`split_name` method, fourth element
        (if present) denotes maiden name.
    """
    name_tuple = split_name(record.value)
    if name_tuple[1] == '?':
        name_tuple = (name_tuple[0], '', name_tuple[2])
    maiden = record.sub_tag_value("SURN")
    if maiden:
        # strip "(maiden)" from family name
        ending = '(' + maiden + ')'
        surname = name_tuple[1]
        if surname.endswith(ending):
            surname = surname[:-len(ending)].rstrip()
            if surname == '?':
                surname = ''
        name_tuple = (name_tuple[0], surname, name_tuple[2], maiden)
    return name_tuple