import random, copy, os, sys

def normalise_symbol(name):
    """normalise_symbol(string)

    replace CapWords, mixedCase, and under_scores with hyphenated-lower-case
    replace leading "is-" with trailing "?"

    e.g.:
    CapWords -> cap-words
    mixedCase -> mixed-case
    TLAName -> tla-name
    TheTLAName -> the-tla-name
    under_scores -> under-scores
    hyphenated-lower-case -> hyphenated-lower-case
    is-stupid -> stupid?
    is_stupid -> stupid?
    isStupid -> stupid?

    ... etc.

    lll -> lll
    UUU -> lll
    Ull -> lll

    UUl -> l-ll

    lUl -> l-ll # lU -> l-l
    lUU -> l-ll # lU -> l-l
    UlU -> ll-l # lU -> l-l
    llU -> ll-l # lU -> l-l
    """
    n, ame = name[0].lower(), name[1:]
    celd = name[0]
    cmeld = ''
    for c in ame: #XXX this is completely broken
        if c == '_':
            n += '-'
        elif c.islower() and celd.isupper() and cmeld.isupper():
            n = n[:-1] + '-' + n[-1:] + c
        elif c.isupper():
            if celd.islower():
                n += '-'
            n += c.lower()
        else:
            n += c
        cmeld = celd
        celd = c
    if n[:3] == 'is-':
        n = n[3:] + '?'
    return n

def clone(corpus, **kwargs):
    """clone(corpus, **attributes)

    Create a duplicate corpus, overriding the named attributes.

    Example: clone(myevent, target=that) creates a duplicate event with a
    different `target' attribute.

    The clone is a shallow copy.
    """
    c = copy.copy(corpus)
    vars(c).update(kwargs)
    return c
