Convert the classicial six-tuple representation of MCH readings into IPA
    (or Baxter's ASCII system). 
    This function is more or less implemented in MiddleChinese.

def sixtuple2baxter(chars, debug=False, rhymebook=None):
    """ 
    Convert the classicial six-tuple representation of MCH readings into IPA
    (or Baxter's ASCII system). 
    This function is more or less implemented in MiddleChinese.
    """
    if not rhymebook:
        rhymebook = _cd.GY

    if len(chars) != 6:
        raise ValueError('chars should be a sixtuple')
    
    # convert chars to long chars
    chars = gbk2big5(chars)

    # assign basic values
    she,hu,deng,diao,yun,sheng = list(chars) 
    
    # try converting the values to mch representations
    initial = rhymebook['sheng'].get(sheng, '?')
    final = rhymebook['yun'].get(yun, '?')
    tone = rhymebook['diao'].get(diao, '?')
    medial = rhymebook['hu'].get(hu, '?')
    division = rhymebook['deng'].get(deng, '?')
        
    # debug is for cross-checking
    if debug:
        return [(sheng, initial), (hu, medial), (deng, division),(yun, final),
                (diao, tone)]

    # check and raise error if things are not handled
    if "?" in [initial, final, tone, medial, division]:
        raise ValueError("Unrecognized elements in {0}.".format(
            ' '.join([initial, final, tone, medial, division])))

    # treat the final if division is 3 and they start with 'j', note that so
    # far, we don't handle chongnius
    final = final[1:] if final[0] == 'j' and division in '4' \
            else final
    final = final[1:] if final[0] == 'j' and division in '3' \
            else final

    # reduce finals starting with 'w'
    final = final[1:] if final[0] == 'w' else final
    
    # resolve the medial (the hu) by checking for labial initial
    medial = '' if (initial[0] in 'pbm' and '*' not in final) \
            or final[0] in 'u' \
            or 'o' in final and not '*' in final and not '?' in final \
            else medial
    
    # correct for initials with sandeng-i
    initial = initial[:-1] if initial.endswith('j') else initial

    # get the medial corrected by deng
    medial = "j" + medial if division == '3' \
            and 'i' not in final \
            and 'y' not in initial \
            else medial

    # deprive the rime from its leading "j" if we have a medial
    final = final[1:] if final[0] in 'j' and 'j' in medial else final
    final = final[1:] if final[0] in 'w' and 'w' in medial else final
    final = final[1:] if final[0] == '*' or final[0] == '?' else final
    final = 'i' + final[1:] if final[0] == '!' \
            and division == '4' \
            and 'i' not in final \
            and (initial[0] in "pbmkgx'" or initial.startswith('ng')) \
            else final

    # chongniu medial-re-order
    medial = 'j' + medial if division == '4' \
            and '!' in final \
            and 'j' not in medial \
            and (initial[0] in "pbmkgx'" or initial.startswith('ng')) \
            else medial

    final = final[1:] if final[0] == '!' else final
    
    # put everything together
    return [initial,medial,final,tone]