import string


def format_sample(sample):
    """
    将单个样本转化为方便阅读的字符串
    
    Parameters
    ----------
    sample: dict{'tokens': list[str], 'h': [str, str, list[list[int]]], 't': [str, str, list[list[int]]]}
        FewRel数据集中的单个句子

    Returns
    ----------
    res: sentence
        方便阅读的字符串
        
    """
    for idx in sample['h'][2]:
        sample['tokens'][idx[0]] = '{' + sample['tokens'][idx[0]]
        sample['tokens'][idx[-1]] = sample['tokens'][idx[-1]] + '}'
        
    for idx in sample['t'][2]:
        sample['tokens'][idx[0]] = '[' + sample['tokens'][idx[0]]
        sample['tokens'][idx[-1]] = sample['tokens'][idx[-1]] + ']'
     
    sentence = ''
    sz = len(sample['tokens'])
    
    for i in range(sz):
        cur = sample['tokens'][i]
        nxt = None
        if i + 1 < sz:
            nxt = sample['tokens'][i + 1]
        sentence += cur
        if nxt is not None and nxt not in string.punctuation and cur not in '([-':
            sentence += ' '
    
    return sentence