human
Can you provide a the Python program you requested given the following:
Complete the below
def decode_htmlentities(text):

    ''''Decode HTML entities in text, coded as hex, decimal or named.
    Adapted from http://github.com/sku/python-twitter-ircbot/blob/321d94e0e40d0acc92f5bf57d126b57369da70de/html_decode.py
    >>> u = u\'E tu vivrai nel terrore - L&#x27;aldil&#xE0.
(1981)\'
    >>> print(decode_htmlentities(u).encode(\'UTF-8\'))
    E tu vivrai nel terrore - L\'aldilÃ  (1981)
    >>> print(decode_htmlentities("l&#39;eau"))
    l\'eau
    >>> print(decode_htmlentities("foo &lt.
bar"))
    foo < bar'
    '''
assistant
#Here is the Python program you requested.
    def substitute_entity(match):
        try:
            ent = match.group(3)
            if (match.group(1) == '#'):
                if (match.group(2) == ''):
                    return safe_unichr(int(ent))
                elif (match.group(2) in ['x', 'X']):
                    return safe_unichr(int(ent, 16))
            else:
                cp = n2cp.get(ent)
                if cp:
                    return safe_unichr(cp)
                else:
                    return match.group()
        except:
            return match.group()
    return RE_HTML_ENTITY.sub(substitute_entity, text)