

def convert_html_entities(s):
    import htmlentitydefs
    matches = re.findall('&#\\d+;', s)
    if (len(matches) > 0):
        hits = set(matches)
        for hit in hits:
            name = hit[2:(- 1)]
            try:
                entnum = int(name)
                s = s.replace(hit, unichr(entnum))
            except ValueError:
                pass
    matches = re.findall('&#[xX][0-9a-fA-F]+;', s)
    if (len(matches) > 0):
        hits = set(matches)
        for hit in hits:
            hex = hit[3:(- 1)]
            try:
                entnum = int(hex, 16)
                s = s.replace(hit, unichr(entnum))
            except ValueError:
                pass
    matches = re.findall('&\\w+;', s)
    hits = set(matches)
    amp = '&'
    if (amp in hits):
        hits.remove(amp)
    for hit in hits:
        name = hit[1:(- 1)]
        if htmlentitydefs.name2codepoint.has_key(name):
            s = s.replace(hit, unichr(htmlentitydefs.name2codepoint[name]))
    s = s.replace(amp, '&')
    return s

dataset/ETHPy150Open miklevin/pipulate/common.py convert_html_entities/original