code: def charset_table_to_dict(tablestring):
      """Takes a string with the contents of a Sphinx charset table file and
      returns a mapping object (a defaultdict, actually) of the kind expected by
      the unicode.translate() method: that is, it maps a character number to a unicode
      character or None if the character is not a valid word character.
  
      The Sphinx charset table format is described at
      http://www.sphinxsearch.com/docs/current.html#conf-charset-table.
      """
  
      # map = {}
      map = defaultdict(lambda: None)
      for line in tablestring.split("\n"):
          if not line or line.startswith("#"):
              continue
          line = _dewhite.sub("", line)
          for item in line.split(","):
              if not item:
                  continue
              match = _range_map.match(item)
              if match:
                  start1 = charspec_to_int(match.group(1))
                  end1 = charspec_to_int(match.group(2))
                  start2 = charspec_to_int(match.group(3))
                  end2 = charspec_to_int(match.group(4))
                  assert (end1 - start1) == (end2 - start2)
                  try:
                      for fromord, tooord in izip(
                          xrange(start1, end1 + 1),
                          xrange(start2, end2 + 1),
                      ):
                          map[fromord] = unichr(tooord)
                  except ValueError:
                      pass
                  continue
  
              match = _char_map.match(item)
              if match:
                  fromord = charspec_to_int(match.group(1))
                  toord = charspec_to_int(match.group(2))
                  try:
                      map[fromord] = unichr(toord)
                  except ValueError:
                      pass
                  continue
  
              match = _stray_char.match(item)
              if match:
                  ord = charspec_to_int(match.group(0))
                  try:
                      map[ord] = unichr(ord)
                  except ValueError:
                      pass
                  continue
  
              match = _stray_range.match(item)
              if match:
                  start = charspec_to_int(match.group(1))
                  end = charspec_to_int(match.group(2))
                  try:
                      for ord in xrange(start, end + 1):
                          map[ord] = unichr(ord)
                  except ValueError:
                      pass
                  continue
  
              match = _checker_range.match(item)
              if match:
                  fromord = charspec_to_int(match.group(1))
                  toord = charspec_to_int(match.group(2))
                  assert toord - fromord % 2 == 0
                  for ord in xrange(fromord, toord + 1, 2):
                      try:
                          map[ord] = unichr(ord + 1)
                          map[ord + 1] = unichr(ord + 1)
                      except ValueError:
                          pass
                  continue
  
              raise Exception("Don't know what to do with %r" % item)
      return dict(map)
  
imports: from collections import defaultdict
  import re
  from whoosh.compat import izip, u, iteritems, unichr, xrange
label_desc: Use image node_id 2 for calling a global function with example usage: charset_table_to_dict(tablestring) and returns: dict