def top_x_bleu(query_dic, mark, x=1):
    """
    Calculate the top x average bleu value predictions ranking by item, x default is set above
    :param query_dic: dict, key is qid, value is (item, bleu) tuple list, which will be ranked by 'item' as key
    :param mark:string, which indicates which method is evaluated, also used as output file name here.
    :param x:int, define top x
    :return:average bleu score
    """
    all_total = 0.0
    with open(top_bleu_path + mark, 'w') as writer:
        for k in query_dic:
            candidate_lst = query_dic[k]
            top_x = sorted(candidate_lst, key=lambda a: a[0], reverse=True)[:x]
            total = 0
            for t in top_x:
                total += t[1]
            ave_bleu = total / x
            writer.write('%s\tAverageBleu:%f\tTop%d:%s\n' % (k, ave_bleu, x, str(top_x)))

            all_total += ave_bleu
            if k in contrast_dic:
                contrast_dic[k].append(str(ave_bleu))
            else:
                contrast_dic[k] = []
                contrast_dic[k].append(str(ave_bleu))
    result_string = '%s\ttop%d_Bleu:\t%f' % (mark, x, all_total / len(query_dic))
    print result_string
    # eval_result_dict['Bleu'].append(result_string)
    return ['Bleu', result_string]