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 __HOLE__:
        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

#   ____                                        __  __           _       _      
#  / ___|___  _ __ ___  _ __ ___   ___  _ __   |  \/  | ___   __| |_   _| | ___ 
# | |   / _ \| '_ ` _ \| '_ ` _ \ / _ \| '_ \  | |\/| |/ _ \ / _` | | | | |/ _ \
# | |__| (_) | | | | | | | | | | | (_) | | | | | |  | | (_) | (_| | |_| | |  __/
#  \____\___/|_| |_| |_|_| |_| |_|\___/|_| |_| |_|  |_|\___/ \__,_|\__,_|_|\___|
#                                                                               
#                         In every job that must be done,
#                           There is an element of fun.
#                   You find the fun and snap, the job's a game
#                          And every task you undertake
#                            Becomes a piece of cake...
dataset/ETHPy150Open miklevin/pipulate/common.py/convert_html_entities